Plex Auto-Delete Script

I just checked the script on Windows machine on a remote Linux server and it appears to work. Please double check that your configuration is accurate.

Are you using a correct token that has authorization to use the server? Please try using your username and password to see if that works. If you would then like to use a token, then run the script with --test parameter and it will print the token it uses in the console or log file:

python.exe PlexAutoDelete.py --test

You can then copy the token and use it in the script and delete your myPlex information.

Thanks for the quick reply. I did what you asked, but it didn't return any tokens?

INFO:root:Running in Test Mode
INFO:root:----------------------------------------------------------------------------
INFO:root:                           Detected Settings
INFO:root:----------------------------------------------------------------------------
INFO:root:Host: xxx.xx.xxx.xx
INFO:root:Port: xxxx
INFO:root:Logged in as: username
INFO:root:Keep Mode: DISABLED
INFO:root:Show Count: 1
INFO:root:Number of Shows Detected For Keeping: 0
INFO:root:Shows to Keep: | (None Listed) | 
INFO:root:Delete: Disabled - Flagging Only
INFO:root:Delete OnDeck: No
INFO:root:Operating System: Linux 
INFO:root:Section List Mode: User-defined
INFO:root:Operating on user-defined sections: 1,2
INFO:root:
INFO:root:--------- Section 1: Movies -----------------------------------
INFO:root:
INFO:root:--------- Section 2: TV Shows -----------------------------------
INFO:root:
INFO:root:----------------------------------------------------------------------------
INFO:root:----------------------------------------------------------------------------
INFO:root:                Summary -- Script Completed Successfully
INFO:root:----------------------------------------------------------------------------
INFO:root:
INFO:root:  Total File Count      0
INFO:root:  Kept Show Files       0
INFO:root:  On Deck Files         0
INFO:root:  Deleted Files         0
INFO:root:  Moved Files           0
INFO:root:  Copied Files          0
INFO:root:  Flagged Files         0
INFO:root:  Rescanned Sections    
INFO:root:
INFO:root:----------------------------------------------------------------------------
INFO:root:----------------------------------------------------------------------------

I don't know if it matters, but I am in a home group where I am the head/admin of it. Also does the script take into account pin? I'm still trying to learn about the API, so I apologize if that is basic

I haven't tested the script with a pin but hopefully it won't be needed, because I don't know how to implement it.

It looks like DEBUG messages weren't being saved to the log so I fixed that along with some other bugs I had. Hopefully it works now. Please make sure that you have something that has been recently watched and not removed from the library. The script only parses from the recently viewed list. I also added a version number so you can keep track of the latest versions. The link has changed so please use the following link for the newest version

 ngovil21's PlexAutoDelete_1.1.py: https://dl.dropboxusercontent.com/u/9141060/PlexAutoDelete_1.1.py

Sorry I haven't had a chance to read through all the posts yet, but I wanted to ask this. Can this work with Plex Home and different users' watch status?

Haven't really tested because I don't use Plex Home, but I would assume it only uses the main accounts watch status. For other users, it might be possible to get their status using a different token, but I don't know how to get the token for Home users. Currently the token is retrieved using plex.tv authentication, so accounts made through plex.tv could potentially work.

I just tested the latest script and all is working! So I can also confirm that this does work with a plex home setup, as well as with pins! Thank you again, all of you rock!

Well, I think I spoke too soon...rats! The script is now able to flag the media, but is unable to move it. To reiterate, I'm running the script on a local windows computer that is connecting to a remote plex server on a linux machine. I have my DestinationDir = "/media/transfer/movies/", however I keep getting the error: error moving file: [WinError 3] The system cannot find the path specified: 'C:\\data\\media\\Movies\\Campaign, The (2012)\\The Campaign (2012) DVD-Rip DVD.avi'. I do have the remote machine mounted via sshfs, and tried changing the directory to a local one (S:\media ransfer\movies) but that didn't work either. It seems to only try to move it to the local C drive. Ideally I'd just move it to a different folder on the linux box. Any suggestions?

Settings:

PC = "L"
Host = "xxx.xx.xxx.xx"
Port = "xxxx"
SectionList = []
IgnoreSections = []
Delete = "0"
DeleteSimilar = "1"
PlexDelete = "1" 
Move = "1"
Copy = "0"
Shows = []
OnDeck = "1"
DestinationDir = "/data/transfer/movies/"
DestinationSection = ""
TriggerRescan = True
KeepMode = ""
DaysToKeep = "0"
 
LogFile = "C://Python34//Code//PlexLog.txt"
 
#Use Username/Password or Token.
Username = ""
Password = ""
#  or
Token = "xxxxxxxxxxxxxxxxxxxxxxx"
 
 
 
Output:
INFO:root:Running in Test Mode
INFO:root:----------------------------------------------------------------------------
INFO:root:                           Detected Settings
INFO:root:----------------------------------------------------------------------------
INFO:root:Host: 185.21.217.18
INFO:root:Port: 5813
INFO:root:Using Token for login
INFO:root:Keep Mode: DISABLED
INFO:root:Show Count: 0
INFO:root:Number of Shows Detected For Keeping: 0
INFO:root:Shows to Keep: | 
INFO:root:Delete: Disabled - Flagging Only
INFO:root:Delete OnDeck: No
INFO:root:Operating System: Linux 
INFO:root:Section List Mode: Auto
INFO:root:Operating on sections: 1,2
INFO:root:Skipping Sections: 
INFO:root:
INFO:root:--------- Section 1: Movies (Cloud) -----------------------------------
INFO:root: 
INFO:root:Viewed:1x | Days Since Viewed: 2 Added: 7 | /data/media/Movies/Campaign, The (2012)/The Campaign (2012) DVD-Rip DVD.avi
INFO:root:**[FLAGGED] /data/media/Movies/Campaign, The (2012)/The Campaign (2012) DVD-Rip DVD.avi
INFO:root:
INFO:root:--------- Section 2: TV Shows (Cloud) -----------------------------------
INFO:root:
INFO:root:----------------------------------------------------------------------------
INFO:root:----------------------------------------------------------------------------
INFO:root:                Summary -- Script Completed Successfully
INFO:root:----------------------------------------------------------------------------
INFO:root:
INFO:root:  Total File Count      1
INFO:root:  Kept Show Files       0
INFO:root:  On Deck Files         0
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:----------------------------------------------------------------------------

Ya the Script can only use the API from the Plex Media Server, so the file paths will be relative to the Plex Media Server. Any reason you can't run the script on the remote machine? That would be the the best choice. I run the script daily on a remote linux box using cron.

I could give you instructions on how to edit the script to replace the remote path with the correct local path, but that's more of a hack than a good solution.

You're right, that is what I should do. I'm simply not as familiar with the linux environment in general, but sounds like I'm about to be! Thanks for your willingness to help, but if the modification will simply be for me and not help the general public that uses this script then it isn't worth it. Again, thanks for the willingness though! 

Last thing however, you say that the script uses the paths associated with the PMS; my PMS is on the server, so shouldn't it be using the linux paths anyway? Just a thought.

You're right, that is what I should do. I'm simply not as familiar with the linux environment in general, but sounds like I'm about to be! Thanks for your willingness to help, but if the modification will simply be for me and not help the general public that uses this script then it isn't worth it. Again, thanks for the willingness though!

Well if you were able to setup the linux server with plex, then it shouldn't be too hard to add the script. Run the script with --test as the argument until you have all the settings the way you want and then look at using cron to run the script automatically for you.

Last thing however, you say that the script uses the paths associated with the PMS; my PMS is on the server, so shouldn't it be using the linux paths anyway? Just a thought.

No because the deleting/moving/copying is being done by the local computer (in your case the Windows pc) so the linux path would need to be converted to the location mounted on the local computer. I could do this in a simple way, but it would be more difficult to maintain the script and I would get questions on how to set it up. The proper way to do this would be through ssh, but that would be much more complicated.

I have the remote machine mounted via SSHFS, so I’ll try using that as the path instead. If it doesn’t work, then I’ll move it to linux. Thanks again!

Ok, this may sound like a stupid question, but how exactly to I use this?   I have created the bat file and downloaded the latest python file. Now what exactly goes in the bat file and where do I need to put both of these files?  

Thanks, 

I added a try / except to the "DELETE SHOWS" portion of the code on line 192 to prevent access denied error from stopping script.

Ok, this may sound like a stupid question, but how exactly to I use this?   I have created the bat file and downloaded the latest python file. Now what exactly goes in the bat file and where do I need to put both of these files?  

Thanks, 

1. Download and install python -> https://www.python.org/

2. Download the script -> https://dl.dropboxusercontent.com/u/9141060/PlexAutoDelete_1.1.py

3. Move the script to wherever you intend to 'run' it from.

4. Open the script in your favorite editor and modify the various variables at the top. I recommend Notepad ++ for this. -> http://notepad-plus-plus.org/

