Plex Auto-Delete Script

OK I'm finally posting my rewrite of the script. I rewrote the script from the ground up to be more cohesive instead of a lot of different things hacked together. The script will now navigate through the Plex API movies and shows instead of only looking at the Recently Added page. This means that episodes should no longer be missed if a large update is made to the library. It also allows more customization and control of individual shows. I will probably post a new thread with this new version, but I will post it for you guys to test first. I have included most of the updates people have requested in the thread so hopefully everyone is happy and we can prevent fragmentation.

Most of the new options in the script are described in the commented code,but I'll go over the basics. Any line that has a # in the front is a comment and should not be edited.

The general options are the same give your HOST IP, Port, SectionList, Log File. Also for any users with Plex Home and in general it is recommended to use a username/password or a Token for logging in. Token is the recommended way. To generate a Token first fillout the host, port, username and password fields. Then run the script with the --test parameter. A token will be generated form plex.tv and will be printed in the console and log file. You can then paste this token into the script and remove the username and password.

Newly added there is an option to translate a remote Plex server address to a locally mounted address. This will simply replace the beginning part of the file name with the path to the file on the local server. I would recommend running the script on the Plex Server itself, but if that's not possible then this is another option. This will require all of the Plex Media files to be accessible by the local computer.

Now there are default actions that will be used for any show. These default actions can be overridden for shows later. The default actions are similar to PlexAutoDelete and are described in the script.

New options are:

similar_files - this is similar to the delete_similar option, but now it will also move, copy, or delete similar named files depending on the action specified.

cleanup_movie folders - this will search through movie directories and delete folders that are smaller than minimum_folder_size in megabytes.

default_episodes - this will save a certain number of TV Shows in a series, useful if you want to keep a few episodes always available or have episodes to recap before the next season starts.

default_maxDays - this will override the default_episodes setting and delete files that have been stored for too long

There is a new ShowPreferences section. Examples settings are included in the script. It allows you to have custom settings on a per show basis. The show settings in ShowPreferences will override the default settings. You can specify how many episodes to keep, whether to only delete watched episodes, the minimum days to keep, the action to perform, etc. This allows complete customization on TV_Shows. There is not IgnoreShows anymore, but the same thing can be done by setting the action for a show to "keep". The ShowPreferences has a particular format, so it is recommended you copy paste one of the examples and carefully edit the line. Each line in ShowPreferences should end in a comma (,) or you will get format errors.

The script has been tested on Windows and Linux, on python2 and python3. Let me know if there are any issues.

PlexCleaner v1

https://www.dropbox.com/s/39ei1444lwrefut/PlexCleaner.py?dl=0

Hey ngovil21,

Thank you! This looks like a great rewrite, am digging the options and the way you have done the show lists and other stuff.

I have encountered an error when running this which I suspect is some unicode escape thing:

first not so serious error:

Cannot print message

[Keeping] /home/plex/movies/C/Caché (2005)/Cache.2005.720p.BluRay.x264-CiNEFiLE.mkv

more serious error:

Inside Amy Schumer - S03xE01 - Last F...able Day | Viewed: 0 | Days Since Last Viewed: 0

[Keeping] /home/plex/series/Inside Amy Schumer/Season 3/Inside Amy Schumer - S03E01 - Last F.able Day HDTV-720p.mkv

 

Traceback (most recent call last):

  File "./AutoDelete9.py", line 527, in

    checkShow(getURLX("http://" + Host + ":" + Port + show_key))

  File "./AutoDelete9.py", line 401, in checkShow

    episode_num = int(VideoNode.getAttribute('index'))                 #Video index refers to the epiosde number

ValueError: invalid literal for int() with base 10: ''

 

My python skills are too crap to fix this, I reckon someone with your abilities will find these rather quickly...

Ya the first error is a weird Unicode error that I haven't been able to figure out. Python is a little wonky in which it handles strings and the libraries have changed. I'm not going to fix it because it's more important to me to have compatibility with Python 2 and Python 3. I think it only affects printing out the name, but the script will still work.

The second error appears to be a show that is not numbered. Do you mind telling me what show you have after Inside Amy Schumer? The script searches alphabetically, so it should be the next one. I have updated the script to use other options other than the index if it is not available. Try it now.

https://www.dropbox.com/s/39ei1444lwrefut/PlexCleaner.py?dl=0

You should really delete the username and password from the script and change it!!!

Whoops thanks for the heads up!

OK because of my previous mess-up. I added the ability to save the settings to a config file. After configuring the new script, you can run the script with a --dump option to create a JSON file with all of your settings, like so:

python PlexCleaner.py --dump /path/to/config/file

The default location is .plexcleaner in the current directory if no argument is specified. You can then load the config file using the Config variable at the top of the script, or using the --load option:

python PlexCleaner.py --load /path/to/config/file

The config file can be more finicky so I wouldn't try to edit it too much manually.

Same link.

