Skip to content

Instantly share code, notes, and snippets.

@cfurrow
Forked from blacktwin/aired_today_playlist.py
Last active May 14, 2021 02:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cfurrow/8bb5cb079602eafb358c9ab062ba1ebb to your computer and use it in GitHub Desktop.
Save cfurrow/8bb5cb079602eafb358c9ab062ba1ebb 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
PLEX_URL="http://192.168.1.72:32400"
PLEX_TOKEN=abcdefg
"""
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. http://192.168.1.72:32400)
Make sure PLEX_TOKEN is set in your .env file (get from app.plex.tv and open your dev console while playing a video)
To build a playlist for a given date:
python aired_today_playlist.py '2019-02-10'
To build a playlist for today's date:
python aired_today_playlist.py
"""
import os, operator, time, sys, datetime
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)
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)
else:
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 ')
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
# print('Looking for videos that aired in month {} and day {}'.format(str(today.tm_mon), str(today.tm_mday))
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]
if len(aired_lst) > 0:
# Create Playlist
print('Adding {} videos to playlist.'.format(len(aired_lst)))
plex.createPlaylist(TODAY_PLAY_TITLE, play_lst)
else:
print('Nothing to add!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment