Skip to content

Instantly share code, notes, and snippets.

@blacktwin
Created February 26, 2017 22:25
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save blacktwin/397f07724abebd1223ba6ea644ea1669 to your computer and use it in GitHub Desktop.
Save blacktwin/397f07724abebd1223ba6ea644ea1669 to your computer and use it in GitHub Desktop.
Create a Plex Playlist with what was aired on this today's month-day, sort by oldest first, using PlexAPI
"""
Create a Plex Playlist with what was aired on this today's month-day, sort by oldest first.
If Playlist from yesterday exists delete and create today's.
If today's Playlist exists exit.
"""
import operator, time
from plexapi.server import PlexServer
import requests
baseurl = 'http://localhost:32400'
token = 'xxxxxx'
plex = PlexServer(baseurl, token)
library_name = ['Movies', 'TV Shows'] # You library names
child_lst = []
aired_lst = []
today = time.gmtime(time.time())
TODAY_PLAY_TITLE = 'Aired Today {}-{}'.format(today.tm_mon, today.tm_mday)
# Remove old Aired Today Playlists
for playlist in plex.playlists():
if playlist.title == TODAY_PLAY_TITLE.startswith('Aired Today') and not TODAY_PLAY_TITLE:
r = requests.delete('{}/playlists/{}?X-Plex-Token={}'
.format(baseurl, TODAY_PLAY_TITLE, token))
print('Removing old Aired Today Playlists ')
print(r)
elif playlist.title == TODAY_PLAY_TITLE:
print('{} already exists. No need to make again.'.format(TODAY_PLAY_TITLE))
exit(0)
# Get all movies or episodes from LIBRARY_NAME
for library in library_name:
for child in plex.library.section(library).all():
if child.type == 'movie':
child_lst += [child]
elif child.type == 'show':
child_lst += child.episodes()
else:
pass
# Find what aired with today's month-day
for video in child_lst:
try:
if str(video.originallyAvailableAt.month) == str(today.tm_mon) \
and str(video.originallyAvailableAt.day) == str(today.tm_mday):
aired_lst += [[video] + [str(video.originallyAvailableAt)]]
except Exception as e:
pass
# Sort by original air date, oldest first
aired_lst = sorted(aired_lst, key=operator.itemgetter(1))
# Remove date used for sorting
play_lst = [x[0] for x in aired_lst]
# Create Playlist
plex.createPlaylist(TODAY_PLAY_TITLE, play_lst)
@adamcarter1
Copy link

This is cool! How do i implement it?

@cfurrow
Copy link

cfurrow commented Feb 17, 2019

@adamcarter1:

Be sure that you have python and pip installed, then you can run this:

# This installs the plex api so it can be used in the above script
pip install plexapi

# This downloads the above script. You can copy/paste the above code, or use wget to download the "raw" file from github
wget https://gist.githubusercontent.com/blacktwin/397f07724abebd1223ba6ea644ea1669/raw/72b2e633f12473f5e9a9e95be3906a19d7843c86/aired_today_playlist.py

Then, you need to edit the aired_today_playlist.py

Edit the baseurl to point to your server. Mine looks like this:
baseurl = 'http://192.168.1.72:32400'

And since I only wanted to get a list of all TV shows that aired today, I modified the following line
library_name = ['TV Shows'] # You library names

You'll need to get a valid plex token. The easiest way to do this, is open up your plex server via https://app.plex.tv, start playing some video from your library and then open your browser's developer tools, and look in the "network" tab. In there, you'll see a bunch of requests being made to your server that look like this:

https://some-plex-url-goes-here:32400/:/timeline?ratingKey=4681&key=%2Flibrary%2Fmetadata%2F4681&playbackTime=434808&playQueueItemID=XXXX&state=buffering&hasMDE=1&time=434000&duration=1213000&X-Plex-Product=Plex%20Web&X-Plex-Version=3.87.2&X-Plex-Client-Identifier=XXXX&X-Plex-Platform=Chrome&X-Plex-Platform-Version=72.0&X-Plex-Sync-Version=2&X-Plex-Device=OSX&X-Plex-Device-Name=Chrome&X-Plex-Device-Screen-Resolution=1536x782%2C1920x1080&X-Plex-Token=XXXXXXX&X-Plex-Language=en&X-Plex-Text-Format=plain&X-Plex-Session-Identifier=XXXXX

Find the plex token in there, by looking up the value for X-Plex-Token. Copy the value and add it into the python file.

token = 'XXXXXXX'

Now you're ready to run the script!

python aired_today_playlist.py

That's it!

@leftovernick
Copy link

Hello, trying to implement this, but running into an issue where it says there must be items when creating a playlist. I set a print statement for the lists to make sure they have content and this is what is returned:

