Skip to content

Instantly share code, notes, and snippets.

@JonnyWong16
Created January 12, 2024 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonnyWong16/2fe6bf68c722fadca24240e65fd36002 to your computer and use it in GitHub Desktop.
Save JonnyWong16/2fe6bf68c722fadca24240e65fd36002 to your computer and use it in GitHub Desktop.
Creates a Plex smart playlist with random episodes from multiple TV shows while keeping the episodes in order for each show.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Description: Creates a Plex smart playlist with random episodes from multiple TV shows
while keeping the episodes in order for each show.
Author: /u/SwiftPanda16
Requires: plexapi
'''
from plexapi.exceptions import NotFound
from plexapi.server import PlexServer
# ## EDIT SETTINGS ##
PLEX_URL = 'http://localhost:32400'
PLEX_TOKEN = 'XXXXXXXXXXXXXXXXXXXX'
LIBRARY = 'TV Shows'
SHOWS = [
'Show Title 1',
'Show Title 2',
'Show Title 3',
]
PLAYLIST_TITLE = 'Shuffled Episodes'
UNWATCHED_ONLY = True # True for unwatched episodes only, False for all episodes
# ## CODE BELOW ##
def main():
plex = PlexServer(PLEX_URL, PLEX_TOKEN)
# Get tv shows library
tvshows = plex.library.section(LIBRARY)
# Sort by season and episode number, then randomize the shows
sort = [
'season.index:asc',
'episode.index:asc',
'show.random'
]
filters = {
'show.title': SHOWS,
}
if UNWATCHED_ONLY:
filters['episode.unwatched'] = True
# Check for existing playlist and update it otherwise create a new playlist
try:
plex.playlist(PLAYLIST_TITLE).updateFilters(sort=sort, filters=filters)
except NotFound:
tvshows.createPlaylist(
title=PLAYLIST_TITLE,
smart=True,
libtype='episode',
sort=sort,
filters=filters,
)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment