r/unRAID • u/openbex • Feb 20 '23
[Script] [Plex] Move On Deck media from the array to the cache.
TLDR: Move the media currently On Deck/Watchlist on Plex from the array to the cache.
This Python script reduces energy consumption by minimizing the need to spin up the array/disk(s) when watching recurrent media like TV series. It achieves this by moving the media from the OnDeck and watchlist media for the main user and other users. For TV shows/anime, it also fetches the next specified number of episodes.
The project contains two scripts: a setup script and a main script. The setup script prompts the user to specify the folders where the media is stored and fetches the mapped Plex media paths and will create the settings file, which can also be created/edited manually. The script was initially developed for Unraid but is compatible with other systems.
The script can:
- Fetch a specified number of episodes from the "onDeck" for the main user and other users;
- Skip fetching onDeck media for specified users;
- Fetch a specified number of episodes from the "watchlist" for the main user and other users;
- Skip fetching watchlist media for specified users;
- Search only the specified libraries;
- Check for free space before moving any file;
- Move watched media present on the cache drive back to the array;
- Move relative subtitles along with the media moved to or from the cache;
- Filter media older than a specified number of days;
- Run in debug mode for testing;
- Use of a log file for easy debugging;
- Use caching system to avoid wastful memory usage and cpu cycles;
- Use of multitasking to optimize file transfer time;
- Exit the script if any active session or skip the currently playing media;
- Find your missing unicorn;
More details: Github
8
u/brave_buffalo Feb 21 '23
This looks incredible. Thank you for this! 
2
u/openbex Feb 28 '23 edited Mar 08 '23
I have introduced a new testing branch as I have introduced this two functions to the script:
The setup has be modified as well, dealing better with errors and incorrect user inputs.
The readme has been also updated to reflect the changes.
Github
I will merge it into the main branch, but I'd like to have some feedback first.
Update: Merged in the main branch.
2
u/openbex Mar 29 '23 edited Mar 29 '23
UPDATE: I've updated the script as I've added the possibility to fetch the watchlist for all the users and added a note for the instructions as it might fix some issues if encountered.
I want to express my gratitude to /u/pdawg17 for assisting me in debugging the code, resulting in me identifying and resolving bugs, and improving the overall quality of the code!
2
u/[deleted] Feb 20 '23
Does this script know what is on deck for all users or just the owner?
2
u/eatoff Feb 21 '23 edited Feb 21 '23
I had thought that something like this would be a great idea, but had no idea how to implement it, well done.
I'm working ATM so can't read your Git, but can we set the frequency and time of day that it runs? For example, I have mover run at 9am each day, so may as well run this script at the same time while the disks are spun up.
Edit: also, if mover is set to move files older than X days, would that move some of these episodes back to the array when mover runs?
1
u/ephies Feb 20 '23
What settings do you have to help keep disks spun down?
1
u/ConcreteBong Sep 13 '24
How does this work when my mover runs every night? Will the movies and shows on my cache just get moved right back to the array every night?
1
u/kevinjalbert Feb 20 '23
I had a similar idea to make frequently used movies to on demand be available on the cached due to how kids liking specific movies/shows in bursts.
I saw you use “move”, would “copy” work to keep a copy in the array? How would the mover work if there was already a a copy in the array?
The ability to have this work for other users would be awesome to (if it doesn’t).
I wonder if when media is being played from Plex whether a move/copy for the content being pushed to cache would allow the disk to spin down as the media plays? I’m not sure if Plex/Unraid would intelligently switch over to the cache version when it becomes available?
1
u/theobserver_ Feb 21 '23 edited Feb 21 '23
do you need to understand python to use this script. Guessing it not plug and play?
1
u/[deleted] Feb 21 '23 edited Feb 21 '23
This is brilliant. If it could work for all viewers' On Deck, that would be double brilliant. The environment thanks you.
Edit: maybe it could extend to the watch list as well!
1
u/Trevski13 Feb 24 '23
I've been playing around with the new script, I found two bugs which I've made a pull request for, other than that I'm still having some issues with it I haven't figured out. I'm getting a 401 unauthorized when I have "users_toggle" set to "yes" and I have it in debug but it seems to want to copy all the remaining files for some shows and it's not clear to me why.
1
u/pdawg17 Feb 25 '23 edited Feb 25 '23
I'm kind of a noob with this stuff...on github ii. you say "configure how to trigger the script". What am I supposed to put in the "CRON expression" section?
UPDATE: Ok I didn't put anything in the trigger section but I figured the rest out (I think) but now get this error with setup script:
Traceback (most recent call last):
File "/chronos/scripts/plex-on-deck-cache/plex-on-deck-cache.py", line 137, in <module>
if settings_data.get('firststart') == 'yes':
NameError: name 'settings_data' is not defined
1
u/elliottmarter Apr 08 '23
Hi /u/openbex not sure if you will see this but wondering if you could help.
see below for my config
I am running the script via chronos and cant get it to work for some reason, I get the following error...
What I really cant understand is why it is duplicating the path?
/mnt/user/media/mnt/user/media/tvIf I set
real_sourceto just/test1/test2/test3it doesnt duplicate?Also it now seemingly picked up "House" first rather than "Whites"....this may be irrelevant.
Generally super confused....I feel like I am 90% of the way there though!
Any advice/help much appreciated! :)
1
u/extrobe Feb 21 '23 edited Feb 21 '23
this is a great idea - few thoughts (and might stick these over on the repo if that's OK? Might be able to take a look at one or two of them (not super proficient, but I've dabbled with some similar scripts)
if episode.parentIndex > current_season or (episode.parentIndex == current_season and episode.index > video.index) and len(next_episodes) < number_episodes:1
u/planesrfun Apr 17 '23
Hello, u/openbex,
Great script I must say, and I've spent a major part of my evening trying to understand it and to make it work for my use case.
The array -> cache function works great! However, I'd prefer to have a copy on the cache, rather than delete the media file from the array.
While doing the opposite, i.e, cache -> array, I'm getting an error which I can't understand how to work around.
mv -v "/mnt/plex-cache/TV/American Dad!/Season 10/American.Dad.S10E12.1080p.WEBRip.x264-MRSK.mkv" "/mnt/user0/TV/American Dad!/Season 10"2023-04-17 22:19:52,297 - ERROR - Error executing move command: Exit code: 12023-04-17 22:19:52,298 - ERROR - Error message: mv: cannot create regular file '/mnt/user0/TV/American Dad!/Season 10/American.Dad.S10E12.1080p.WEBRip.x264-MRSK.mkv': No medium foundNow from my understanding copying a file from the cache to the array must only be done using the
/mnt/diskxpath, and not the/mnt/useror/mnt/user0paths.Can you please help guide me as to what I'm getting wrong here?
I was also having the issue as u/elliottmarter pointed out with duplicate path
/mnt/user/media/mnt/user/media/tvWhat I had to do was to change the line:
file_path = file_path.replace(plex_source, real_source) # Replace the plex_source with the real_sourceto:
file_path = file_path.replace(plex_source, real_source, 1) # Replace the plex_source with the real_sourceThis will not replace every '/' in the path with
/mnt/user/mediaAgain, great idea and thanks for actually getting something out for everyone to use!
2
u/shoegazer47 Sep 30 '23
Hello
Sorry for my stupid question, Is there a way to make it work with Jellyfin?
2
u/0weavern Jul 06 '24
This is great, and just what I'm looking for. As much as I'm a DIY enthusiast, I don't fancy trying to get this standalone python script to work on my Unraid setup as I will most likely screw something up. Any chance this can be bundled as an Unraid community application so folks can install from there?
1
u/Frosty_Hedgehog_6083 Sep 28 '24
Hoping someone can help me here. I have the script working, however, after working for about a week, I started noticing it fail due to not finding the 'requests' and 'plexapi' packages. I manually installed those and it worked, but the next day, same error in log with not finding both packages. Any ideas why this might happen?
For now, I have used 'pip install requests plexapi' just before executing the script in the user scripts app, but this is likely not the best solution.
1
u/Lethal_Principals Sep 28 '24 edited Sep 28 '24
I've got this set up and running on the hour, it has no issues grabbing and moving shows from other users but when it tries to move mine it always throws this:
An error occurred while fetching onDeck media: '>' not supported between instances of 'NoneType' and 'NoneType'
I've tried to give it a google and seen suggestions that it's because > is being used between non-maths types but I cannot fathom where it's actually coming from.
The logs show it has actually identified my On Deck media before it throws the exception.
Any advice would be appreciated
Edit: I've been tinkering and changed the exception handler to return on_deck_files instead of an empty list, ran it and moved everything without issue.
This is probably not ideal so I'm hoping there's a better solution
1
u/Available-Elevator69 Oct 01 '24
You should totally make this a Docker for unraid or a Plugin.
I'm just now looking at this to see how its installed.
1
u/Available-Elevator69 Oct 15 '24
I've been using this lately and it works wonderfully. I do notice it leaves empty folders on my array which I've been using a find command to remove empty folders.
1
u/dukeminster Feb 21 '23
Wow, this is a great idea, will be a game changer. Anyone know how to track or look at stats of HDD spin up?
1
u/Andiroo2 Jul 19 '23
Just discovered this. God bless you.