-
Notifications
You must be signed in to change notification settings - Fork 23
/
plex_request.py
415 lines (326 loc) · 18.3 KB
/
plex_request.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
from datetime import datetime
import os
import traceback
import re
import requests
import declxml as xml
from flask import Flask, jsonify, request, Response
from flask_caching import Cache
from shared.discord import discordError, discordUpdate
from shared.shared import plex, plexHeaders, pathToScript
from shared.overseerr import requestItem, getUserForPlexServerToken
from werkzeug.routing import BaseConverter, ValidationError
mediaTypeNums = {
"movie": "1",
"show": "2",
"season": "3",
"episode": "4"
}
class MetadataRatingKeyConverter(BaseConverter):
regex = '[0-9a-fA-F]{24}'
def to_python(self, value):
if re.match(self.regex, value):
return value
raise ValidationError()
def to_url(self, value):
return value
# Instantiate the app
app = Flask(__name__)
app.config.from_object(__name__)
app.url_map.strict_slashes = False
app.url_map.converters['metadataRatingKey'] = MetadataRatingKeyConverter
# Set up caching
cacheDir = os.path.join(pathToScript, "../cache")
for file in os.listdir(cacheDir):
filePath = os.path.join(cacheDir, file)
try:
if os.path.isfile(filePath):
os.unlink(filePath)
except Exception as e:
print(e)
cache = Cache(app, config={'CACHE_TYPE': 'filesystem', 'CACHE_DEFAULT_TIMEOUT': 300, 'CACHE_DIR': cacheDir})
_print = print
def print(*values: object):
_print(f"[{datetime.now()}]", *values, flush=True)
def processDict(key, value):
return xml.dictionary(key, [*traverseDict(value, processDict, processList, processElse)], required=False)
def processList(key, value):
if any(value):
alias = 'Metadata' if key == 'Metadata' else None
key = 'Video' if key == 'Metadata' else key
return xml.array(processDict(key, value[0]), alias)
else:
return xml.array(xml.dictionary(key, []))
def processElse(key, _):
return xml.string('.', attribute=key, required=False)
def traverseDict(thisDict, processDict, processList, processElse):
return (traverse(key, value, processDict, processList, processElse) for key, value in thisDict.items())
def traverseList(thisList, key, processDict, processList, processElse):
return (traverse(key, value, processDict, processList, processElse) for value in thisList)
def traverse(key, value, processDict, processList, processElse):
if processDict and isinstance(value, dict):
return processDict(key, value)
elif processList and isinstance(value, list):
return processList(key, value)
else:
return processElse(key, value)
@app.route('/library/request/<mediaType>/<mediaTypeNum>/<ratingKey>', methods=['GET'])
@app.route('/library/request/<mediaType>/<mediaTypeNum>/<ratingKey>/<children>', methods=['GET'])
@app.route('/library/request/<mediaType>/<mediaTypeNum>/<ratingKey>/season/<season>', methods=['GET'])
@app.route('/library/request/<mediaType>/<mediaTypeNum>/<ratingKey>/season/<season>/<children>', methods=['GET'])
def libraryRequest(mediaType, mediaTypeNum, ratingKey, season=None, children=None):
token = request.headers.get('X-Plex-Token', None) or request.args.get('X-Plex-Token', None)
originalRatingKey = ratingKey
try:
if not mediaTypeNum or mediaTypeNum not in mediaTypeNums.values():
print(f"Unknown mediaTypeNum: {mediaTypeNum}")
discordError(f"Unknown mediaTypeNum: {mediaTypeNum}")
if mediaTypeNum == mediaTypeNums['show'] or children:
skipRequest = True
headers = {
**plexHeaders,
'X-Plex-Token': token
}
guid = f"plex://{mediaType}/{ratingKey}"
params = {**request.args}
if mediaTypeNum != '0':
params['type'] = mediaTypeNum
if mediaType == 'show':
params['show.guid'] = guid
if mediaTypeNum == mediaTypeNums['season']:
params['season.index'] = season
else:
params['guid'] = guid
allRequest = requests.get(f"{plex['serverHost']}/library/all", headers=headers, params=params)
all = allRequest.json()
mediaContainer = all['MediaContainer']
# If you try and get a season that doesn't exist you'll get the show instead, if it does exist.
if ('Metadata' in mediaContainer and
((mediaContainer['Metadata'][0]['type'] == 'season' and mediaTypeNum == mediaTypeNums['season'])
or (mediaContainer['Metadata'][0]['type'] == 'movie' and mediaTypeNum == mediaTypeNums['movie'])
or (mediaContainer['Metadata'][0]['type'] == 'show' and mediaTypeNum == mediaTypeNums['show']))):
skipRequest = True
if not children:
if mediaTypeNum == mediaTypeNums['season']:
mediaContainer['Metadata'][0]['key'] = f"/library/request/{mediaType}/{mediaTypeNum}/{ratingKey}/season/{season}/children"
response = jsonify(all)
response.headers.add('Access-Control-Allow-Origin', 'https://app.plex.tv')
return response
if mediaTypeNum == mediaTypeNums['show']:
return children(mediaContainer['Metadata'][0]['ratingKey'])
metadataUrl = f"{plex['serverHost']}{mediaContainer['Metadata'][0]['key']}"
metadataRequest = requests.get(metadataUrl, headers=headers, params=request.args)
metadata = metadataRequest.json()
response = jsonify(metadata)
response.headers.add('Access-Control-Allow-Origin', 'https://app.plex.tv')
return response
else:
metadataHeaders = {**plexHeaders, 'X-Plex-Token': plex['serverApiKey']}
args = {k: v for k, v in request.args.items() if k != 'X-Plex-Token'}
if not children and mediaTypeNum == mediaTypeNums['season']:
metadataSeasonsRequest = requests.get(f"{plex['metadataHost']}library/metadata/{ratingKey}/children", headers=metadataHeaders, params=args)
metadataSeasons = metadataSeasonsRequest.json()
metadataSeason = next((s for s in metadataSeasons['MediaContainer']['Metadata'] if s['index'] == int(season)))
ratingKey = metadataSeason['ratingKey']
urlSuffix = "/children" if children else ""
metadataMetadataRequest = requests.get(f"{plex['metadataHost']}library/metadata/{ratingKey}{urlSuffix}", headers=metadataHeaders, params=args)
metadata = metadataMetadataRequest.json()
if 'MediaContainer' in metadata and 'Metadata' in metadata['MediaContainer']:
if children:
if mediaTypeNum == mediaTypeNums['season']:
metadata['MediaContainer']['Metadata'] = []
elif mediaTypeNum == mediaTypeNums['show']:
seasons = metadata['MediaContainer']['Metadata']
metadata['MediaContainer']['Metadata'] = []
metadata['MediaContainer'] = addRequestableSeasons(metadata['MediaContainer'], seasons, originalRatingKey)
else:
item = metadata['MediaContainer']['Metadata'][0]
parentTitle = item.get('parentTitle', '')
title = item.get('title', '')
title = f"{parentTitle}: {title}" if parentTitle else title
if mediaTypeNum == mediaTypeNums['show']:
item['title'] = f"Request - {title}"
else:
item['title'] = f"{title} - Requesting..."
if mediaTypeNum == mediaTypeNums['season']:
item['key'] = f"/library/request/{mediaType}/{mediaTypeNum}/{originalRatingKey}/season/{season}/children"
else:
item['key'] = f"/library/request/{mediaType}/{mediaTypeNum}/{originalRatingKey}/children"
response = jsonify(metadata)
response.headers.add('Access-Control-Allow-Origin', 'https://app.plex.tv')
return response
except:
e = traceback.format_exc()
print(f"Error in /library/request")
print(e)
discordError(f"Error in /library/request", e)
return 'Server Error', 500
finally:
if not locals().get('skipRequest', False):
title = locals().get('title', 'Untitled')
requestMedia(token, originalRatingKey, mediaType, season, title)
def requestMedia(token, ratingKey, mediaType, season, title):
try:
cacheKey = ratingKey if mediaType == 'movie' else f"{ratingKey}_{season}"
recentlyRequested = cache.get(cacheKey) or []
if token not in recentlyRequested:
user = getUserForPlexServerToken(token)
metadataHeaders = {**plexHeaders, 'X-Plex-Token': plex['serverApiKey']}
requestItem(user, ratingKey, datetime.now().timestamp(), metadataHeaders, getSeason=lambda: [int(season)])
recentlyRequested.append(token)
cache.set(cacheKey, recentlyRequested)
print(f"{title} - Requested by {user['displayName']} via Plex Request")
discordUpdate(f"{title} - Requested by {user['displayName']} via Plex Request", f"User Id: {user['id']}, Media Type: {mediaType}, {f'Season: {season},' if season else ''} Rating Key: {ratingKey}")
except:
e = traceback.format_exc()
print(f"Error in request")
print(e)
discordError(f"Error in request", e)
@app.route('/library/all', methods=['GET'])
def all():
try:
headers = {
**request.headers,
'Accept': 'application/json',
}
allRequest = requests.get(f"{plex['serverHost']}/library/all", headers=headers, params=request.args)
if allRequest.status_code != 200:
return allRequest.text, allRequest.status_code
all = allRequest.json()
mediaContainer = all['MediaContainer']
if not 'Metadata' in mediaContainer:
fullGuid = (request.args.get('guid', None) or request.args.get('show.guid'))
guidMatch = re.match('plex:\/\/(.+?)\/(.+?)(?:\/|$)', fullGuid)
mediaType, guid = guidMatch.group(1, 2)
season = request.args.get('season.index')
mediaTypeNum = request.args.get('type', mediaTypeNums['movie'] if mediaType == 'movie' else mediaTypeNums['season'] if season else mediaTypeNums['show'])
if mediaType != 'episode' and (mediaTypeNum != mediaTypeNums['season'] or season != '0'):
metadataHeaders = {**plexHeaders, 'X-Plex-Token': plex['serverApiKey']}
args = {k: v for k, v in request.args.items() if k != 'X-Plex-Token'}
urlSuffix = "/children" if mediaTypeNum == mediaTypeNums['season'] else ""
metadataAllRequest = requests.get(f"{plex['metadataHost']}library/metadata/{guid}{urlSuffix}", headers=metadataHeaders, params=args)
if metadataAllRequest.status_code == 200:
libraryId = plex['serverMovieLibraryId'] if mediaType == 'movie' else plex['serverTvShowLibraryId']
additionalMetadata = metadataAllRequest.json()['MediaContainer']['Metadata'][0]
if mediaTypeNum == mediaTypeNums['season'] or mediaTypeNum == mediaTypeNums['episode']:
additionalMetadata['key'] = f"/library/request/{mediaType}/{mediaTypeNum}/{guid}/season/{season}"
else:
additionalMetadata['key'] = f"/library/request/{mediaType}/{mediaTypeNum}/{guid}"
additionalMetadata['ratingKey'] = "12065"
additionalMetadata['librarySectionTitle'] = "Request Season :" if mediaTypeNum == mediaTypeNums['episode'] else "Request :"
additionalMetadata['librarySectionID'] = libraryId
additionalMetadata['librarySectionKey'] = f"/library/sections/{libraryId}"
additionalMetadata['Media'] = [{
"videoResolution": "Request Season :" if mediaTypeNum == mediaTypeNums['episode'] else "Request :"
}]
additionalMetadata['childCount'] = 0
mediaContainer['size'] = 1
mediaContainer['Metadata'] = [additionalMetadata]
if request.accept_mimetypes.best_match(['application/xml', 'application/json']) == 'application/json':
response = jsonify(all)
cors = allRequest.headers.get('Access-Control-Allow-Origin', None)
if cors:
response.headers.add('Access-Control-Allow-Origin', cors)
else:
processor = processDict('MediaContainer', mediaContainer)
xmlString = xml.serialize_to_string(processor, mediaContainer, ' ')
response = Response(xmlString, mimetype='application/xml')
if 'fullGuid' in locals():
print('Request in xml')
return response
except:
e = traceback.format_exc()
print(f"Error in /library/all")
print(e)
discordError(f"Error in /library/all", e)
return 'Server Error', 500
@app.route('/library/metadata/<metadataRatingKey:id>/children', methods=['GET'])
def metadataChildren(id):
metadataHeaders = {**plexHeaders, 'X-Plex-Token': plex['serverApiKey']}
args = {k: v for k, v in request.args.items() if k != 'X-Plex-Token'}
metadataChildrenRequest = requests.get(f"{plex['metadataHost']}library/metadata/{id}/children", headers=metadataHeaders, params=args)
if metadataChildrenRequest.status_code != 200:
return metadataChildrenRequest.text, metadataChildrenRequest.status_code
children = metadataChildrenRequest.json()
mediaContainer = children['MediaContainer']
if request.accept_mimetypes.best_match(['application/xml', 'application/json']) == 'application/json':
response = jsonify(children)
cors = metadataChildrenRequest.headers.get('Access-Control-Allow-Origin', None)
if cors:
response.headers.add('Access-Control-Allow-Origin', cors)
else:
processor = processDict('MediaContainer', mediaContainer)
xmlString = xml.serialize_to_string(processor, mediaContainer, ' ')
response = Response(xmlString, mimetype='application/xml')
if 'fullGuid' in locals():
print('Request in xml')
return response
@app.route('/library/metadata/<id>/children', methods=['GET'])
def children(id):
try:
headers = {
**request.headers,
'Accept': 'application/json',
}
childrenRequest = requests.get(f"{plex['serverHost']}/library/metadata/{id}/children", headers=headers, params=request.args)
if childrenRequest.status_code != 200:
return childrenRequest.text, childrenRequest.status_code
children = childrenRequest.json()
mediaContainer = children['MediaContainer']
if 'viewGroup' in mediaContainer and mediaContainer['viewGroup'] == "season" and 'Metadata' in mediaContainer and mediaContainer['Metadata']:
fullGuid = mediaContainer['Metadata'][0]['parentGuid']
guidMatch = re.match('plex:\/\/(.+?)\/(.+?)(?:\/|$)', fullGuid)
_, guid = guidMatch.group(1, 2)
metadataHeaders = {**plexHeaders, 'X-Plex-Token': plex['serverApiKey']}
args = {k: v for k, v in request.args.items() if k != 'X-Plex-Token'}
metadataChildrenRequest = requests.get(f"{plex['metadataHost']}library/metadata/{guid}/children", headers=metadataHeaders, params=args)
if metadataChildrenRequest.status_code == 200:
seasons = metadataChildrenRequest.json().get('MediaContainer', {}).get('Metadata', [])
mediaContainer = addRequestableSeasons(mediaContainer, seasons, guid)
if request.accept_mimetypes.best_match(['application/xml', 'application/json']) == 'application/json':
response = jsonify(children)
cors = childrenRequest.headers.get('Access-Control-Allow-Origin', None)
if cors:
response.headers.add('Access-Control-Allow-Origin', cors)
else:
processor = processDict('MediaContainer', mediaContainer)
xmlString = xml.serialize_to_string(processor, mediaContainer, ' ')
response = Response(xmlString, mimetype='application/xml')
if 'fullGuid' in locals():
print('Request in xml')
return response
except:
e = traceback.format_exc()
print(f"Error in /library/metadata/{id}/children")
print(e)
discordError(f"Error in /library/metadata/{id}/children", e)
return 'Server Error', 500
def addRequestableSeasons(mediaContainer, seasons, ratingKey):
allSeasons = [item for item in seasons if item['index'] != 0]
metadata = mediaContainer.get('Metadata', [])
existingMetadataIndices = {item['index']: item for item in metadata}
for item in allSeasons:
if item['index'] not in existingMetadataIndices:
item['title'] = f"Request - {item.get('title', '')}"
item['key'] = f"/library/request/show/{mediaTypeNums['season']}/{ratingKey}/season/{item['index']}"
item['ratingKey'] = "12065"
item.pop('Guid', None)
item.pop('Image', None)
item.pop('Role', None)
item.pop('banner', None)
item.pop('contentRating', None)
item.pop('hasGenericTitle', None)
item.pop('originallyAvailableAt', None)
item.pop('parentArt', None)
item.pop('parentType', None)
item.pop('publicPagesURL', None)
item.pop('userState', None)
item.pop('year', None)
item.pop('parentKey', None)
metadata.append(item)
metadata.sort(key=lambda x: x['index'])
mediaContainer['size'] = len(metadata)
mediaContainer['totalSize'] = len(metadata)
return mediaContainer
if __name__ == '__main__':
app.run('127.0.0.1', 12599, debug=True)