From 574d02309365f2d20f4ace8c37d22cf30dcaf74c Mon Sep 17 00:00:00 2001 From: Michael Shepanski Date: Sun, 14 Oct 2018 21:43:27 -0400 Subject: [PATCH] Added keepSeason tag to keep only the latest season --- tools/plex-autodelete.py | 53 ++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/tools/plex-autodelete.py b/tools/plex-autodelete.py index ee3b854b..7bcb7d8a 100755 --- a/tools/plex-autodelete.py +++ b/tools/plex-autodelete.py @@ -6,18 +6,18 @@ This comes in handy when you have a show you keep downloaded, but do not religiously keep every single episode that is downloaded. Usage: -Intended usage is to add one of the tags keep5', keep10, keep15, to any show -you want to have this behaviour. Then simply add this script to run on a schedule -and you should be all set. +Intended usage is to add one of the tags keep5, keep10, keep15, keepSeason, to +any show you want to have this behaviour. Then simply add this script to run on +a schedule and you should be all set. Example Crontab: -*/5 * * * * /home/atodd/plex-autodelete.py >> /home/atodd/plex-autodelete.log 2>&1 +@daily /home/atodd/plex-autodelete.py >> /home/atodd/plex-autodelete.log 2>&1 """ import os from datetime import datetime from plexapi.server import PlexServer -TAGS = {'keep5':5, 'keep10':10, 'keep15':15} +TAGS = {'keep5':5, 'keep10':10, 'keep15':15, 'keepSeason':'season'} datestr = lambda: datetime.now().strftime('%Y-%m-%d %H:%M:%S') @@ -28,7 +28,6 @@ def delete_episode(episode): filepath = mediapart.file print('%s Deleting %s.' % (datestr(), filepath)) os.unlink(filepath) - # Check the parent directory is empty dirname = os.path.dirname(filepath) if not os.listdir(dirname): print('%s Removing empty directory %s.' % (datestr(), dirname)) @@ -37,18 +36,42 @@ def delete_episode(episode): print('%s Error deleting %s; %s' % (datestr(), filepath, err)) -if __name__ == '__main__': - print('%s Starting plex-markwatched script..' % datestr()) +def keep_episodes(show, keep): + """ Delete all but last count episodes in show. """ + deleted = 0 + print('%s Cleaning %s to %s episodes.' % (datestr(), show.title, keep)) + sort = lambda x:x.originallyAvailableAt or x.addedAt + items = sorted(show.episodes(), key=sort, reverse=True) + for episode in items[keep:]: + delete_episode(episode) + deleted += 1 + return deleted + + +def keep_season(show, keep): + """ Keep only the latest season. """ + deleted = 0 + print('%s Cleaning %s to latest season.' % (datestr(), show.title)) + for season in show.seasons()[:-1]: + for episode in season.episodes(): + delete_episode(episode) + deleted += 1 + return deleted + + +def auto_delete(): + print('\n%s Starting plex-autodelete script..' % datestr()) plex = PlexServer() for section in plex.library.sections(): if section.type in ('show',): - episodes_deleted = 0 + deleted = 0 for tag, keep in TAGS.items(): + func = keep_season if keep == 'season' else keep_episodes for show in section.search(collection=tag): - print('%s Cleaning %s to %s items.' % (datestr(), show.title, keep)) - items = sorted(show.episodes(), key=lambda x:x.originallyAvailableAt or x.addedAt, reverse=True) - for episode in items[keep:]: - delete_episode(episode) - episodes_deleted += 1 - if episodes_deleted: + deleted += func(show, keep) + if deleted: section.update() + + +if __name__ == '__main__': + auto_delete()