Skip to content

Instantly share code, notes, and snippets.

@cfurrow
Last active February 19, 2023 16:23
Show Gist options
  • Save cfurrow/13a062359bd83ac17a38f8c4fcd3bab2 to your computer and use it in GitHub Desktop.
Save cfurrow/13a062359bd83ac17a38f8c4fcd3bab2 to your computer and use it in GitHub Desktop.
Create a "Saturday Morning Cartoons" Plex playlist
PLEX_URL="http://192.168.1.5:32400"
PLEX_TOKEN=abc123
"""
Create a "Saturday Morning Cartoons" playlist from your Plex TV Show collections
named "80s Cartoons" and "90s Cartoons".
Update the values in .env to match your server. Google details on how to find
your X-Plex-Token value and put that in the .env file for PLEX_TOKEN
Then you can run this to create or recreate your playlist:
python cartoon_playlist.py
"""
import os, operator, time, sys, datetime, re
import requests
from dotenv import load_dotenv
from plexapi.server import PlexServer
load_dotenv(verbose=True)
baseurl = os.environ['PLEX_URL']
token = os.environ['PLEX_TOKEN']
plex = PlexServer(baseurl, token)
CARTOON_PLAYLIST_TITLE = 'Saturday Morning Cartoons'
for playlist in plex.playlists():
if playlist.title == CARTOON_PLAYLIST_TITLE:
r = requests.delete('{}/playlists/{}?X-Plex-Token={}'.format(baseurl, CARTOON_PLAYLIST_TITLE, token))
print('{} already exists. Deleting it and will rebuild.'.format(CARTOON_PLAYLIST_TITLE))
tv_shows_section = plex.library.section('TV Shows')
episode_list = []
for collection in tv_shows_section.collection():
regexp = re.compile(r'(80s|90s) Cartoons')
if not regexp.search(collection.title):
break
print(collection)
for tv_show in collection.children:
print(tv_show)
for episode in tv_show.episodes():
if not episode.isWatched:
print(episode.title)
episode_list += episode
break
print('Adding {} cartoons to playlist.'.format(len(episode_list)))
plex.createPlaylist(CARTOON_PLAYLIST_TITLE, episode_list)
@cfurrow
Copy link
Author

cfurrow commented Feb 19, 2023

I've not used this script in a few years, but, it seems like it did not find anything to add.

Adding 0 cartoons to playlist.

Verify you have a Collection in your Plex library called "80s Cartoons" or "90s Cartoons". Otherwise the script won't know where to find cartoons to add toy our playlist.

It also seems like the underlying PlexAPI library has changed its syntax. Notice this error:

plexapi.exceptions.BadRequest: Must include items to add when creating new playlist.

Try changing this line:

plex.createPlaylist(CARTOON_PLAYLIST_TITLE, episode_list)

to this:

plex.createPlaylist(CARTOON_PLAYLIST_TITLE, items=episode_list)

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