Skip to content

Instantly share code, notes, and snippets.

@JonnyWong16
Last active February 1, 2024 19:38
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/725edf03749a8e16f5e7e3ae776b544b to your computer and use it in GitHub Desktop.
Save JonnyWong16/725edf03749a8e16f5e7e3ae776b544b to your computer and use it in GitHub Desktop.
Creates a Plex 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 playlist with random episodes from multiple TV shows
while keeping the episodes in order for each show.
Author: /u/SwiftPanda16
Requires: plexapi
'''
import random
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'
# ## CODE BELOW ##
# https://stackoverflow.com/a/10645233
def interleave(*args):
iters = sum(([iter(arg)] * len(arg) for arg in args), [])
random.shuffle(iters)
return map(next, iters)
def main():
plex = PlexServer(PLEX_URL, PLEX_TOKEN)
# Get tv shows library
tvshows = plex.library.section(LIBRARY)
# Get all episodes for each show
episodes = [tvshows.get(title).episodes() for title in SHOWS]
# Interleave episodes in random show order
shuffled_episodes = list(interleave(*episodes))
# Check for existing playlist and delete it
try:
plex.playlist(PLAYLIST_TITLE).delete()
except NotFound:
pass
# Create a new playlist
plex.createPlaylist(PLAYLIST_TITLE, items=shuffled_episodes)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment