forked from itsToggle/pd_inject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
542 lines (418 loc) · 19.7 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
"""Main module for running tasks.
This module sets up a mock plex server using Flask, ngrok for public HTTPS access, and defines routes for handling different tasks. It includes functionality for session management, encoding content, caching, handling media provider routes, metadata, availability, search, download, and agent routes. It also sets up the mock servers based on configurations and runs the Flask application in a separate thread.
"""
from flask import Flask, request
from flask_caching import Cache
from pyngrok import ngrok, conf, process
import threading
import logging
import json
import zlib
import requests
import regex
from dicttoxml import dicttoxml
from modules import common
from modules import plex
from modules import torrentio
from modules import realdebrid
from settings import settings
import time
import copy
import uuid
import os
# Create a logger object for this module
logger = logging.getLogger(__name__)
# Initialize a session object from the common module for HTTP requests
session = common.session()
# Specify the port number for the Flask application
PORT = 8008
def configure_logging():
"""Configure logging settings for the application."""
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s (%(levelname)s) [%(module)s.%(funcName)s] %(message)s'
)
# Initialize the Flask application
app = Flask(__name__)
# Set up caching for the Flask application with simple backend and default timeout
cache = Cache(app, config={'CACHE_TYPE': 'simple', "CACHE_DEFAULT_TIMEOUT": 300})
# Configure the default region for ngrok and connect to establish a public URL
conf.get_default().region = 'us'
public_url = ngrok.connect(PORT)
# Silence the Flask, Ngrok and dicttoxml loggers
logging.getLogger('werkzeug').setLevel(logging.WARNING)
logging.getLogger('dicttoxml').setLevel(logging.WARNING)
process.logger.setLevel(logging.WARNING)
process.ngrok_logger.setLevel(logging.WARNING)
# Start the Flask application in a separate thread to allow concurrent processing
threading.Thread(target=app.run, kwargs={
'use_reloader': False,
'debug': False,
'port': PORT
}).start()
# Initialize mock Plex servers based on settings and the obtained ngrok public URL
mock_servers = [plex.mockserver(server, public_url.public_url) for server in settings.get("versions")]
def zlib_encode(content):
"""Compress content using zlib with highest compression level."""
zlib_compress = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS)
data = zlib_compress.compress(content) + zlib_compress.flush()
return data
def deflate_encode(content):
"""Compress content using deflate algorithm."""
deflate_compress = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)
data = deflate_compress.compress(content) + deflate_compress.flush()
return data
def gzip_encode(content):
"""Compress content using gzip algorithm."""
gzip_compress = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)
data = gzip_compress.compress(content) + gzip_compress.flush()
return data
def format(content, request):
"""Encode and format the response content based on the request's accepted MIME types.
Args:
content: The response content to be encoded and formatted.
request: The Flask request object containing the client's request details.
Returns:
A tuple containing the encoded content, HTTP status code, and response headers.
"""
# Convert content to XML or JSON based on request's accepted MIME types
if request.accept_mimetypes.best == 'application/xml' and isinstance(content, dict):
content = dicttoxml(content, root=False, attr_type=False)
elif isinstance(content, dict):
content = json.dumps(content).encode('utf-8')
# Compress the content and prepare headers for the response
uncompressed = len(content)
content = gzip_encode(content)
compressed = len(content)
headers = {
'X-Plex-Content-Original-Length': uncompressed,
'X-Plex-Content-Compressed-Length': compressed,
'Content-Encoding': 'gzip',
'Content-Type': request.accept_mimetypes.best,
'Access-Control-Allow-Origin': '*',
'X-Plex-Protocol': '1.0',
'Vary': 'Origin, X-Plex-Token',
'Connection': 'keep-alive'
}
return content, 200, headers
# Define global variables for managing state and locks for thread safety
releases = None
processing_lock = threading.Lock()
processing = False
search_lock = threading.Lock()
last_search_time = 0
last_search_term = ""
data_store = {}
@app.route('/media/providers', methods=['GET'])
@app.route('/<path:server>/media/providers', methods=['GET'])
def providers(server=mock_servers[0].IDENTIFIER):
"""Handle requests for media providers.
This route is used to determine if a server is online via regular polling.
Its also used to define the servers name as shown in the Plex UI.
Args:
server: The optional identifier for a specific mock server.
(Mobile and TV clients dont respect the "server" identifier in /<path:server>/... and rather call the endpoint directly /... )
Returns:
The response content, status code, and headers as formatted by the `format` function.
"""
# Select the appropriate mock server based on the server identifier
mock_server = next((s for s in mock_servers if s.IDENTIFIER == server), None)
# Decode the request's full path
path = requests.utils.unquote(request.full_path)
# Get content from the mock server's provider method
content = mock_server.provider('includePreferences=1' in path)
# Format the content based on the request headers and return the response
content, code, headers = format(content, request)
return content, code, headers
@app.route('/<path:server>/library/metadata/<path:guid>', methods=['GET'])
def metadata(server, guid):
"""Handle requests for metadata.
This (currently unused) route can be used to fake the presence of media items in your mocked libraries
Args:
server: The optional identifier for a specific mock server.
(Mobile and TV clients dont respect the "server" identifier in /<path:server>/... and rather call the endpoint directly /... )
Returns:
The response content, status code, and headers as formatted by the `format` function.
"""
mock_server = next((s for s in mock_servers if s.IDENTIFIER == server), None)
if 'availabilities' in guid:
content = {}
content, code, headers = format(content, request)
return content, code, headers
content = mock_server.metadata(requests.utils.unquote(guid), request.args)
content, code, headers = format(content, request)
return content, code, headers
@app.route('/library/all', methods=['GET'])
@app.route('/<path:server>/library/all', methods=['GET'])
@cache.cached(timeout=300, query_string=True)
def availability(server=None):
"""Handle requests for library availability.
This route checks the availability of items in the mocked library by scraping releases
and comparing them against a debrid service. Found releases are returned as library entries.
Args:
server: The optional identifier for a specific mock server.
(Mobile and TV clients dont respect the "server" identifier in /<path:server>/... and rather call the endpoint directly /... )
Returns:
The response content, status code, and headers as formatted by the `format` function.
"""
start_time = time.time()
mock_server = next((s for s in mock_servers if s.IDENTIFIER == server), None)
path = requests.utils.unquote(request.full_path)
guid = regex.search(r'(?<=guid=)(.*?)(?=&)', path, regex.I).group()
global releases, processing, data_store
if mock_server:
mock_server = [mock_server]
else:
mock_server = mock_servers
with processing_lock:
if not processing:
processing = True
type, imdb, s, e = mock_server[0].identify(path)
releases = torrentio.scrape(type, imdb, s, e)
realdebrid.check(releases)
common.releases.type_filter(releases, type, s, e)
local_releases = copy.deepcopy(releases)
time.sleep(0.05)
processing = False
metadata = []
for server in mock_server:
if len(mock_server) > 1:
local_releases = copy.deepcopy(releases)
local_releases = common.releases.sort(server, local_releases)
unique_id = str(uuid.uuid4())
data_store[unique_id] = local_releases
for i, release in enumerate(local_releases[:server.RESULTS]):
metadata += [{
"ratingKey": "2785",
"key": f"/download/{requests.utils.quote(unique_id)}/{i}",
"librarySectionID": 2,
"librarySectionKey": "/library/sections/2",
"guid": guid,
"librarySectionTitle": server.SERVERNAME,
"Media": [
{
"videoResolution": (release['title'] if len(mock_server) == 1 else release['resolution']),
},
],
},]
content = {
"MediaContainer": {
"size": 1,
"allowSync": False,
"identifier": "com.plexapp.plugins.library",
"mediaTagPrefix": "/system/bundle/media/flags/",
"mediaTagVersion": 1655122614,
"Metadata": metadata
}
}
content, code, headers = format(content, request)
logger.info(f"took {time.time() - start_time:.2f}s")
return content, code, headers
@app.route('/hubs/search', methods=['GET'])
@app.route('/<path:server>/hubs/search', methods=['GET'])
@cache.cached(timeout=300, query_string=True)
def search(server=None):
"""Handle search requests.
This route searches torrentio by search query and returns search results by scraping releases
and comparing them against a debrid service. Found releases are returned as library entries.
Args:
server: The optional identifier for a specific mock server.
(Mobile and TV clients dont respect the "server" identifier in /<path:server>/... and rather call the endpoint directly /... )
Returns:
The response content, status code, and headers as formatted by the `format` function.
"""
start_time = time.time()
mock_server = next((s for s in mock_servers if s.IDENTIFIER == server), None)
query = request.args.get('query', '')
# Debounce the search
global last_search_time, releases, processing
with search_lock:
# Replace the old timer with the new one
last_search_time = time.time()
last_search_term = copy.deepcopy(query)
while True:
with search_lock:
# If a new search has been initiated by the same client, break the loop
if time.time() - last_search_time > 1:
break
time.sleep(0.1)
if not query == last_search_term:
content = {}
content, code, headers = format(content, request)
return content, code, headers
if mock_server:
mock_server = [mock_server]
else:
mock_server = mock_servers
with processing_lock:
if not processing:
processing = True
type, imdb, s, e = torrentio.search(query)
releases = torrentio.scrape(type, imdb, s, e)
realdebrid.check(releases)
common.releases.type_filter(releases, type, s, e)
local_releases = copy.deepcopy(releases)
time.sleep(0.05)
processing = False
metadata = []
for server in mock_server:
if len(mock_server) > 1:
local_releases = copy.deepcopy(releases)
local_releases = common.releases.sort(server, local_releases)
unique_id = str(uuid.uuid4())
data_store[unique_id] = local_releases
for i, release in enumerate(local_releases[:server.RESULTS]):
metadata += [
{
"librarySectionTitle": "Torrentio",
"score": "1",
"ratingKey": "",
"key": f"/download/{requests.utils.quote(unique_id)}/{i}",
"guid": release['title'],
"studio": "Hyperobject Industries",
"type": "movie",
"title": release['title'],
"librarySectionID": 1,
"librarySectionKey": "/library/sections/1",
"contentRating": "R",
"summary": "",
"rating": 5.5,
"audienceRating": 7.8,
"year": 2022,
"tagline": "Based on truly possible events.",
"thumb": "https://static.thenounproject.com/png/1390707-200.png",
"art": "https://static.thenounproject.com/png/1390707-200.png",
"duration": 8591530,
"originallyAvailableAt": "2021-12-24",
"addedAt": 1655228225,
"updatedAt": 1655228225,
"audienceRatingImage": "rottentomatoes://image.rating.upright",
"primaryExtraKey": "/library/metadata/89",
"ratingImage": "rottentomatoes://image.rating.rotten",
}
]
content = {
"MediaContainer": {
"size": 18,
"Hub": [
{
"title": "Movies",
"type": "movie",
"hubIdentifier": "movie",
"context": "",
"size": 1,
"more": False,
"style": "shelf",
"Metadata": metadata
}
]
}
}
content, code, headers = format(content, request)
logger.info(f"took {time.time() - start_time:.2f}s")
return content, code, headers
@app.route('/download/<path:id>/<path:num>', methods=['GET'])
@app.route('/<path:server>/download/<path:id>/<path:num>', methods=['GET'])
def download(server=mock_servers[0].IDENTIFIER, id="", num=0):
"""Handle download requests.
This (only internally used) route actually downloads the releases
Args:
server: The optional identifier for a specific mock server.
(Mobile and TV clients dont respect the "server" identifier in /<path:server>/... and rather call the endpoint directly /... )
Returns:
The response content, status code, and headers as formatted by the `format` function.
"""
start_time = time.time()
releases = data_store[id]
for release in releases[int(num):]:
if realdebrid.download(release):
break
plex.library.refresh(release)
content = {}
content, code, headers = format(content, request)
logger.info(f"took {time.time() - start_time:.2f}s")
return content, code, headers
@app.route('/<path:server>/system/agents', methods=['GET'])
def agents(server):
"""Handle search requests.
This (currently unused) route would be called when accessing the mock servers "agent" settings.
The idea would be to allow the user to control this programs settings through the official plex UI.
Args:
server: The optional identifier for a specific mock server.
(Mobile and TV clients dont respect the "server" identifier in /<path:server>/... and rather call the endpoint directly /... )
Returns:
The response content, status code, and headers as formatted by the `format` function.
"""
mock_server = next((s for s in mock_servers if s.IDENTIFIER == server), None)
content = mock_server.agents(requests.utils.unquote(request.full_path))
content, code, headers = format(content, request)
return content, code, headers
@app.route('/photo/:/transcode', methods=['GET'])
@app.route('/<path:server>/photo/:/transcode', methods=['GET'])
def transcode(server):
"""Handle photo transcode requests.
This route is used by Plex to request a client specific resize of metadata pictures.
It also allows us to respond with a different user profile picture for our mock servers.
Args:
server: The optional identifier for a specific mock server.
(Mobile and TV clients dont respect the "server" identifier in /<path:server>/... and rather call the endpoint directly /... )
Returns:
The response content, status code, and headers as formatted by the `format` function.
"""
url = request.args.get('url', None)
if "avatar" in url:
url = 'https://i.ibb.co/w4BnkC9/GwxAcDV.png'
response = session.get(url)
content = response.content
content, code, headers = format(content, request)
return content, code, headers
@app.route('/library/sections/2/prefs', methods=['GET'])
@app.route('/:/prefs', methods=['GET'])
@app.route('/<path:server>/library/sections/2/prefs', methods=['GET'])
@app.route('/<path:server>/:/prefs', methods=['GET'])
def prefs(server=mock_servers[0].IDENTIFIER):
"""Handle preference requests.
These routes are used by Plex to request a servers preferences.
This is also where the mock server name is once again asked for.
Args:
server: The optional identifier for a specific mock server.
(Mobile and TV clients dont respect the "server" identifier in /<path:server>/... and rather call the endpoint directly /... )
Returns:
The response content, status code, and headers as formatted by the `format` function.
"""
mock_server = next((s for s in mock_servers if s.IDENTIFIER == server), None)
content = mock_server.prefs()
content, code, headers = format(content, request)
return content, code, headers
@app.route('/:/websockets/notifications', methods=['GET'])
@app.route('/updater/status', methods=['GET'])
@app.route('/updater/check', methods=['GET', 'PUT'])
@app.route('/accounts/1', methods=['GET'])
@app.route('/myplex/account', methods=['GET'])
@app.route('/<path:server>/:/websockets/notifications', methods=['GET'])
@app.route('/<path:server>/updater/status', methods=['GET'])
@app.route('/<path:server>/updater/check', methods=['GET', 'PUT'])
@app.route('/<path:server>/accounts/1', methods=['GET'])
@app.route('/<path:server>/myplex/account', methods=['GET'])
def empty(server=mock_servers[0].IDENTIFIER):
"""Handle keep-alive requests.
These routes are used by Plex to determine a variety of things, but they can be handled by empty responses.
Its just important to respond at all, to keep the server alive in the eyes of plex.
Args:
server: The optional identifier for a specific mock server.
(Mobile and TV clients dont respect the "server" identifier in /<path:server>/... and rather call the endpoint directly /... )
Returns:
The response content, status code, and headers as formatted by the `format` function.
"""
content = {"MediaContainer": {"size": 0}}
content, code, headers = format(content, request)
return content, code, headers
# Entry point of the script
if __name__ == "__main__":
# Configure logging before main program execution
configure_logging()
# Register mock servers for handling routes
for server in mock_servers:
server.register()
# Log the information about ngrok public URL where the mock servers are running
logging.info(f"mock servers running on ngrok https: {public_url}")