Last active
February 1, 2024 19:38
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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