-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresound.py
388 lines (300 loc) · 15.1 KB
/
resound.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Description: Sync all Plex playlists to shared users.
# Author: SuperRiderTH, /u/SwiftPanda16
# Requires: plexapi, requests, xmltodict
import sys
import requests
import xmltodict
import plexapi
from plexapi.server import PlexServer
from plexapi.exceptions import BadRequest, NotFound, Unauthorized
from packaging import version
# Recent versions of Plex return a status code when deleting a playlist
# that the PlexAPI doesn't understand until a version after 4.1.2.
# We want to catch that and check it if our version is at or lower than that.
PLEXAPI_CHECK_204 = version.parse(plexapi.VERSION) <= version.parse("4.1.2")
# Note: I am disabling warnings for urllib3 because we are making an unverified
# HTTPS request. This is just to hide the unverified HTTPS request warning, as
# this should be running on the server itself, accessing localhost.
# HTTPS use cases are not supported at this time, but may work.
import urllib3
urllib3.disable_warnings()
### Plex Settings ###
PLEX_URL = 'http://localhost:32400'
PLEX_TOKEN = 'xxxxxxxxxxxxxxxxxxxx'
### User Settings ###
# If the whitelist is empty, all users will be synchronized.
# An alternative name can be added with a comma, for example: "[email protected],Name"
# Example: USER_WHITELIST = ['User', '[email protected],User2', 'User3,Real Name of User 3']
USER_WHITELIST = []
# If you want to include the server owner in syncing, this must be set to True.
INCLUDE_SERVER_OWNER = True
SERVER_OWNER_USER = 'ServerOwner'
# Note: This isn't actually the user.
# It is just used for this script so it can be whatever you want.
# Can also have an alternative name, example: "ServerOwner,Name".
### Sync Settings ###
# These are the characters used in playlists to determine if a playlist
# should be ignored, or was made by this script on a prior run.
IGNORE_CHARACTER = "!"
SYNC_CHARACTER = "|"
### Variables that should not be touched by humans ##
MAJOR_VERSION = 2
MINOR_VERSION = 0
PATCH_VERSION = 2
VERSION = f"{MAJOR_VERSION}.{MINOR_VERSION}.{PATCH_VERSION}"
USERS = []
NAMES = []
USER_SERVER = []
PLEX_SERVER = PlexServer(PLEX_URL, PLEX_TOKEN)
MY_PLEX = PLEX_SERVER.myPlexAccount()
ARG_CLEAN = False
ARG_DRYRUN = False
## CODE BELOW ##
def fetch_plex_api(path='', method='GET', plextv=False, **kwargs):
"""Fetches data from the Plex API"""
url = 'https://plex.tv' if plextv else PLEX_URL.rstrip('/')
headers = {'X-Plex-Token': PLEX_TOKEN,
'Accept': 'application/json'}
params = {}
if kwargs:
params.update(kwargs)
try:
if method.upper() == 'GET':
r = requests.get(url + path,
headers=headers, params=params, verify=False)
elif method.upper() == 'POST':
r = requests.post(url + path,
headers=headers, params=params, verify=False)
elif method.upper() == 'PUT':
r = requests.put(url + path,
headers=headers, params=params, verify=False)
elif method.upper() == 'DELETE':
r = requests.delete(url + path,
headers=headers, params=params, verify=False)
else:
print("Invalid request method provided: {method}".format(method=method))
return
if r and len(r.content):
if 'application/json' in r.headers['Content-Type']:
return r.json()
elif 'application/xml' in r.headers['Content-Type']:
return xmltodict.parse(r.content)
else:
return r.content
else:
return r.content
except Exception as e:
print("Error fetching from Plex API: {err}".format(err=e))
def get_user_tokens(server_id):
api_users = fetch_plex_api('/api/users', plextv=True)
api_shared_servers = fetch_plex_api('/api/servers/{server_id}/shared_servers'.format(server_id=server_id), plextv=True)
user_ids = {user['@id']: user.get('@username', user.get('@title')) for user in api_users['MediaContainer']['User']}
users = {user_ids[user['@userID']]: user['@accessToken'] for user in api_shared_servers['MediaContainer']['SharedServer']}
return users
def init_users():
global SERVER_OWNER_USER, USER_WHITELIST, USER_SERVER
# Get all the users on the server for verification, or to use if whitelist is empty.
plex_users = get_user_tokens(PLEX_SERVER.machineIdentifier)
print ("========================================")
print ("Users")
print ("========================================")
# Use the list of users if there is no whitelist.
if len(USER_WHITELIST) == 0:
for x in plex_users:
USER_WHITELIST.append(x)
else:
print ("------------------------------")
print ("Whitelist exists, syncing users:")
print ("------------------------------")
# If we are including the server owner, we want to add them to the whitelist.
if INCLUDE_SERVER_OWNER:
USER_WHITELIST.append(SERVER_OWNER_USER)
if len(SERVER_OWNER_USER.split(',')) > 1:
SERVER_OWNER_USER = SERVER_OWNER_USER.split(',')[0]
# Go through and cleanup the whitelist for alternative names.
for x in USER_WHITELIST:
if len(x.split(',')) > 1:
USERS.append(x.split(',')[0])
NAMES.append(x.split(',')[1])
else:
USERS.append(x)
NAMES.append(x)
# Verify that all the users actually exist in the server.
for x in USERS:
if x in plex_users or x == SERVER_OWNER_USER:
print(x)
else:
print ('User "' + x + '" not found! Aborting.')
return True
print ("========================================")
print("Getting user servers...")
# Login as all the users, store their servers for later.
for x in USERS:
if x == SERVER_OWNER_USER:
USER_SERVER.append(PLEX_SERVER)
else:
user = MY_PLEX.user(x)
token = user.get_token(PLEX_SERVER.machineIdentifier)
USER_SERVER.append(PlexServer(PLEX_URL, token))
return False
def get_owner_user(playlistTitle):
for name in NAMES:
if name == playlistTitle.split(": ")[0].strip(SYNC_CHARACTER):
return USERS[NAMES.index(name)]
assert "User not found!"
def process_playlists():
for user in USERS:
print ("========================================")
print (NAMES[USERS.index(user)] + " - " + user)
print ("========================================")
for playlist in USER_SERVER[USERS.index(user)].playlists():
if playlist.title.startswith(IGNORE_CHARACTER):
print("Ignoring " + playlist.title)
continue
# Get the items in the playlist, to check if it actually has items.
playlistItems = playlist.items()
# We also want to ignore smart playlists.
if playlist.smart :
print("Playlist " + playlist.title + " is a smart playlist, ignoring...")
# And also ignore empty playlists.
elif not playlistItems:
print("Playlist " + playlist.title + " is empty.")
# If we find a synced playlist, we want to see if it exists.
else:
if playlist.title.startswith(SYNC_CHARACTER):
if ARG_CLEAN:
if not ARG_DRYRUN:
print("Removing playlist: " + playlist.title)
else:
print("Dry run, not removing: " + playlist.title)
else:
print("Checking if synced playlist exists: " + playlist.title)
# We want to see if the playlist exists in the original user.
# If it does not, we just remove it.
playlistExists = False
owner = get_owner_user(playlist.title)
# We need to see if the owner of the playlist actually exists,
# this will handle any synced playlists from removed users.
if owner in USERS:
for playlistCheck in USER_SERVER[USERS.index(owner)].playlists():
if playlist.title.split(": ")[1] == (playlistCheck.title):
playlistExists = True
pass
pass
# If we are cleaning up the playlists, we just pretend they all don't exist.
if ARG_CLEAN:
playlistExists = False
# If the play list exists, we move on and don't
if playlistExists:
pass
else:
# We grab a potential exception to see if it is actually alright.
if PLEXAPI_CHECK_204:
try:
USER_SERVER[USERS.index(user)].playlist(playlist.title).delete()
except BadRequest as e:
message = getattr(e, 'message', str(e))
if message.startswith("(204)"):
pass
else:
raise BadRequest(message)
else:
if not ARG_DRYRUN:
USER_SERVER[USERS.index(user)].playlist(playlist.title).delete()
if not ARG_CLEAN:
print("Playlist not found, deleted playlist...")
else:
print("Playlist not found.")
# We found an actual playlist, so we want to sync it with other users.
else:
if ARG_CLEAN:
pass
else:
print ("========================================")
print("Found: " + playlist.title)
print ("========================================")
for userCheck in USERS:
if USERS.index(userCheck) != USERS.index(user):
# We need to find the playlist, otherwise we create it.
playlistExists = False
playlistCheckName = SYNC_CHARACTER + NAMES[USERS.index(user)] + ": " + playlist.title
for playlistCheck in USER_SERVER[USERS.index(userCheck)].playlists():
if playlistCheckName == playlistCheck.title:
playlistExists = True
targetPlaylist = playlistCheck
pass
pass
pass
# If the playlist exists, we check the items and add or remove them as necessary.
if playlistExists:
print ("------------------------------")
print("Syncing playlist for: " + NAMES[USERS.index(userCheck)])
print ("------------------------------")
# Remove items if not found in original playlist.
for item in targetPlaylist.items():
if item not in playlist.items():
if not ARG_DRYRUN:
print('- "' + item.title + '"' + " not found in playlist! Removing...")
targetPlaylist.removeItems(item)
pass
else:
print('- "' + item.title + '"' + " not found in playlist!")
pass
pass
# Add items if not found in synced playlist.
for item in playlist.items():
if item not in targetPlaylist.items():
if not ARG_DRYRUN:
print('- "' + item.title + '"' + " not found in playlist! Adding...")
targetPlaylist.addItems(item)
pass
else:
print('- "' + item.title + '"' + " not found in playlist!")
pass
pass
# Playlist does not exist, so we just clone it in it's entirety.
else:
if not ARG_DRYRUN:
print ("Creating '" + playlistCheckName + "' for " + NAMES[USERS.index(userCheck)])
USER_SERVER[USERS.index(userCheck)].createPlaylist( playlistCheckName, items=playlist.items() )
pass
else:
print ("Dry run, not creating '" + playlistCheckName + "' for " + NAMES[USERS.index(userCheck)])
pass
pass
def main():
print ("")
print ("Resound Version: " + VERSION)
print ("Plex Server Version: " + PLEX_SERVER.version)
print ("Python-PlexAPI Version : " + plexapi.VERSION)
global ARG_CLEAN, ARG_DRYRUN
# Check for any arguments.
if len(sys.argv) > 1:
for x in sys.argv:
if x == "clean":
ARG_CLEAN = True
if x == "dryrun":
ARG_DRYRUN = True
if ARG_CLEAN:
print ("========================================")
print ("Clean argument detected, just removing playlists.")
print ("========================================")
if ARG_DRYRUN:
print ("========================================")
print ("Dry run, will not modify playlists.")
print ("========================================")
if PLEXAPI_CHECK_204:
print ("PlexAPI version is <= 4.1.2, will ignore status code 204 when deleting.")
print ("For details:\nhttps://github.com/pkkid/python-plexapi/pull/580\n")
if init_users():
return
if process_playlists():
return
return
if __name__ == "__main__":
main()
print ("========================================")
print("Done.")