Skip to content

Instantly share code, notes, and snippets.



Forked from blacktwin/
Last active May 14, 2021
What would you like to do?
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.
Make sure PLEX_URL is set in your .env file (e.g.
Make sure PLEX_TOKEN is set in your .env file (get from and open your dev console while playing a video)
To build a playlist for a given date:
python '2019-02-10'
To build a playlist for today's date:
import os, operator, time, sys, datetime
import requests
from dotenv import load_dotenv
from plexapi.server import PlexServer
baseurl = os.environ['PLEX_URL']
token = os.environ['PLEX_TOKEN']
plex = PlexServer(baseurl, token)
library_name = ['Movies', 'TV Shows'] # You library names
child_lst = []
aired_lst = []
if len(sys.argv) > 1:
date_arg = datetime.datetime.strptime(sys.argv[1], '%Y-%m-%d')
date_in_seconds = time.mktime(date_arg.timetuple())
today = time.gmtime(date_in_seconds)
today = time.gmtime(time.time())
TODAY_PLAY_TITLE = 'Aired Today {}-{}'.format(today.tm_mon, today.tm_mday)
print('Building a playlist: {}'.format(TODAY_PLAY_TITLE))
# 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 ')
elif playlist.title == TODAY_PLAY_TITLE:
print('{} already exists. No need to make again.'.format(TODAY_PLAY_TITLE))
# 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()
# Find what aired with today's month-day
# print('Looking for videos that aired in month {} and day {}'.format(str(today.tm_mon), str(today.tm_mday))
for video in child_lst:
if str(video.originallyAvailableAt.month) == str(today.tm_mon) \
and str( == str(today.tm_mday):
aired_lst += [[video] + [str(video.originallyAvailableAt)]]
except Exception as e:
# 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]
if len(aired_lst) > 0:
# Create Playlist
print('Adding {} videos to playlist.'.format(len(aired_lst)))
plex.createPlaylist(TODAY_PLAY_TITLE, play_lst)
print('Nothing to add!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment