Script to clean TV Shows
#1
This script walks through your tv shows and deletes those episodes that match the following criteria:
  • Has been watched
  • Older than 20 days (easy to change)
  • Not part of a "ignored path"
  • Not the only episode in the series

I store all my tv series in their own path, so the ignored path is useful for me when I don't want to delete certain episodes in an entire series.

Credit to mdpauley who I based some of my script off of one of his.

Anyone can use/modify this to their heart's content. It would be sweet if someone made a simple gui to modify ignored paths based on tv shows (ignore certain shows), and modify the older than date count.

There ARE more efficient ways of doing this, but I'm lazy and tired and had an itch to write this.

Code:
import xbmc
import pprint
from urllib import quote_plus
import re
import datetime

# set any paths to ignore here
ignore_paths = []

#change the -20 to match whatever range you want
d = datetime.datetime.now() + datetime.timedelta(days=-20)
minusdays = d.strftime('%Y-%m-%d')

# this gets filled with stuff, leave alone
eps_to_remove = []

print "finding shows with more than one episode"
# find all shows with more than 1 episode
shows_sql = "select count(idEpisode), idShow from tvshowlinkepisode group by idShow;"
shows = xbmc.executehttpapi( "QueryVideoDatabase(%s)" % ( quote_plus( shows_sql ), ) )

showsp=re.compile('<field>(.+?)</field><field>(.+?)</field>')
showmatch=showsp.findall(shows)
for epcount,showid in showmatch:
    if epcount > 1:
        print "searching show: %s" % showid
        # now get the episodes for each show
        eps_sql = "select tvshowlinkepisode.idEpisode from tvshowlinkepisode join episode on episode.idEpisode=tvshowlinkepisode.idEpisode where tvshowlinkepisode.idShow = %s" % showid
        eps = xbmc.executehttpapi( "QueryVideoDatabase(%s)" % ( quote_plus( eps_sql ), ) )
        epsp=re.compile('<field>(.+?)</field>')
        epsmatch=epsp.findall(eps)
    
        # get all the episodes, storing all but the most recent
        hi_ep = 0
        hi_s = 0
        hi_details = []
        for epid in epsmatch:
            print "searching episode: %s" % epid
            # now get the episode details if the episode is older than specified limit
            details_sql = "select episode.c12,episode.c13, episode.c05, files.playCount, files.strFilename, (select path.strpath from path where files.idpath = path.idpath) from files join episode on episode.idfile=files.idfile where episode.c05 <='"+minusdays+"' and episode.idEpisode = %s" % epid
            # gives season,ep, playtime, playcount, file, path
    
            records = xbmc.executehttpapi( "QueryVideoDatabase(%s)" % ( quote_plus( details_sql ), ) )
    
            p=re.compile('<field>(.+?)</field><field>(.+?)</field><field>(.+?)</field><field>(.+?)</field><field>(.+?)</field><field>(.+?)</field>')
            match=p.findall(records)
            for s,ep,playdate,playcount,file,path in match:
                if hi_s == 0 and hi_ep == 0:
                    # first, make it hi and move on
                    print "ignoring first found episode"
                    hi_s = s
                    hi_ep = ep
                    hi_details = [s,ep,playdate,playcount,file,path]
                elif s < hi_s:
                    # season is lower, just remove it
                    eps_to_remove.append([path,file])
                elif s > hi_s:
                    # season is greater, just remove current hi
                    eps_to_remove.append([hi_details[5],hi_details[4]])
                    hi_s = s
                                        hi_ep = ep
                                        hi_details = [s,ep,playdate,playcount,file,path]
                elif s == hi_s:
                    # seasons equal, test episodes
                    if ep < hi_ep:
                        # episode lower, just remove it
                        eps_to_remove.append([path,file])
                    else:
                        #episode is newer or equal, remove current hi
                        eps_to_remove.append([hi_details[5],hi_details[4]])
                                            hi_s = s
                                            hi_ep = ep
                                            hi_details = [s,ep,playdate,playcount,file,path]

for path,file in eps_to_remove:
    if path not in ignore_paths:
        full = path + file
        print "removing %s" % full
        xbmc.executehttpapi("FileDelete(%s)" % full)

xbmc.executebuiltin('XBMC.updatelibrary(video)')

Also, if someone has had success using xbmc with more than one sound card enabled on the system PLEASE let me know :confused2:
Reply
#2
Hi,

this is something i was looking for.
But it doesn't seem to work.

Here is my debug-log
http://pastebin.com/kDz1v9im

The Sript starts scanning and counting, but it does not delete anything.
http://www.xbmcnerds.com - german xbmc community
Reply
#3
Yes, please, make this script work! Watching shows like The Daily Show or Conan and then auto-delete to save space and time would be so nice!
Reply
#4
Didn't update my post.
The Script works (at least with Dharma), but all you see is the "end" of it, when it starts to update the library.

xbmc.executebuiltin('XBMC.updatelibrary(video)')
http://www.xbmcnerds.com - german xbmc community
Reply
#5
That's great news that the script is working in Dharma, but:
1 - I'm on Eden beta1
2 - I'm a total script newb and don't know how where to put this script. I can install plugins and repositories from zip files, but I don't know where to save this file and how to run it.
I appreciate any help.
Reply
#6
you can find the script here:
http://www.xbmcnerds.com/index.php?page=...&dataID=28

(if you can't download it, contact me again. i'll send you a zip-file)

after putting it (the folder) to your addons (and restart xbmc), the script should be visible at programs.
http://www.xbmcnerds.com - german xbmc community
Reply
#7
Thanks donabi, I got it (registering for your site in german was funny). I think the script is working, but I need a few more days to be sure. Funny how you altered the standard 20 days to 8, because I was going to do just that!
Reply
#8
It would be great if this could be updated with a GUI for convenience. The feature is surely lacking from XBMC, but my guess is that most users would prefer a more ... userfriendly - approach than manually editing a script to exclude paths.

Maybe just selecting the series to include in the cleanup would be preferable?

FFY
Reply
#9
I really need a copy of this script in ZIP format. Anyone have it they can send it on?

Reply

Logout Mark Read Team Forum Stats Members Help
Script to clean TV Shows0