Plex Auto-Delete Script

I’m not sure how this would be fringe if Plex requires it for a show to stay on deck.

What is episodes supposed to represent when watched is set? It’s not the number of episodes that will be kept, nor is it the number of unwatched episodes that will be kept, nor is it the number of watched episodes that will be kept (which is what I’d like it to be).

Plex should only show unwatched shows in the on deck area. Episodes is supposed to represent the minimum number of watched and unwatched episodes to keep that are less than the maximum days old. From my usage it seems to be working.

And by fringe I meant it would be a feature specifically for you, that could cause confusion to other users. I know you have a particular use in mind, but I am just trying to keep the script in line with what makes sense to me.

But Plex won’t put an episode on the deck unless there’s a watched episode for that show. That’s the whole point.

Episodes being a minimum does fit what I’m seeing. It just seems less meaningful when you have unwatched episodes, and watched is also set.

I’m new to Plex, just set it up over a week ago. I have everything working but was dissatisfied to learn there is no delete option on my clients. This script is a great compromise however I can’t seem to get it to work, I can’t even get it to give me a token. What am I doing wrong? I keep getting HTTP Error 401: Unauthorized when I run it

I’m running Plex 0.9.12.18 on a FreeNAS box in a jail (FreeBSD)
I have media in Plex and several of them are marked as watched
I have installed Python 2.7 on the Plex Media Server and running the script from it.
I’m using the latest version of your script (even the development version gives the same error)
In the script for Host I have tried leaving it blank, local IP, and remote IP and I have my username and password entered. The rest of the settings I have left as default.

When running it with the --test option I get the following output:

{
“Host”: “”,
“Port”: “”,
“SectionList”: [],
“IgnoreSections”: [],
“LogFile”: “”,
“trigger_rescan”: false,
“Token”: “”,
“Username”: “xxxx”,
“Password”: “xxxx”,
“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_location”: “”,
“default_onDeck”: true,
“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”: {
“Profile 1”: {
“maxDays”: 30,
“episodes”: 3,
“location”: “/path/to/folder”,
“action”: “delete”,
“watched”: true,
“onDeck”: true,
“minDays”: 10,
“homeUsers”: “”
}
},
“Version”: 1.9
}
Error getting token, trying without…
HTTP Error 401: Unauthorized
HTTP Error 401: Unauthorized
HTTP Error 401: Unauthorized

                       Detected Settings

Host: http://127.0.0.1
Port: 32400
HTTP Error 401: Unauthorized
HTTP Error 401: Unauthorized
HTTP Error 401: Unauthorized
Section List Mode: User-defined
Operating on user-defined sections:



            Summary -- Script Completed Successfully

Total File Count 0
Kept Show Files 0
On Deck Files 0
Deleted Files 0
Moved Files 0
Copied Files 0
Flagged Files 0
Rescanned Sections



@choeschen Ya it looks like the script is unable to get a token for your server.

I’m not familiar with BSD and how jails work, but we’ll try to find what’s causing the problem.

A few questions, is the jail connected to the internet, because the script can only get the token from plex.tv, just want to make sure there’s no weird firewall rules in place.

I also added an option on the development branch with a --debug flag, that print error messages. Could you try using the latest development file with the --debug and --test flag and let me know what the error is?

A jail is basically a virtual server. Yes it has access to the internet and I have the port forward setup on my firewall that forwards port 32400 to my plex media server. I have enabled remote access and plex is reporting that my server is fully accessible from outside my network.

Here is the output from the latest development with the --debug flag:

Error getting Token: urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)
Error getting token, trying without…
Error requesting page: HTTP Error 401: Unauthorized
Error requesting page: HTTP Error 401: Unauthorized
Error requesting page: HTTP Error 401: Unauthorized

                       Detected Settings

Host: http://127.0.0.1
Port: 32400
Error requesting page: HTTP Error 401: Unauthorized
Error requesting page: HTTP Error 401: Unauthorized
Error requesting page: HTTP Error 401: Unauthorized
Section List Mode: User-defined
Operating on user-defined sections:



            Summary -- Script Completed Successfully

Total File Count 0
Kept Show Files 0
On Deck Files 0
Deleted Files 0
Moved Files 0
Copied Files 0
Flagged Files 0
Rescanned Sections



Ok it looks like the python on FreeBSD isn’t able to verify to make an SSL connection because it doesn’t trust the certificate. This seems to be a known issue with python 2.7 on FreeBSD because it looks for the certificate in the wrong place on FreeBSD systems.

This link seems site seems to offer a fix that has been shared on other sites. I don’t think it will cause a security issue, but don’t hold me to that
http://lists.freebsd.org/pipermail/freebsd-python/2015-January/007832.html

It seems to just create a system link to the place that python is expecting to find the SSL certificate. Hope that helps.

Awesome that fixed it!

Next question - is the home user option not working? Here are my basic settings and test runs:

default_episodes = 0
default_watched = True
default_onDeck = True
default_minDays = 0
default_maxDays = 60

When I change the setting default_homeUsers = ‘’ I get the following summary:

Total File Count 235
Kept Show Files 229
On Deck Files 1
Deleted Files 0
Moved Files 0
Copied Files 0
Flagged Files 6
Rescanned Sections

With the setting default_homeUsers = ‘choeschen’ I get the following summary:

Total File Count 235
Kept Show Files 235
On Deck Files 1
Deleted Files 0
Moved Files 0
Copied Files 0
Flagged Files 0
Rescanned Sections

When I go into Plex under my user (which is the main account user) I see 6 watched shows that should be deleted, that is what the first test run flagged. However when I specify my user it is not flagging anything. I am also noticing the same issue if I specify another one of my users, it will always flag nothing even though that user has watched shows that should be getting flagged.

I believe I have found the error

Line 512:
if not lastViewedAt:

I removed this line as it was preventing the calculation of days since last viewed and the function was always returning -1

Line 706:
episodes[key] = {‘season’: season_num, ‘episode’: episode_num, ‘title’: title, ‘view’: m[‘view’],

I changed m[‘view’] to compareDay

Now when I run the script it will correctly flag shows that have been watched by the specified user.

Not sure if I’m missing something, but I keep getting this error when I try to dump the config file.

****_eth0:PlexCleaner user$ python PlexCleaner.py --dump "config.conf" Saving settings to config.conf Traceback (most recent call last): File "PlexCleaner.py", line 778, in <module> dumpSettings(args.dump) File "PlexCleaner.py", line 298, in dumpSettings if 'End Preferences' in Settings['ShowPreferences']: NameError: global name 'Settings' is not defined

@choeschen Ok I think I fixed the homeUsers bug. The not lastViewedAt was supposed to be the same line as the next line. Can you try the latest master or development branch and see if it works with Home Users? Also thank you if you were the one who posted the pull request on Github.

@wschlotter Ya sorry about that, there was a bug. It should be fixed now. I would copy your settings from your current script to a temp file and then get the latest version from github. If you used git checkout, then you may need to run a git stash on the PlexCleaner directory before you are able to do a pull.

Still having some issues. Willing to admit it’s me, but any help is appreciated.

Tried loading config file with sudo python PlexCleaner.py --config "Cleaner.conf" and got

Traceback (most recent call last): File "PlexCleaner.py", line 792, in <module> options = json.loads(opt_string) File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads return _default_decoder.decode(s) File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 366, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 382, in raw_decode obj, end = self.scan_once(s, idx) ValueError: Expecting property name: line 1 column 1383 (char 1382)

Thanks for accepting my pull. The new updated script is working fine for me now.

Another question. I haven’t looked much into the Plex API. Is there a way to find out if a video is in progress? I would love to add an option to have it auto delete videos that are still in progress after x days.

Reason for that is that some TV show recordings I have are not correctly marked as watched by Plex because they have credits and other stuff at the end.

@wschlotter It looks like there is something wrong with the formatting of the config file. Did you edit the file after you did the dump? You can PM me the config file and I can take a look. Just rename it to Cleaner.txt and remove your username, password, or tokens. Currently formatting is done with json which is really picky. I’m looking to use a different style that will hopefully be easier.

@pfluger I don’t see anything in the API that gives partial watch status, but you can look. You may have to manually mark some of those shows as watched.

Downloaded the latest version in the master branch and it is working with homeUsers. Thanks for your work making this script.

I posted another pull request.

I have added a new setting progressAsWatched. If set to anything > 0, all videos that have been viewed more than ‘progressAsWatched’ percent of their total duration will be considered watched. For example, when set to 75, any video that has the view progress bar at more than 75% of its total duration will be considered watched.

I though maybe you want to add this to the master branch if anyone else might find this useful. I am currently using this to delete certain shows even when not marked as watched, but with progress of > 75% (My wife always forgets to fast forward past the credits so Plex can mark as watched :wink: )

Need a little help with shared user settings.

I have 2 people who I have invited to share my server, but they are not PlexPass users. Can I still manage the deletion of content based on their watch history as well? The log keeps telling me I need their ‘Token’.

@wschlotter Only users that are in a PlexHome can be handled together in the script. To
manage Shared Users you would need to run the script with their login
information or token. This would not share watch status information.

For my shared users I usually leave things on my server for a few extra
weeks after I have watched them. And just keep a profile for shows they
only watch that just keeps the three latest episodes.

@d.g. said:
But Plex won’t put an episode on the deck unless there’s a watched episode for that show. That’s the whole point.

Episodes being a minimum does fit what I’m seeing. It just seems less meaningful when you have unwatched episodes, and watched is also set.

I’d like to echo this one – it is NOT a fringe request. This is fundamental to making this delete script truly useful for many of us.

For those of us who are looking for an auto-delete script such as yours (thank you for your hard work on this for the community!), as well as a WORKING “On Deck” list, the functionality described above is essential.

Yes, we can workaround this partially by using default_minDays, but that’s not useful if you sometimes go several weeks without watching a particular series. It’s not really a good solution to this.

What would be ideal, is a default_watched_keep setting that will keep that number of watched episodes at a minimum. Setting default_watched_keep=1 should do the trick for those of us who want a working On Deck list.

Please consider adding this functionality…not having it creates a substantial user experience problem in some people’s households who rely on a working On Deck and who very much want to use a cleanup script.

Thanks!