https://www.dropbox.com/s/39ei1444lwrefut/PlexCleaner.py?dl=0

Did you add special character files like for anime show? 

I know it works well with special character files in Python 2, but I'm not sure about Python 3. If you tell me the fix you made for special characters I can put it in.

Well i got mains modify for special characters but i don't know of how or what they modify on it. I just know it work. :D

Here where a post mains, it a little bit old but it work for me https://forums.plex.tv/topic/84822-plex-auto-delete-script/?p=792789

Ok well I looked at your Script and I don't see anything special in your old script, so try out my script and let me know if it works or tell me the name of a show that I should test out.

Ok well I looked at your Script and I don't see anything special in your old script, so try out my script and let me know if it works or tell me the name of a show that I should test out.

Will do. Tomorrow full try out. 

The second error appears to be a show that is not numbered. Do you mind telling me what show you have after Inside Amy Schumer? The script searches alphabetically, so it should be the next one. I have updated the script to use other options other than the index if it is not available. Try it now.

Hi, so you were right, the next show in my collection is Insomniac with Dave Attell. That show has one season that has season numbering (S01E01 etc) and 2 seasons that use the date format! Nice little corner case :)

Anyone played around with adding PushBullet for deleted files?

Trying out the script now, I've been looking for something like this :)

FYI, you should really think about setting up a GIT repo at GitHub. It would really make this project take off.

I second the github idea. It would help keep everything more organized and promote better contribution

Ya I actually did open up a Github repo last night just to simplify things for myself. I'm still new to Github and figuring out how it works.

Here's the link:

https://github.com/ngovil21/Plex-Cleaner

master channel will be for public releases, development will be unstable and just include whatever I happen to be working on.

Awesome! Thanks. I'll keep updates from there then. By the way, already have the script configured and running on cron. I love it! It was just what I was looking to do :) thanks for all the hard work at it!

Installed, configured and "croned"!

Having some problem though...

The episodes I watched yesterday are not moved (I use move instead of delete).

The logfile says:

24 - S02xE020 - Day 2: 3:00 A.M.-4:00 A.M. | Viewed: 1 | Days Since Last Viewed: 0
[Keeping] /media/tommy/TV och Film/TV/24/24 - S02E20 - Day 2 300 A.M.-400 A.M..mkv
 
but it claims that it is still On Deck:
  Total File Count      3227
  Kept Show Files       3227
  On Deck Files         4
 
When checking the On Deck in Plex, the episodes are not there.
 
If I run the old PlexAutoDelete script, the files are moved.
  Total File Count      82
  Kept Show Files       79
  On Deck Files         1
  Deleted Files         0
  Moved Files           3
 
Settings:
  "cleanup_movie_folders": false, 
  "default_action": "move", 
  "default_episodes": 0, 
  "default_location": "/home/tommy/Downloads/delete", 
  "default_maxDays": 60, 
  "default_minDays": 0, 
  "default_onDeck": true, 
  "default_watched": true, 
  "minimum_folder_size": 30, 
  "plex_delete": false, 
  "similar_files": true
 
Any clues?
 

Just to check if the show is showing up in the section onDeck could you add this code to the script and tell me if the episode is there?

On the latest version around line 605, after this line:

deck = getURLX("http://" + Host + ":" + Port + "/library/sections/" + Section + "/onDeck")

add these lines:

log("On Deck Shows")
log(deck.toprettyxml())

You can PM me the output if you can't figure it out. Thanks for testing the script.

I also pushed an update that changed how minDays works. Before it would only delete if minDays was greater than the Days Since Viewed, meaning if they were both 0, it wouldn't delete. Now it deletes if minDays is greater than or equal to.

No they are not in the "On Deck"

These are:

file="/media/tommy/TV och Film/TV/Friends/Friends - S05E11 - The One With All The Resolutions.mkv"

file="/media/tommy/TV och Film/TV/MasterChef Canada/Season 2/MasterChef Canada - S02E08 - Wedding on the Waves.mp4"

file="/media/tommy/TV och Film/TV/The Last Man on Earth/Season 1/The Last Man on Earth - S01E12 - The Tandyman Can - 720p.x264.AC3.mkv"

file="/media/tommy/TV och Film/TV/Homeland/Homeland - S01E12 - Marine One.mkv"

file="/media/tommy/TV och Film/TV/Death in Paradise/Season 3/Death in Paradise - S03E03 - Episode 3.avi"

Which are the episodes to come.

Homeland is marked as Watched but is correctly marked as On Deck. (probably seen >80%)

Btw,Your line 605 is my 588...

I have

ShowPreferences = {
  "Bosch":{"action":"keep"},   #This show will skipped
  "Fortitude":{"action":"keep"},   #This show will skipped
  "Death in Paradise":{"action":"keep"},   #This show will skipped
  "The Last Man on Earth":{"action":"keep"},   #This show will skipped
  "Broadchurch":{"action":"keep"},   #This show will skipped
  "End Preferences" : {} # Keep this line
}