Plex Auto-Delete Script

Ah ha! I see why it's not working then. The script is trying to match RecentlyViewed episodes with OnDeck episodes. That will never happen. The episodes in OnDeck are always either UnWatched (or InProgress) so they will never match up to any episodes in RecentlyViewed (all always watched status.)

Here is how OnDeck works (in my understanding:)

If there is a TV episode marked as recently viewed then On Deck will show the next unwatched episode in that series as On Deck.

That Title2 parameter is very interesting. I wonder... 

Hmm, now I'm a little confused.

Given what you just said that OnDeck is either Un-watched or In-progress. Then the original script, should have touched it at all. Unless the view-count isn't correct. But I thought I originally tested that watching shows half way through and watching shows fully threw even marking it as watched and then re-marking it as unwatched and it either changed that count or set it to 0. BUT Speaking that out loud now I think I just found a small coding flaw. As view-count isn't resetting to zero so it might keep the value of the file it previously checked.. Which is why it might deleting some shows that it shouldn't be..

So yea, like I said it should only be deleting shows that were marked as watched, if its unwatched or in progress, it shouldn't be deleted. That was the idea at least LOL.

But yea, I think I need to add a fix for resetting that view-count so that it doesn't accidentally take the previous files incase the next file doesn't have it.

Plex is wierd (and its probably for performance reasons) but a show will only have a view-count paremeter if its been viewed. Otherwise it will not have a view-count of 0. UNLESS you have marked it as watched and then re-marked it as watched.

OK, enough blabbing gotta head to work.

I'll take a look at that view-count thing sometime in the next 24 hours if I can and I'll post it when its done.

Thanks for you interest, help and feedback on this project. I appreciate it.

Hmm, now I'm a little confused.

Given what you just said that OnDeck is either Un-watched or In-progress. Then the original script, should have touched it at all. 

Yeah, you are right! The original script doesn't touch OnDeck items. The updated script doesn't touch OnDeck items. The problem is that when all of a shows watched episodes are deleted Plex then removes that show from OnDeck. The script in its current form ruins the OnDeck function. The only way around this is to always keep one watched episode around.

Anyways, thanks a million for making this script happen! It is really so so useful. Manually deleting shows is so old school!  B)

Sorry I haven't had a time to sit down and look at the xml data for that parementer that says "onDeck" yet...

So going back to a little hunch,

Try Changing this portion of the code near the bottom and let me know it makes any difference...

####################################################################################

Get Files for Watched Shows

####################################################################################
for VideoNode in doc.getElementsByTagName(“Video”):
view = ‘’
view = VideoNode.getAttribute(“viewCount”)
if view == ‘’:
view = 0
view = int(view)

all your doing is adding that line that say view = '' above the line that says view = VideoNode...

EDIT:

After Posting This, I realized its probably going to do nothing as the line below it should have already determined it couldn't find anything.. BUT just wanted to make sure...

Maybe run the script >> logfile and compare the 2...

Here is the log with the change:

----------------------------------------------------------------------------
                           Detected Settings
----------------------------------------------------------------------------
Host: 127.0.0.1
Port: 32400
Section: 1
URL: http://127.0.0.1:32400/library/sections/1/recentlyViewed
OnDeck URL: http://127.0.0.1:32400/library/sections/1/onDeck
Show Count: 1
Number of Shows Detected For Keeping: 1
Shows to Keep: | Game of Thrones | 
Delete: Disabled - Flagging Only
Delete OnDeck: No
Operating System: Linux (Auto Detected)
----------------------------------------------------------------------------
----------------------------------------------------------------------------

**[FLAGGED] /Volumes/ext3TB/TV/Castle (2009)/Season 06/Castle (2009) - 6x08 - A Murder Is Forever.mkv
**[FLAGGED] /Volumes/ext3TB/TV/Castle (2009)/Season 06/Castle (2009) - 6x07 - Like Father, Like Daughter.mkv
**[FLAGGED] /Volumes/ext3TB/TV/Brooklyn Nine-Nine/Season 01/Brooklyn Nine-Nine - 1x08 - Old School.mkv
**[FLAGGED] /Volumes/ext3TB/TV/The Walking Dead/Season 04/The Walking Dead - 4x02 - Infected.mkv
**[FLAGGED] /Volumes/ext3TB/TV/The Walking Dead/Season 04/The Walking Dead - 4x01 - 30 Days Without an Accident.mkv
**[FLAGGED] /Volumes/ext3TB/TV/The Wrong Mans/Season 01/The Wrong Mans - 1x04 - Inside Mans.mkv
**[FLAGGED] /Volumes/ext3TB/TV/The Wrong Mans/Season 01/The Wrong Mans - 1x03 - Dead Mans.mkv
**[FLAGGED] /Volumes/ext3TB/TV/The Wrong Mans/Season 01/The Wrong Mans - 1x02 - Bad Mans.mkv
**[FLAGGED] /Volumes/ext3TB/TV/The Wrong Mans/Season 01/The Wrong Mans - 1x01 - The Wrong Mans.mkv
**[FLAGGED] /Volumes/ext3TB/TV/New Girl/Season 03/New Girl - 3x07 - Coach.mkv



            Summary -- Script Completed Successfully

