Created
January 28, 2026 01:15
-
-
Save EldonMcGuinness/390b90cca7e634d8dfb7ca5186d678f2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 python3 | |
| ####################################### | |
| # This python script should be run | |
| # as a cron job every 15 minutes to | |
| # cache the next episode of a currently | |
| # playing TV show. | |
| ######################################## | |
| import requests | |
| import os | |
| import psutil | |
| from plexapi.server import PlexServer, CONFIG | |
| from plexapi.exceptions import NotFound | |
| from plexapi.video import Episode | |
| PLEX_URL = 'http://127.0.0.1:32400' | |
| PLEX_TOKEN = 'YOUR TOKEN HERE' | |
| RCLONE_CONFIG='/usr/local/etc/rclone.conf' | |
| MOUNT_TRANSLATE=['/tv', '/mnt/media-cache/tv'] | |
| if not PLEX_URL: | |
| PLEX_URL = CONFIG.data['auth'].get('server_baseurl') | |
| if not PLEX_TOKEN: | |
| PLEX_TOKEN = CONFIG.data['auth'].get('server_token') | |
| plex = PlexServer(PLEX_URL, PLEX_TOKEN) | |
| currentlyPlaying = plex.sessions() | |
| def translateMount(file): | |
| file = file.replace(MOUNT_TRANSLATE[0], MOUNT_TRANSLATE[1]) | |
| print("TEST: " + file) | |
| return file | |
| for episode in currentlyPlaying: | |
| if isinstance(episode, Episode): | |
| show = episode.grandparentTitle | |
| seasonNumber = episode.parentIndex | |
| filename = episode.media[0].parts[0].file | |
| episodeNumber = episode.index | |
| episodeSection = episode.librarySectionTitle | |
| print("Show: " + show) | |
| print("Season: " + str(seasonNumber)) | |
| print("Ep Num: " + str(episodeNumber)) | |
| def nextEpisode(show, seasonNumber, episodeNumber): | |
| episodes = plex.library.section(episodeSection).get(show).episodes() | |
| try: | |
| index = next(i for i, ep in enumerate(episodes) if ep.seasonNumber == seasonNumber and ep.episodeNumber == episodeNumber) | |
| return episodes[index + 1] | |
| except StopIteration: | |
| raise NotFound | |
| except IndexError: | |
| # already last episode | |
| pass | |
| nextEp = nextEpisode(show, int(seasonNumber), int(episodeNumber)) | |
| try: | |
| fileToCache = translateMount(nextEp.media[0].parts[0].file) | |
| print("Next ep is " + fileToCache) | |
| startCache = 1 | |
| except: | |
| print("No file found to cache. Possibly last available episode?") | |
| startCache = 0 | |
| if startCache == 1 and fileToCache: | |
| for proc in psutil.process_iter(): | |
| if proc.name() in 'rclone': | |
| if proc.cmdline()[1] in 'md5sum': | |
| if proc.cmdline()[2] in fileToCache: | |
| print("File is already being cached: " + fileToCache) | |
| startCache = 0 | |
| if startCache == 1 and fileToCache: | |
| print("Starting cache of " + fileToCache) | |
| bashCommand = 'nohup rclone --config "' + RCLONE_CONFIG + '" md5sum "' + fileToCache + '" &' | |
| os.system(bashCommand) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment