Skip to content

Instantly share code, notes, and snippets.

@brimur
Last active May 29, 2023 18:37
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save brimur/95277e75ca399d5d52b61e6aa192d1cd to your computer and use it in GitHub Desktop.
Save brimur/95277e75ca399d5d52b61e6aa192d1cd to your computer and use it in GitHub Desktop.
Cache unwatched On Deck items in Plex using rclone
#######################################
# This python script should be run
# as a cron job every 6 hours to
# cache On Deck episodes.
########################################
import os
import psutil
from subprocess import check_call
from itertools import chain
from plexapi.server import PlexServer
from plexapi.video import Episode
PLEX_URL = 'http://127.0.0.1:32400'
PLEX_TOKEN = ''
if __name__ == '__main__':
plex = PlexServer(PLEX_URL, PLEX_TOKEN)
files = []
for video in plex.library.onDeck():
if isinstance(video, Episode):
for media in video.media:
for part in media.parts:
files.append(part.file)
for count, fileToCache in enumerate(files):
if len(files) > 0:
print("Next ep is " + fileToCache)
startCache = 1
for proc in psutil.process_iter():
if proc.name() in 'rclone':
if proc.cmdline()[1] in 'md5sum':
if proc.cmdline()[4] in fileToCache:
print("File is already being cached: " + fileToCache)
startCache = 0
if startCache == 1:
print("Starting cache of " + fileToCache)
bashCommand = "nohup rclone md5sum --checkers 8 '" + fileToCache + "' &"
os.system(bashCommand)
@frank1x
Copy link

frank1x commented Jan 28, 2022

Hi, the script works very well with the main user. Is it possible to make it work with other users? Because it currently does not detect them.
Ty

@brimur
Copy link
Author

brimur commented Jan 28, 2022

@frank1x Each user has their own PLEX_TOKEN so if you want to use this for other users you will need to get their tokens and then run this for each. Easiest way to do it would be to create a copy of this file for each user, maybe add their name to the file name, and then schedule each file to run the same as the original one

@frank1x
Copy link

frank1x commented Jan 28, 2022

Works perfect, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment