Plex Auto-Delete Script

@WPG Just so I understand the use, you basically want to be able to save a previous episode, so if you take a break in a show, you could go back and watch the previous episodes for context? What do you mean by a working “On Deck” list?

The only reason I have been resistant to implementing the feature is because it doesn’t work well with the current program flow and I didn’t see a good way of implementing it. I’ll keep thinking about it.

The desire isn’t to go back and watch the previous episode for context, it’s that we have an unwatched episode which is currently on deck and we want to keep it on deck until we decide to watch it.

If we delete all watched episodes for that series then Plex takes it off the deck! We have to keep at least one watched episode around for a series to retain it’s status as being ‘on deck’.

Consider this small patch which alters the flow, but shouldn’t change the current functionality:

$ diff PlexCleaner.py PlexCleaner-patch.py 
717c717
<     count = 0
---
>     count = 1
719c719
<     for key in sorted(episodes):
---
>     for key in sorted(episodes, reverse=True):
730c730
<         if ((len(episodes) - count) > show_settings['episodes']) or \
---
>         if (count > show_settings['episodes']) or \

The change here is that we’ve reversed the order in which we process the episodes. The rest of the logic still works because it was only examining one episode at a time. So now it’s trival to ‘count the number of watched episodes we’ve kept so far’ or ‘keep track of if the next episode in the series (the one we just processed) is on deck’. Yes?

[Edit: Removed second patch since it had a minor bug, and it’s irrelevant now anyway]

Alright, that makes more sense to me. So if we want to preserve the function of On Deck, how about we check if the next show is On Deck, before we delete a show. I changed the episodes to a list so that’s easy to do with the index.

if show_settings['onDeck']: checkDeck = onDeck or (k+1< len(episodes) and CheckOnDeck(episodes[k+1]['media_id'])
If checkDeck is true, then the show is kept, which it will be if the next episode is onDeck. You just have to enable onDeck for that show/profile or by default. Does that work?

I like the episodes being printed out in ascending order.

Sounds good, and much less to change if you’ve already moved to a list.
The only thing I can think of is that might mess with the onDeckCount stats?

Well I think it works because the OnDeck stat is the number of files we didn’t delete due to being on deck, so the double count makes sense. If anyone thinks its confusing I’ll change it.

Looking forward to testing this! :slight_smile:

just started using the script…but i can’t seem to get it to move files as i want to. it always just flags them.

Im trying to do things with the profile function. I defined a profile in the config file and added the one movie to the corresponding collection. But it won’t move them.

What am I doing wrong?

Here is the log file.

INFO:root:---------------------------------------------------------------------------- INFO:root: Detected Settings INFO:root:---------------------------------------------------------------------------- INFO:root:Host: http://localhost INFO:root:Port: 32400 INFO:root:Operating on sections: 2 INFO:root:Skipping Sections: 1,3,4 INFO:root: INFO:root:--------- Section 2: Movies ----------------------------------- INFO:root:He Never Died | Viewed: 0 | Days Since Viewed: 0 | On Deck: True INFO:root:[Keeping] /home/brain/plex.media/movies/he.never.died.(2015)/he.never.died.2015.mkv INFO:root: INFO:root:Sicario | Viewed: 1 | Days Since Viewed: 0 | On Deck: False INFO:root:**[FLAGGED] /home/brain/plex.media/movies/sicario.(2015)/sicario.2015.mkv INFO:root: INFO:root:Sleeping with Other People | Viewed: 0 | Days Since Viewed: 0 | On Deck: False INFO:root:[Keeping] /home/brain/plex.media/movies/sleeping.with.other.people.(2015)/sleeping.with.other.people.2015.mkv INFO:root: INFO:root:The Walk | Viewed: 0 | Days Since Viewed: 0 | On Deck: True INFO:root:[Keeping] /home/brain/plex.media/movies/the.walk.(2015)/the.walk.2015.mkv INFO:root: INFO:root: INFO:root:---------------------------------------------------------------------------- INFO:root:---------------------------------------------------------------------------- INFO:root: Summary -- Script Completed Successfully INFO:root:---------------------------------------------------------------------------- INFO:root: INFO:root: Total File Count 4 INFO:root: Kept Show Files 3 INFO:root: On Deck Files 2 INFO:root: Deleted Files 0 INFO:root: Moved Files 0 INFO:root: Copied Files 0 INFO:root: Flagged Files 1 INFO:root: Rescanned Sections INFO:root: INFO:root:---------------------------------------------------------------------------- INFO:root:----------------------------------------------------------------------------

And here is my config:

{ "Host": "localhost", "Port": "32400", "SectionList": [2], "IgnoreSections": [1,3,4], "LogFile": "/home/brain/Plex-Cleaner/PlexCleaner.log", "trigger_rescan": true, "Token": "xxx", "Username": "", "Password": "", "Shared": false, "DeviceName": "", "RemoteMount": "", "LocalMount": "", "plex_delete": false, "similar_files": true, "cleanup_movie_folders": true, "minimum_folder_size": 30, "default_episodes": 0, "default_minDays": 0, "default_maxDays": 60, "default_action": "move", "default_watched": true, "default_progressAsWatched": 0, "default_location": "", "default_onDeck": false, "default_homeUsers": "", "ShowPreferences": { "Show 1": { "maxDays": 30, "episodes": 3, "location": "/path/to/folder", "action": "delete", "watched": true, "onDeck": true, "minDays": 10, "homeUsers": "Bob,Joe,Will" }, "Show 2": { "onDeck": false, "maxDays": 30, "minDays": 10, "episodes": 0, "location": "/path/to/folder", "action": "delete", "watched": false }, "Show 3": { "action": "keep" }, "Show Preferences": {} }, "MoviePreferences": { "action": "flag", "location": "", "minDays": 0, "onDeck": true, "watched": true }, "Profiles": { "ArchiveMovie": { "location": "/home/brain/to.archive/movies", "action": "move", "watched": true, "onDeck": false, "minDays": 0, "homeUsers": "brain87" } }, "Version": 1.91 }

Can anybody help me please?

@brain87 Sorry I didn’t mention it earlier, but Profiles are currently only set up to work with TV Shows. I will look into it and probably add in profiles to Movies as well soon.

@ngovil21 said:
Well I think it works because the OnDeck stat is the number of files we didn’t delete due to being on deck, so the double count makes sense. If anyone thinks its confusing I’ll change it.

Looking forward to testing this as well. Is there an ETA?

Thanks again!

@WPG The fix should already be there. Could you test the latest version on Github?

@ngovil21 said:
@brain87 Sorry I didn’t mention it earlier, but Profiles are currently only set up to work with TV Shows. I will look into it and probably add in profiles to Movies as well soon.

too bad. would love profiles for movies…looking forward to it.

@ngovil21 said:
@WPG The fix should already be there. Could you test the latest version on Github?

Will do - do I need to adjust any settings in the config?

@WPG said:

@ngovil21 said:
@WPG The fix should already be there. Could you test the latest version on Github?

Will do - do I need to adjust any settings in the config?

No Settings need to be changed. OnDeck needs to be enabled for the shows.

I would really like to use this script but I have no python experience at all.

I have installed latest python on my windows machine.

But how do I run these commands - I just get syntax errors.

I just want to run the script manually.

Ok so I have been trying to get this to work and I believe it should. I am running ubuntu 13.04 with Plex 0.9.14.6 and I keep getting the following error. I will post my config file and the error below, Any help would be appreciated

Error

matt@AkersServer:~/Plex-Cleaner-master$ python PlexCleaner.py --test
Loading config file: /home/matt/Plex-Cleaner-master/Cleaner.conf
Traceback (most recent call last):
File “PlexCleaner.py”, line 804, in
options = json.loads(opt_string)
File “/usr/lib/python2.7/json/init.py”, line 338, in loads
return _default_decoder.decode(s)
File “/usr/lib/python2.7/json/decoder.py”, line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File “/usr/lib/python2.7/json/decoder.py”, line 384, in raw_decode
raise ValueError(“No JSON object could be decoded”)
ValueError: No JSON object could be decoded

Here is the contents of my Cleaner.conf

{
“Host”: “”,
“Port”: “32400”,
“SectionList”: [11],
“IgnoreSections”: [],
“LogFile”: “log.log”,
“trigger_rescan”: true,
“Token”: “”,
“Username”: “USERNAME_FOR_PLEX.TV_ENETER_HERE”,
“Password”: “password_was_enetered_here”,
“Shared”: false,
“DeviceName”: “”,
“RemoteMount”: “”,
“LocalMount”: “”,
“plex_delete”: false,
“similar_files”: true,
“cleanup_movie_folders”: false,
“minimum_folder_size”: 30,
“default_episodes”: 0,
“default_minDays”: 0,
“default_maxDays”: 60,
“default_action”: “flag”,
“default_watched”: true,
“default_progressAsWatched”: 0,
“default_location”: “”,
“default_onDeck”: true,
“default_homeUsers”: “all”,
“ShowPreferences”: {
“Show 1”: {
“maxDays”: 30,
“action”: “delete”,
“onDeck”: true,
“minDays”: 10,
“location”: “/path/to/folder”,
“watched”: true,
“episodes”: 3
},
“Show 2”: {
“maxDays”: 30,
“action”: “delete”,
“onDeck”: false,
“minDays”: 10,
“location”: “/path/to/folder”,
“watched”: false,
“episodes”: 0
},
“Show 3”: {
“action”: “keep”
}
},
“MoviePreferences”: {
“action”: “flag”,
“location”: “”,
“minDays”: 0,
“onDeck”: true,
“watched”: true
},
“Profiles”: {
“Profile 1”: {
“episodes”: 3,
“action”: “delete”,
“onDeck”: true,
“minDays”: 10,
“location”: “/path/to/folder”,
“watched”: true,
“maxDays”: 30,
“homeUsers”: “”
}
},
“Version”: 1.91
}

@extreme12 What syntax errors are you getting?

@AkersHouse Could you PM your config file? Remove any personal info and change the extension to .txt when you send and I’ll take a look. The config file, json, has a very specific format and anything that is off will cause an error. If you are on windows, I would suggest not using notepad to edit the config file as it can mess with the formatting.

My Config File is added here in case anyone else wants to look and to allow access to it easy. Personal Info was removed and extension changed to .txt

@AkersHouse said:
My Config File is added here in case anyone else wants to look and to allow access to it easy. Personal Info was removed and extension changed to .txt

Ya the text editor you used is causing the problem. Converting the file back to UTF-8 fixed the issue. Could you start again from the default Settings.conf.default from GitHub and use a different text editor to change the settings and make sure the encoding is set to UTF-8. I would recommend notepad++ or Atom.

Thank you so much, Changing to Notepad++ was the key, Gedit and Nano just screwed it up for me I guess.

No problem. Nano, should have been fine, the encoding must have gotten messed up somewhere.

The json module is really annoying, and I’ve been looking at ways to change without creating using an external dependency. I have a custom config file parser that I made, but I haven’t tested it enough to see if it will be easier to use, or just create more problems.

If anyone would like to test a different config file system, that will be closer to yaml (but much more basic), let me know. I’m trying to avoid having users install pyyaml externally. Or if any scripters would like to offer feedback. It is under the branch development-config.