Total File Count 10
Kept Show Files 0
On Deck Files 0
Deleted Files 0
Flagged Files 10



Idea: What if we keep one watched episode around for each series by using the most recent originallyAvailableAt= '' date. I think that would work right?

FYI, I figured out that the AddedAt = '' and updatedAt = '' figures are unix timestamps

I converted one from my log here: http://www.onlineconversion.com/unix_time.htm

addedAt="1384264854= Tue, 12 Nov 2013 14:00:54 GMT

Also this might be helpful: http://stackoverflow.com/questions/3682748/converting-unix-timestamp-string-to-readable-date-in-python

Additionally, I figured out what the onDeck parameter is. It is placed on the episode that is first in line for On Deck. If I watch an episode that has following unwatched episodes Plex will give it that tag/parameter and show that next episode as the first onDeck episode.

I don't think it will be useful to us. I think what I mentioned in the last post would work though.

I don't think we need to even look at the OnDeck XML. I think you can delete the OnDeck portions from the script and replace them with a function that compares the originallyAvailableAt dates for each file (obtained from recentlyWatched XML) against all watched files and then keeps only the most recent file for each series based on that date.

Awesome job with the timestamps, I can work with that. Ty for the link as well.

Yea we can try all your ideas. They all sound plausible. There's rarely ever just 1 way of doing things, many different ways to get the end result.

So yea, thanks. I'm gonna try and work in next week heading out of town this weekend.

Awesome job with the timestamps, I can work with that. Ty for the link as well.

Yea we can try all your ideas. They all sound plausible. There's rarely ever just 1 way of doing things, many different ways to get the end result.

So yea, thanks. I'm gonna try and work in next week heading out of town this weekend.

Haha, yeah I wish I was more help. Have an awesome weekend!

I would have been scratching my head about that timestamps for a while, lol. Cause I could think of a number of possibilities. I will eventually need that for additional features too, so yea that's actually a big help.

I just noticed that the xml at http://localhost:32400/library/sections/1/ has the parameter:

title1="TV Shows" (similar for the movies section...)

Just FYI since that might be useful for a future fail-safe to check for that before running the script so that no one accidentally deletes their Movies. Probably not a big deal since you already have it defaulted to test mode but maybe something to consider when you add the Movies section I guess.

Yea, I've seen that. However, I believe everyone titles there's diffrently.

I have one media server laveld as TV shows, the other is labeled as TV-Shows.

But I guess if we looked for the word tv or show that might be sufficient if thats what they are naming it.

Sorry, I haven't had a chance to work on it this week, been working 14 days straight with no down-time.

Sorry, I haven't had a chance to work on it this week, been working 14 days straight with no down-time.

No worries! Work comes first. Thanks for your time

I found another potentially useful thing:

In Plex Media Server settings you will find this:

I would guess that value might be usable in a config file. Perhaps it could be useful?

Hey Steven4x4,

Thanks for this!

I modified your script a bit.  Changes:

   -  loop on multiple sections instead of just operating on one section

   -  added an option for "move files" instead of just deleting files.  I prefer to move watched files to another directory, where I can delete them manually, in case anything gets accidentally marked as watched. (read: in case i fall asleep while watching.)

The move stuff uses shutil to do some symlink handling, b/c I like to use symlinks to create different views.  I'm not sure how portable this is to other OS's, but works fine in Linux.

I'd provide you with a diff, but I also moved your functions to the top & had to indent the rest to create a loop, so the diff would be pretty confusing. 

I've attached a copy of my modified script in case you're interested.  And if not, that's cool too.  Thanks again.

plex_move_files.txt (8.82 KB)

Edit