[[, '1899-11-24 00:00:00'], [, '1899-11-24 00:00:00'], [, '1899-11-24 00:00:00'], [, '1948-11-24 00:00:00'], [, '1966-11-24 00:00:00'], [, '1973-11-24 00:00:00'], [, '1979-11-24 00:00:00'], [, '1979-11-24 00:00:00'], [, '1980-11-24 00:00:00'], [, '1984-11-24 00:00:00'], [, '1985-11-24 00:00:00'], [, '1986-11-24 00:00:00'], [, '1987-11-24 00:00:00'], [, '1988-11-24 00:00:00'], [, '1989-11-24 00:00:00'], [, '1989-11-24 00:00:00'], [, '1989-11-24 00:00:00'], [, '1989-11-24 00:00:00'], [, '1990-11-24 00:00:00'], [, '1990-11-24 00:00:00'], [, '1991-11-24 00:00:00'], [, '1992-11-24 00:00:00'], [, '1992-11-24 00:00:00'], [, '1993-11-24 00:00:00'], [, '1993-11-24 00:00:00'], [, '1993-11-24 00:00:00'], [, '1993-11-24 00:00:00'], [, '1995-11-24 00:00:00'], [, '1995-11-24 00:00:00'], [, '1995-11-24 00:00:00'], [, '1995-11-24 00:00:00'], [, '1995-11-24 00:00:00'], [sode:12321:Mobile-Suit-Gundam-W-s01e33>, '1995-11-24 00:00:00'], [, '1995-11-24 00:00:00'], [, '1996-11-24 00:00:00'], [, '1996-11-24 00:00:00'], [, '1996-11-24 00:00:00'], [, '1996-11-24 00:00:00'], [, '1997-11-24 00:00:00'], [, '1997-11-24 00:00:00'], [, '1997-11-24 00:00:00'], [, '1997-11-24 00:00:00'], [, '1997-11-24 00:00:00'], [, '1998-11-24 00:00:00'], [, '1998-11-24 00:00:00'], [, '1998-11-24 00:00:00'], [, '1998-11-24 00:00:00'], [, '1998-11-24 00:00:00'], [, '1999-11-24 00:00:00'], [, '1999-11-24 00:00:00'], [, '1999-11-24 00:00:00'], [, '1999-11-24 00:00:00'], [, '1999-11-24 00:00:00'], [, '1999-11-24 00:00:00'], [, '1999-11-24 00:00:00'], [, '2000-11-24 00:00:00'], [, '2000-11-24 00:00:00'], [, '2000-11-24 00:00:00'], [, '2000-11-24 00:00:00'], [, '2000-11-24 00:00:00'], [, '2001-11-24 00:00:00'], [, '2001-11-24 00:00:00'], [, '2001-11-24 00:00:00'], [, '2002-11-24 00:00:00'], [old-By-Ginger-s02e20>, '2002-11-24 00:00:00'], [, '2002-11-24 00:00:00'], [, '2002-11-24 00:00:00'], [, '2002-11-24 00:00:00'], [, '2002-11-24 00:00:00'], [, '2002-11-24 00:00:00'], [, '2002-11al-s01e07>, '2022-11-24 00:00:00'], [, '2022-11-24 00:00:00'], [, '2022-11-24 00:00:00'], [, '2022-11-24 00:00:00'], [, '2022-11-24 00:00:00'], [, '2022-11-24 00:00:00']]
[, , , , , , , , , , , , , , , , , , , , , , , , , , Dragon-Ball-Z-s07e14>, , , , , , , , , , , , , , , , , , , , , , , , , , rek:-Voyager-s06e09>, , , , , , , , , , , , , ]
Traceback (most recent call last):
File "/tmp/user.scripts/tmpScripts/android13AiredToday.py/script", line 65, in
plex.createPlaylist(TODAY_PLAY_TITLE, play_lst)
File "/usr/lib64/python3.9/site-packages/plexapi/server.py", line 548, in createPlaylist
return Playlist.create(
File "/usr/lib64/python3.9/site-packages/plexapi/playlist.py", line 439, in create
return cls._create(server, title, items)
File "/usr/lib64/python3.9/site-packages/plexapi/playlist.py", line 345, in _create
raise BadRequest('Must include items to add when creating new playlist.')
plexapi.exceptions.BadRequest: Must include items to add when creating new playlist.

@cfurrow
Copy link

cfurrow commented Nov 27, 2023

@leftovernick You may have better luck using this: https://github.com/cfurrow/plex-playlist-creators

I created a couple of Plex playlist creator scripts, and tested it out again today, and it worked fine.

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