Skip to content

Instantly share code, notes, and snippets.

@spuniun
Last active May 5, 2018 23:59
Show Gist options
  • Save spuniun/547d740dc383189c9c63b3039362a895 to your computer and use it in GitHub Desktop.
Save spuniun/547d740dc383189c9c63b3039362a895 to your computer and use it in GitHub Desktop.
Sync user playlists of recommended items to all users on Plex Media Server.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Description: Sync user playlists of recommended items to all users.
# Author: /u/spuniun
# Requires: plexapi, requests, xmltodict
# Usage: Any user playlist named "<plex username> Recommends" will be synced to all
# other users on that Plex server.
# Thanks: Based entirely off of SwiftPanda16's sync_playlists_to_users.py
# https://gist.github.com/JonnyWong16/2607abf0e3431b6f133861bbe1bb694e
# and pbrink231's plex_top_playlists
# https://github.com/pbrink231/plex_top_playlists
# from which this was cobbled together.
import requests
import xmltodict
from plexapi.server import PlexServer
### EDIT SETTINGS ###
PLEX_URL = 'http://localhost:32400'
PLEX_TOKEN = 'XXXXXXXXXXXXXXX'
## CODE BELOW ##
def get_user_tokens(server_id):
headers = {'Accept': 'application/json', 'X-Plex-Token': PLEX_TOKEN}
result = requests.get('https://plex.tv/api/servers/{server_id}/shared_servers?X-Plex-Token={token}'.format(server_id=server_id, token=PLEX_TOKEN), headers=headers)
xmlData = xmltodict.parse(result.content)
users = {user['@username']: user['@accessToken'] for user in xmlData['MediaContainer']['SharedServer']}
return users
def remove_playlist(plex, playlist_name):
for playlist in plex.playlists():
if playlist.title == playlist_name:
try:
playlist.delete()
#print("{}: Playlist deleted".format(playlist_name))
except:
print("ERROR - cannot delete playlist: {}".format(playlist_name))
return None
def create_playlists(plex, list, playlist_name):
# Remove old playlists
#print('{}: Checking if playlist exist to delete if needed'.format(playlist_name))
remove_playlist(plex, playlist_name)
plex.createPlaylist(playlist_name, list)
#print("{}: playlist created".format(playlist_name))
def loop_plex_users(plex, list, playlist_name):
#update my list
create_playlists(plex, list, playlist_name)
#update list for shared users
plex_users = get_user_tokens(plex.machineIdentifier)
for user in plex_users:
print("{}: updating playlist for user {}".format(playlist_name, user))
user_token = plex_users[user]
user_plex = PlexServer(PLEX_URL, user_token)
create_playlists(user_plex, list, playlist_name)
def main():
plex = PlexServer(PLEX_URL, PLEX_TOKEN)
plex_users = get_user_tokens(plex.machineIdentifier)
plex_playlists = {playlist.title: playlist.items() for playlist in plex.playlists()}
print(plex.myPlexUsername.split("@")[0])
for playlist in plex_playlists:
if playlist.lower() == plex.myPlexUsername.split("@")[0].lower() + ' recommends':
shared_list = plex_playlists.get(playlist)
loop_plex_users(plex, shared_list, playlist)
for user in plex_users:
user_token = plex_users[user]
user_plex = PlexServer(PLEX_URL, user_token)
user_playlists = {playlist.title: playlist.items() for playlist in user_plex.playlists()}
print(user)
for playlist in user_playlists:
if playlist.lower() == user.lower() + ' recommends':
user_shared_list = user_playlists.get(playlist)
loop_plex_users(plex, user_shared_list, playlist)
return
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment