Python:TVCleanup

From Devicenull's Code

Jump to: navigation, search

This code cleans up the filenames of TV shows to not have a bunch of useless stuff. It also standardizes the season format to S01E01 format. Based off code from the find_meta.py script that comes with MythTV.

import sys,os,re
 
def cleanup_title(title):
        regs = [ re.compile("[sS]([0-9]+)[eE]([0-9]+)") # sXXeYY
                , re.compile("([0-9])([0-9][0-9])") # XYY
                , re.compile("([0-9])x([0-9]+)") # XxYY
                ]
        title = title.replace("_", " ").replace(".", " ").replace("-", " ")
        cut_point_strings = [
                "hdtv", "xvid", "dvd", "proper", "720p", "limited",
                "dsr", "pdtv", "cd", "disk", "disc", "lol", "caph",
                "avi", "preair"]
        lowest_cutpoint = len(title)
        for string in cut_point_strings:
                pos = title.lower().rfind(string)
                if pos > 0 and pos < lowest_cutpoint:
                        lowest_cutpoint = pos
 
        title = "%s%s" % (title[0].upper(),title[1:lowest_cutpoint].lower())
        for cur in regs:
                match = cur.search(title)
                if match != None:
                        show = title[0:match.start()].strip()
                        exten = title[match.end():]
                        season = "00%s" % match.groups(0)[0]
                        epi = "00%s" % match.groups(0)[1]
                        season = season[-2:]
                        epi = epi[-2:]
                        title = "%s S%sE%s%s" % (show,season,epi,exten)
 
        return title.strip()
 
for root, dirs, files in os.walk("."):
        for cur in files:
                ext = cur.split(".")[-1]
                if ext != "avi": continue
                dest_name = "%s.%s" % (cleanup_title(cur),ext)
                print cur, " -> ", dest_name
                os.rename(cur,dest_name)