I've made a couple of changes and attached a second version of this script.  This script includes the above changes and these:

  • Added an option for auto-discovery of all sections. So you don't have to specify them in a list. The old way still works too.
  • Added an option to ignore some sections if using the auto-discovery option
  • Added an option to trigger an immediate library rescan for affected sections.  The sections are only rescanned if changes happened w/ that section. 
  • Added an option to always rescan the destination dir, if changes were made. (in the case of move & copy)
  • Added the "copy" option that Steven4x4 mentioned.  (Note on using this: it looks like python's "move" varies by platform.  In some cases it will clobber the destination file if it exists, in others it will throw an exception.  I haven't spent much time sussing it all out, because I don't think people use this option for anything except testing.  But just be aware, if you first run with "copy" selected, then later switch to "move," python might complain about about the destination file already existing.)
  • [minor] added some handling for str/int types.  In the original, numbers were specified as a string. Now the lists can be specified as either strings or ints, and both should work.
  • [minor] - oh, and I added "minor changes by egretsareherons" to the top.  I'm not trying to steal your credit, as far as I'm concerned it's your script (Thanks again, btw) I just don't want you getting blamed for anything I may have screwed up. 

plex_move_watched_v2.txt (12.2 KB)

STEEVo:

Thanks for the input. Sorry, I still haven't found much time to work on this. I had to revert back to my Windows workstation for my PMS.

egretsareherons:

Nice Addition.

I think an option to allow for COPY only, might be nice to. (for testing if people don't want to do the "MOVE").

Yea, I'm not quite sure if the symlinks will work the same in windows, although the concept of them is quite the same.

I will have to see how that works on my windows box. If it doesn't work, then come up with the windows method so it keeps the code universal. As for unix (like macs) I belive the symlinks should work too, but not certian.

It a great option, so I'll defintely be adding if I can find some time to work on this some more. I'll add you in the credit notations as well.

Thanks for your input and contribtuions.

Hey, cool new features egretsareherons!  :D I will test it on os x.

Any chance I can convince you to add a means of keeping one watched copy of each show around? I have been holding off implementing the script in my setup because it ruins the On Deck function when all watched shows are deleted. This makes it confusing for my wife as she uses On Deck section to play her shows/next episodes. I think it would be a really useful addition to this script.

The best way that I have been able to think to implement this I mentioned here: https://forums.plex.tv/topic/84822-plex-auto-delete-script/page-2#entry500116

Thanks again Steven4x4! This script was long overdue!  :)

Steven4x4, I added the "copy" feature you mentioned, and also a few more options. I edited my original comment w/ these changes and linked newer script there.  (old one is still there too.)  Here is a summary of the changes:

I've made a couple of changes and attached a second version of this script.  This script includes the above changes and these:

  • Added an option for auto-discovery of all sections. So you don't have to specify them in a list. The old way still works too.
  • Added an option to ignore some sections if using the auto-discovery option
  • Added an option to trigger an immediate library rescan for affected sections.  The sections are only rescanned if changes happened w/ that section. 
  • Added an option to always rescan the destination dir, if changes were made. (in the case of move & copy)
  • Added the "copy" option that Steven4x4 mentioned.  (Note on using this: it looks like python's "move" varies by platform.  In some cases it will clobber the destination file if it exists, in others it will throw an exception.  I haven't spent much time sussing it all out, because I don't think people use this option for anything except testing.  But just be aware, if you first run with "copy" selected, then later switch to "move," python might complain about about the destination file already existing.)
  • [minor] added some handling for str/int types.  In the original, numbers were specified as a string. Now the lists can be specified as either strings or ints, and both should work.
  • [minor] - oh, and I added "minor changes by egretsareherons" to the top.  I'm not trying to steal your credit, as far as I'm concerned it's your script (Thanks again, btw) I just don't want you getting blamed for anything I may have screwed up. 

See my previous comment for the script.  https://forums.plex.tv/topic/84822-plex-auto-delete-script/page-2#entry537084

STEEVo:  The change you're suggesting with keeping one show around so ondeck works correctly would be handy. It non-trivial, though, at least for me   I don't yet know enough about plex to be able to do something like that.  I just installed it for the first time a few days ago and I don't really understand how/why it determines show names.  So I'm not your guy. At least not yet.  Maybe, someday.  :)  Hopefully someone else is interested & can add something like that.

STEEVo:  The change you're suggesting with keeping one show around so ondeck works correctly would be handy. It non-trivial, though, at least for me   I don't yet know enough about plex to be able to do something like that.  I just installed it for the first time a few days ago and I don't really understand how/why it determines show names.  So I'm not your guy. At least not yet.  Maybe, someday.   :)  Hopefully someone else is interested & can add something like that.

Thanks egretsareherons, great new additions!

I am thinking the KeepOnDeck function should be implemented without regard for Plex naming conventions.

Example: the current script finds 5 watched shows for deletion for the tv series "bones." However, if Keep OnDeck is ticked in the script, before deleting those five, the script runs a "which-one-of you-5-has-the-most-recent-date-for: originallyAvailableAt value?" Then the script deletes the 4 files and keeps the keeps the one that has that most recent date. It seems to me that this would all be based on only the filenames and the originallyAvailableAt value.) This would of course then repeat for each show.

Wouldn't that method avoid any need for obtaining or using show name logic from Plex? Again, I wish I had any programming skills. I would try it. Thank you both again for all the work on this script. I think it is fantastic!

Steven4x4, I added the "copy" feature you mentioned, and also a few more options. I edited my original comment w/ these changes and linked newer script there.  (old one is still there too.)  Here is a summary of the changes:

I've made a couple of changes and attached a second version of this script.  This script includes the above changes and these:

  • Added an option for auto-discovery of all sections. So you don't have to specify them in a list. The old way still works too.
  • Added an option to ignore some sections if using the auto-discovery option
  • Added an option to trigger an immediate library rescan for affected sections.  The sections are only rescanned if changes happened w/ that section. 
  • Added an option to always rescan the destination dir, if changes were made. (in the case of move & copy)
  • Added the "copy" option that Steven4x4 mentioned.  (Note on using this: it looks like python's "move" varies by platform.  In some cases it will clobber the destination file if it exists, in others it will throw an exception.  I haven't spent much time sussing it all out, because I don't think people use this option for anything except testing.  But just be aware, if you first run with "copy" selected, then later switch to "move," python might complain about about the destination file already existing.)
  • [minor] added some handling for str/int types.  In the original, numbers were specified as a string. Now the lists can be specified as either strings or ints, and both should work.
  • [minor] - oh, and I added "minor changes by egretsareherons" to the top.  I'm not trying to steal your credit, as far as I'm concerned it's your script (Thanks again, btw) I just don't want you getting blamed for anything I may have screwed up. 

See my previous comment for the script.  https://forums.plex.tv/topic/84822-plex-auto-delete-script/page-2#entry537084

I really enjoy programming and would love to spend time working on this script, but I just don't know where my time goes anymore.

Hopefully it will get easier in the new year.

I'm glad I was able to contribue this script as it seems a ton of people have been using it (although they don't post), but thats alright with me.

I don't at all feel you are trying to steal my credit. I just wanted you to know when I complie a new script and add the features your develo[ed in to my original code, that you will be noted for your additions.

I most defintely don't like taking credit for anyones work. So just wanted to make sure that your contributions are know.

I appreciate your interest in further developing this script.

STEEVo:  The change you're suggesting with keeping one show around so ondeck works correctly would be handy. It non-trivial, though, at least for me   I don't yet know enough about plex to be able to do something like that.  I just installed it for the first time a few days ago and I don't really understand how/why it determines show names.  So I'm not your guy. At least not yet.  Maybe, someday.   :)  Hopefully someone else is interested & can add something like that.

Same Here - I tried using the On Deck, but it doesn't work the way I wanted it to. When I view a show On Deck, sure thats cool it brought me to the latest episode, but when I need to watch the episode after that, it not very easy.

I typically go to TV Shows > Unwatched > Dexter and watch my shows like that.

Thanks egretsareherons, great new additions!

I am thinking the KeepOnDeck function should be implemented without regard for Plex naming conventions.

Example: the current script finds 5 watched shows for deletion for the tv series "bones." However, if Keep OnDeck is ticked in the script, before deleting those five, the script runs a "which-one-of you-5-has-the-most-recent-date-for: originallyAvailableAt value?" Then the script deletes the 4 files and keeps the keeps the one that has that most recent date. It seems to me that this would all be based on only the filenames and the originallyAvailableAt value.) This would of course then repeat for each show.

Wouldn't that method avoid any need for obtaining or using show name logic from Plex? Again, I wish I had any programming skills. I would try it. Thank you both again for all the work on this script. I think it is fantastic!

I know there a probably a number of ways to get this to work, but just haven't quite understood how Plex is keeping track of what's On Deck.

Because to me it seems they have to pouplate an arry (small database) with all of the epsiodes of that show, then determine which is the last one.

To me that is poor programming as it causes alot of over head in ram utilizing that array. Now I'm sure they don't actually do this and they have some sorta method to their madness.

We just have to figure out that method, because your above method describe is a typyciall way of doing things if we don't have those key identifiers handy.

But again, I'm sure there is a key identifier, but haven't had the time to clearly look through and make the distiction of how Plex is associating their ViewShows attritubites to the attributes in there OnDeck.

Because I tried referencing NOT deleting a viewed show if it was in the OnDeck section. But you said that didn't quite work. Cause my understanding was that Plex already did the hard work after you watched the show it changed the attributes on the episodes and identified which one was OnDeck, (Since it Clearly has a Specific Section for OnDeck) so that means it should have already been marked and not depent on other files, but its seems thats not the case.

So yea, sorry for all the blabling, I just haven't had the time to sit down and figure out what exact distinct attribute its setting/pulling to make programing it accpetable.

The array method you and I discussed could work, but there is just to much over head and I feel is poor programming to do that knowing there is probably a better way.