Skip to content

Instantly share code, notes, and snippets.

@khaosx
Last active March 2, 2022 12:32
Show Gist options
  • Save khaosx/ebd588a80c12dea11512e6ca9873effa to your computer and use it in GitHub Desktop.
Save khaosx/ebd588a80c12dea11512e6ca9873effa to your computer and use it in GitHub Desktop.
Creates a playlist in Plex with the first unwatched episode of your current shows.
# This script creates a playlist that contains the next unwatched episode of any
# shows you define in a collection.
#
# Requires python 3
#
# Usage:
#
# In Plex, create a collection named "Current Shows". It should contain all the shows
# that you want considered for the new playlist.
#
# Update the variables to match your environment. Search Google for details on how to find
# your X-Plex-Token value.
#
# Then you can run this to create or recreate your playlist:
# python current_shows_playlist.py
# Update these:
PLEX_URL = 'http://my.plex.server:32400'
PLEX_TOKEN = 'my_plex_token'
# Leave all this alone!
import os, operator, time, sys, datetime, re
import requests
from plexapi.server import PlexServer
baseurl = PLEX_URL
token = PLEX_TOKEN
plex = PlexServer(baseurl, token)
MY_PLAYLIST = 'Current Shows'
for playlist in plex.playlists():
if playlist.title == MY_PLAYLIST:
print('{} already exists. Deleting and rebuilding.'.format(MY_PLAYLIST))
playlist.delete()
tv_shows_section = plex.library.section('TV')
episode_list = []
for collection in tv_shows_section.collection():
regexp = re.compile(r'Current Shows')
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 {} shows to playlist.'.format(len(episode_list)))
plex.createPlaylist(MY_PLAYLIST, episode_list)
@desatur8
Copy link

desatur8 commented Mar 1, 2022

Hi @khaosx , this is exactly what I am looking for! thanks for the work on this.

I tried running your script, but I have a question, if you dont mind assisting.

Firstly I received an error at line 37
tv_shows_section = plex.library.section('TV')
but realised my library is called "TV Shows", not TV, so edited that part of the script, but then got a error at line 40

for collection in tv_shows_section.collection():

the error is as follows:

Traceback (most recent call last):
  File "/home/current_shows_playlist.py", line 40, in <module>
    for collection in tv_shows_section.collection():
TypeError: collection() missing 1 required positional argument: 'title'

Have you maybe come across the error? can you point me in a direction?

Thanks again

@khaosx
Copy link
Author

khaosx commented Mar 1, 2022

@desatur8 Holy carp! I forgot this existed, which is funny, since I was just looking at trying this in Plex Meta Manager. So, you saved me a little time :)

So, the key to this script is that you have a collection named "Current Shows" defined in PLex, and that you have TV shows (not episodes) tagged in Plex as being part of that collection. You can change the collection name at line 41, but it must match.

So, for me, I have the following:

Collections:
-> "Current Shows"
---> Doctor Who (2005)
---> Archer (2009)
Playlists:
-> "Current Shows"
---> Doctor Who (2005) S01E01 - Rose
---> Archer (2009) S00E01 - Archersaurus

Verify that, and let me know what you find!

@desatur8
Copy link

desatur8 commented Mar 2, 2022

Thank you for replying!

Ok, this is what i have tried (please note, this is maybe my second ever time seeing python, so I am very noob here lol)

After changing token and url, I got the error for TV, that one i sorted out, changed line 37 from TV to TV Shows

But running it then, i get the following error

  File "/home/new_playlist.py", line 40, in <module>
    for collection in tv_shows_section.collection():
TypeError: collection() missing 1 required positional argument: 'title'

My Collection is also named Current Shows
I have manually created a playlist called current shows, and the script does then see it and delete it, so everything works up to there.
I have, in trying to test, and also trying to see how python and plex API works, played with this a bit.

I have uncommented everything and removed comments systematically, whilst doing print() after each statement.

when i print tv_shows_section I get the correct section
when i only run this for collection in tv_shows_section.collection(): i still get the title error, but if i add Current Shows in for collection in tv_shows_section.collection('Current Shows): and do a print of collection it shows the 3 test shows i added to the collection

when i then add regexp = re.compile(r'Current Shows') and do a print of regexp, the output is Current Shows (the word Current Shows), I am not sure if thats correct.

And thats where it was 1 am and i decided to go to bed lol

If you can maybe just glance over this and see if you can see where i made a mistake, i would appreciate it.

@khaosx

I played around a bit, and took some (lies! a lot!) inspiration from your script, and tried making my own! seems to be working

https://github.com/desatur8/PlexScripts

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