5. Set up your scheduler (cron/Task Scheduler/etc) to run the file. Here are the directions for Windows 7. -> http://windows.microsoft.com/en-us/windows/schedule-task#1TC=windows-7 

I tried this version and I get this:

----------------------------------------------------------------------------
                           Detected Settings
----------------------------------------------------------------------------
Host: 127.0.0.1
Port: 32400
No Login specified
Keep Mode: DISABLED
Show Count: 0
Number of Shows Detected For Keeping: 0
Shows to Keep: |
Delete: ***Enabled***
Delete OnDeck: No
Operating System: Windows (Auto Detected)
Section List Mode: Auto
Operating on sections: 1,2,11,13,14,15,19,20
Skipping Sections:

--------- Section 1: TV Shows -----------------------------------

Viewed:1x | Days Since Viewed: 0 Added: 0 | D:\vids v\The Daily Show with Jon Stewart\Season 20\The Daily Show with Jon Stewart - 2015-04-06 - Gene Baur [WEBDL-720p].mkv

Looking for D:\vids v\The Daily Show with Jon Stewart\Season 20\The Daily Show with Jon Stewart - 2015-04-06 - Gene Baur [WEBDL-720p]*
Traceback (most recent call last):
File “autodel.py”, line 528, in
changed += CheckShows(file,media_id);
File “autodel.py”, line 192, in CheckShows
for deleteFile in glob.glob(similarFiles):
File “c:\Python33\lib\glob.py”, line 18, in glob
return list(iglob(pathname))
File “c:\Python33\lib\glob.py”, line 50, in iglob
for name in glob_in_dir(dirname, basename):
File “c:\Python33\lib\glob.py”, line 69, in glob1
return fnmatch.filter(names, pattern)
File “c:\Python33\lib\fnmatch.py”, line 52, in filter
match = _compile_pattern(pat)
File “c:\Python33\lib\functools.py”, line 275, in wrapper
result = user_function(*args, **kwds)
File “c:\Python33\lib\fnmatch.py”, line 46, in _compile_pattern
return re.compile(res).match
File “c:\Python33\lib\re.py”, line 214, in compile
return _compile(pattern, flags)
File “c:\Python33\lib\re.py”, line 281, in _compile
p = sre_compile.compile(pattern, flags)
File “c:\Python33\lib\sre_compile.py”, line 494, in compile
p = sre_parse.parse(p, flags)
File “c:\Python33\lib\sre_parse.py”, line 748, in parse
p = _parse_sub(source, pattern, 0)
File “c:\Python33\lib\sre_parse.py”, line 360, in _parse_sub
itemsappend(_parse(source, state))
File “c:\Python33\lib\sre_parse.py”, line 506, in _parse
raise error(“bad character range”)
sre_constants.error: bad character range

Windows 8.1 with Python 3.3. Any idea?

Unfortunately the way you have the file named you won't be able to use the deleteSimilar files feature. That is because that function uses regex to find similar files and regex doesn't like the hyphen you have between the two brackets in [WEBDL-720p].

Try again with the deleteSimilar option set to 0 and it should work. Hopefully this doesn't affect your setup too much, it only helps delete leftover metadata files like nfo's or subtitles. If you would like to still use that option, then you will need to rename your files. Not much I can do about that.

Ok, I'm not sure if the brackets are in the standard naming scheme of Sonarr.

I changed line 192 to escape the brackets in the name:

        for deleteFile in glob.glob(re.sub(r'([\[\]])', '[\\1]',similarFiles)):

Got it from http://edgylogic.com/blog/escaping-square-brackets-pythons-glob/(3rd comment).

Works fine for me.

@apoapostolov the issue was that he was running the Script on a different computer than the plex media server, which was giving the paths on the plex media server to the file, not the path to the file on the computer the script was running on.

I added an option to delete the file using the Plex API, so you can run the script from a remote computer. It will not delete any similar files because Plex doesn't do that.

Hopefully this works for you wolph and wschlotter. You would need to run a separate script for each server to get a valid Token.

Newest version: https://www.dropbox.com/s/zlvt9gyp4vlkm43/PlexAutoDelete.py?dl=0

Completely missed your reply, that should do nicely :)

I'll give it a try.

Thanks for the help!

I added a MoveSimilar function for anyone who is using the move function and would like move the similar files also. Any python gurus feel free to correct or improve the function.  :)

I also included @brotbuexe's fix for escaping brackets

Here:

http://pastebin.com/GEezTKT3