Skip to content

Instantly share code, notes, and snippets.

@willstare
Last active June 30, 2018 00:00
Show Gist options
  • Save willstare/0d0c55daf992a8dd3cf9ffed30d8d119 to your computer and use it in GitHub Desktop.
Save willstare/0d0c55daf992a8dd3cf9ffed30d8d119 to your computer and use it in GitHub Desktop.
podcastPlaylistGen.py
"""
Create a Plex Playlist with the podcasts that were added today, sorted by oldest first.
If today's playlist exists, we will delete it and create it again to add potential new podcasts.
"""
import operator
import time
from plexapi.server import PlexServer
import requests
import datetime
"""
Finding the Token
Finding a temporary token is pretty simple:
Plex Account in Plex Web App
Browse to a library item and Investigate Media Information and Formats for it
Look in the URL and find the token as the X-Plex-Token value
"""
PLEX_URL = 'http://x.x.x.x:32400'
PLEX_TOKEN = ''
LIBRARY_NAME = ['Podcasts'] # Your podcast library name
today = datetime.datetime.now().date()
TODAY_PLAY_TITLE = '{}-{}-{} Podcasts Added Today'.format(today.year, today.strftime('%m'), today.strftime('%d'))
plex = PlexServer(PLEX_URL, PLEX_TOKEN)
def update_library():
print "Updating library before checking for new podcasts. Please wait 30 seconds."
for library in LIBRARY_NAME:
plex.library.section(library).update()
time.sleep(30)
def remove_old():
# Remove today's playlist if it exists.
for playlist in plex.playlists():
if playlist.title.endswith('Podcasts Added Today') and playlist.title != TODAY_PLAY_TITLE:
print "There are old playlists here, moving on"
elif playlist.title == TODAY_PLAY_TITLE:
playlist.delete()
print('{} already exists, deleting to create a fresh playlist.'.format(TODAY_PLAY_TITLE))
def get_all_content(LIBRARY_NAME):
artist_lst = []
# Get all podcasts from the Podcast Library
print "Retrieving all podcasts from podcast library"
for library in LIBRARY_NAME:
for artists in plex.library.section(library).all():
for artist in artists:
artist_lst += artist.tracks()
else:
pass
return artist_lst
def find_air_dates(artist_lst):
print "Looking for podcasts added today"
# Find what podcasts were added today (year, month, day)
aired_lst = []
for track in artist_lst:
try:
ad_month = str(track.addedAt.month)
ad_day = str(track.addedAt.day)
ad_year = str(track.addedAt.year)
if ad_year == str(today.year) and ad_month == str(today.month) and ad_day == str(today.day):
aired_lst += [[track] + [str(track.addedAt)]]
except Exception as e:
# print(e)
pass
# Sort by added time, 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]
return play_lst
remove_old()
update_library()
play_lst = find_air_dates(get_all_content(LIBRARY_NAME))
# Create Playlist
if play_lst:
print "Creating podcast for today"
plex.createPlaylist(TODAY_PLAY_TITLE, play_lst)
else:
print('Found no podcasts added')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment