-
-
Save brimur/c270deaded9e9cdcb764f163c0565311 to your computer and use it in GitHub Desktop.
####################################### | |
# 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 = '' | |
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() | |
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 = 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 md5sum "' + fileToCache + '" &' | |
os.system(bashCommand) |
Hey @brimur, have this weird issue.
I had a user who watched the last episode of season 4 of The Sopranos. The script queued up the next episode, S05E01, but it kept on doing it until the drive was completely out of space.
It just kept showing this over and over again in the log:
Show: The Sopranos
Season: 4
Ep Num: 13
Next ep is /home/xxxx/m/gcd/plex/TV Shows/The Sopranos (1999) [imdb-tt0141842]/Season 05/The.Sopranos.S05E01.1080p.BluRay.x264-XXX.mkv
Starting cache of /home/plex/m/gcd/plex/TV Shows/The Sopranos (1999) [imdb-tt0141842]/Season 05/The.Sopranos.S05E01.1080p.BluRay.x264-XXX.mkv
f4e1f220d2574e78d322d0696ef6ecc1 The.Sopranos.S05E01.1080p.BluRay.x264-XXX.mkv
The user never played the S05E01 for whatever reason, so I guess it never got out of the cache. It just kept adding it to the cache over and over, until the local cache completely filled out the harddrive.
Anything to do about this? I set my vfs-cache to full and to max out at 500GB - but with this script it just seems to add on beyond the limit set in the mount.
Hey @brimur, have this weird issue.
I had a user who watched the last episode of season 4 of The Sopranos. The script queued up the next episode, S05E01, but it kept on doing it until the drive was completely out of space.
It just kept showing this over and over again in the log:Show: The Sopranos Season: 4 Ep Num: 13 Next ep is /home/xxxx/m/gcd/plex/TV Shows/The Sopranos (1999) [imdb-tt0141842]/Season 05/The.Sopranos.S05E01.1080p.BluRay.x264-XXX.mkv Starting cache of /home/plex/m/gcd/plex/TV Shows/The Sopranos (1999) [imdb-tt0141842]/Season 05/The.Sopranos.S05E01.1080p.BluRay.x264-XXX.mkv f4e1f220d2574e78d322d0696ef6ecc1 The.Sopranos.S05E01.1080p.BluRay.x264-XXX.mkv
The user never played the S05E01 for whatever reason, so I guess it never got out of the cache. It just kept adding it to the cache over and over, until the local cache completely filled out the harddrive.
Anything to do about this? I set my vfs-cache to full and to max out at 500GB - but with this script it just seems to add on beyond the limit set in the mount.
Tbh Im not sure how that is even possible. The md5sum command just says "I need to read this file to create an md5 sum" it doesnt actually know if the file is local or in the cloud so it only triggers the file to be cached if its not local. So once the file caches the first time even if you ran the script 5 times it would always work on the cached file locally. The only way the file would cache over and over again is if there is something wrong with the caching mechanism itself. I dont use VFS-Cache personally so not sure.
How nextEp is calculated doesn't work when shuffle is used; nextEp would be better grabbing the next episode from the playlist.
How nextEp is calculated doesn't work when shuffle is used; nextEp would be better grabbing the next episode from the playlist.
Do you know the commands for that? I've been using Plex for years and never used shuffle or a playlist
I believe it can be done using: https://python-plexapi.readthedocs.io/en/latest/modules/playqueue.html
Getting this errorDo you have the line "from plexapi.video import Episode". Looks like its not recognising the Episode object for some reason. I have not seen that issue and no one else has reported it so not sure what could be causing it
Fixed. My plexapi module was outdated. Upgraded to 4.7 all good
Does this support more than one library like TV shows and Kid TV Shows?
Does this support more than one library like TV shows and Kid TV Shows?
I only have the one library but I updated the code to accommodate multiple TV libraries. I dont have that so if you do please confirm if it works. Thanks
It’s not the pull the issue it’s the screenshot before the CPU. This computer is a i5-9600K. I think whatever that error is it needs code to skip that file.
Anyway to add code that would skip the process when it's the last episode in the season?
Anyway to add code that would skip the process when it's the last episode in the season?
Can you elaborate? I'm not following, do you mean skip getting the first episode of the next season? Why should it skip getting that? Even if there is no episode available it will just fail but nothing else will happen since there is no file to cache. In any case I updated the code so that there needs to be a file to cache before attempting to cache
No. I’m saying that if the show is the absolute last show available in the series. The script gets stuck because there’s nothing telling it to stop looking the way that we have it running in Docker on a project I’m using.
No. I’m saying that if the show is the absolute last show available in the series. The script gets stuck because there’s nothing telling it to stop looking the way that we have it running in Docker on a project I’m using.
I'm assuming by show you mean episode? Ok, not sure what you meant by script gets stuck but I have added some code to check if there is an episode to cache first.
Ok I’ll try it out
what the point of caching next episode?
what the point of caching next episode?
It's mainly for people with a cloud drive and slow internet connections or lots of users. I have another script that caches what is on deck. So when someone watches an episode it is always from the local disk and not streaming from the cloud. If you had 10 users all watching the same show and no cache it would download it from the cloud each time.
Getting this error