Skip to content

Instantly share code, notes, and snippets.

@JonnyWong16
Last active April 20, 2024 12:14
Show Gist options
  • Star 79 You must be signed in to star a gist
  • Fork 17 You must be signed in to fork a gist
  • Save JonnyWong16/2607abf0e3431b6f133861bbe1bb694e to your computer and use it in GitHub Desktop.
Save JonnyWong16/2607abf0e3431b6f133861bbe1bb694e to your computer and use it in GitHub Desktop.
Sync Plex playlists to shared users.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Description: Sync Plex playlists to shared users.
# Author: /u/SwiftPanda16
# Requires: plexapi
from plexapi.exceptions import NotFound
from plexapi.server import PlexServer
### EDIT SETTINGS ###
PLEX_URL = 'http://localhost:32400'
PLEX_TOKEN = 'xxxxxxxxxx'
# List of playlists to sync, set PLAYLISTS = None to sync all playlists
PLAYLISTS = ['Playlist 1', 'Playlist 2', 'Playlist 3']
# List of users to sync the playlists to, set USERS = None to sync to all users
USERS = ['Username 1', 'Username 2', 'Username 3']
### CODE BELOW ###
def sync_playlists():
plex = PlexServer(PLEX_URL, PLEX_TOKEN)
account = plex.myPlexAccount()
if PLAYLISTS is None:
playlists = plex.playlists()
else:
playlists = [plex.playlist(playlist) for playlist in PLAYLISTS]
if USERS is None:
users = account.users()
else:
users = [account.user(username) for username in USERS]
for user in users:
user_plex = plex.switchUser(user.username or user.title)
for playlist in playlists:
print(f"Syncing playlist '{playlist.title}' to user '{user.username or user.title}'...")
# Delete the old playlist
try:
user_plex.playlist(playlist.title).delete()
except NotFound:
pass
# Create a new playlist
user_plex.createPlaylist(playlist.title, items=playlist.items())
if __name__ == "__main__":
sync_playlists()
print("Done.")
@shinedou
Copy link

shinedou commented Sep 16, 2021

I worked it out the new plexapi kills the script revert to the older plexapi and it works

@brettwatson77 What version of plexapi did you have to revert to?

@brettwatson77
Copy link

brettwatson77 commented Sep 16, 2021 via email

@sourcecodemage
Copy link

sourcecodemage commented Nov 16, 2021

I had to revert to # PlexAPI Settings PROJECT = 'PlexAPI' VERSION = ‘4.1.2' And then the script worked again.. It its still working for me with this version of the Plex api Has the script been updated to work with the new plex api? B

On 17 Sep 2021, at 8:47 am, shinedou @.***> wrote: @shinedou commented on this gist. I worked it out the new plexapi kills the script revert to the older plexapi and it works What version of plexapi did you have to revert to? — You are receiving this because you commented. Reply to this email directly, view it on GitHub https://gist.github.com/2607abf0e3431b6f133861bbe1bb694e#gistcomment-3895839, or unsubscribe https://github.com/notifications/unsubscribe-auth/ARMFWJVBYKZTLCZGJNZLBJ3UCJXZXANCNFSM4H5X6J5A. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

I switched to 4.1.2 , still doesn't work

Is the what the settings should look like?
PLEX_URL = 'http://localhost:32400'
PLEX_TOKEN = 'removed'
PLAYLISTS = [ 'Treadmill'] # List of playlists to sync
USERS = [ 'john.smith' ] # List of users to sync the playlists to

john.smith is a managed user, token is for the Admin account.

Environment
Python = v3.9.7

pip freeze output:
charset-normalizer==2.0.7
idna==3.3
PlexAPI==4.1.2
requests==2.26.0
urllib3==1.26.7
XML2Dict==0.2.2
xmltodict==0.12.0

Error message
Traceback (most recent call last): File "/Users/jgooch/repos/sync_playlists/sync_playlists.py", line 112, in <module> main() File "/Users/jgooch/repos/sync_playlists/sync_playlists.py", line 78, in main plex_users = get_user_tokens(plex.machineIdentifier) File "/Users/jgooch/repos/sync_playlists/sync_playlists.py", line 72, in get_user_tokens users = {user_ids[user['@userID']]: user['@accessToken'] for user in api_shared_servers['MediaContainer']['SharedServer']} File "/Users/jgooch/repos/sync_playlists/sync_playlists.py", line 72, in <dictcomp> users = {user_ids[user['@userID']]: user['@accessToken'] for user in api_shared_servers['MediaContainer']['SharedServer']} TypeError: string indices must be integers

@snickers2k
Copy link

snickers2k commented Nov 16, 2021

syncing playlists is already possible in plex - officially (but of course, plex style, only for the admin).

vote here to get "better playlists", like having the "sharing power" for all users.

unsubscribed.

@sourcecodemage
Copy link

I had to revert to # PlexAPI Settings PROJECT = 'PlexAPI' VERSION = ‘4.1.2' And then the script worked again.. It its still working for me with this version of the Plex api Has the script been updated to work with the new plex api? B

On 17 Sep 2021, at 8:47 am, shinedou @.***> wrote: @shinedou commented on this gist. I worked it out the new plexapi kills the script revert to the older plexapi and it works What version of plexapi did you have to revert to? — You are receiving this because you commented. Reply to this email directly, view it on GitHub https://gist.github.com/2607abf0e3431b6f133861bbe1bb694e#gistcomment-3895839, or unsubscribe https://github.com/notifications/unsubscribe-auth/ARMFWJVBYKZTLCZGJNZLBJ3UCJXZXANCNFSM4H5X6J5A. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

I downgraded my plexapi package , but nothing changed.

Did you need to do anything else? Do you perhaps have to tell the plex server with API version you are using for it to cooperate?

@sourcecodemage
Copy link

I rewrote it just enough to copy playlists to other users. Give me a couple of days to clean my version and I'll share the github link.
The main part is the Playlist objects copytouser function. https://python-plexapi.readthedocs.io/en/latest/modules/playlist.html

@alicecantsleep
Copy link

@majorgear any luck getting that updated script finished up? I had this working for years, but stopped using playlists, so I stopped using the script. Fast forward to now, I want to sync up a little x-mas playlist but plex has updated it's api and my version no longer works.

I am at a loss on how to fix this one. I see what you are directing us to in the plexdocs but I'm just too newbie to python to figure it out. I tried a few variations, but find myself unable to get my playlists to sync.

The script will run and it will act like it syncs, but nothing ever shows up on the user accounts.

@brettwatson77
Copy link

brettwatson77 commented Dec 8, 2021 via email

@meisnate12
Copy link

if youre using the newest version of PlexAPI you probably need to change user_plex.createPlaylist(playlist, playlist_items) to user_plex.createPlaylist(playlist, items=playlist_items) the items need to be specified as they no longer just the second argument

@mikelauskas
Copy link

@meisnate12 that would be great if you updated your github repo. I'm really interested in using your library to share my playlists with my managed users.
THX

@juan11perez
Copy link

@meisnate12
Thank you for the tip. With that correction the script worked perfect for me.

@meisnate12
Copy link

@meisnate12 that would be great if you updated your github repo. I'm really interested in using your library to share my playlists with my managed users. THX

not really sure what any of my repos have to do with this file.

@meisnate12
Copy link

@meisnate12 Thank you for the tip. With that correction the script worked perfect for me.

no problem glad it worked for you

@influx3k
Copy link

influx3k commented May 6, 2023

I'm getting the following error when running the script:

User 'FirstName LastName' not found in shared users. Skipping.

However, that user is one of my shared users. Not sure why I'm getting that error. Also, that user is the only one that has their first and last name set in their account, in addition to their username. I'm wondering if that has something to do with it; the error is showing the first and last name in the output, instead of their username like all the others.

@pdschell
Copy link

pdschell commented Oct 5, 2023

I got this working on my synology nas on plexapi 4.15.4. awesome job man. cheers!

@tunerooster
Copy link

tunerooster commented Apr 20, 2024

When I run this, it syncs the playlist to my managed user, but it seems to hang the server. Immediately after running, the Movies and TV pages say "Your home screen is empty" or "Not Authorized". Then when I immediately restart the plex server, everything is as it should be, sync'd playlists and all. Has anyone experienced anything like this, and is there a fix?

And it's not just the user, it's all users, including the admin user, which get empty library screens.

I am running PMS version: v1.40.2.8351-9938371be

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment