Last active 1440820570

Script to rename daily show episodes to "<show name> sXXeXX"

showprocess.py Raw
1import os
2import requests
3import XML2Dict # The PyPI installer doesn't work; get this from http://blha303.com.au/XML2Dict.zip
4from sys import argv
5
6apikey = argv[1] # Get your API key from http://www.thetvdb.com/wiki/index.php?title=Programmers_API
7ids = {"The.Daily.Show": "71256", "The.Colbert.Report": "79274", "Craig.Ferguson": "73387"}
8eplookupurl = "http://thetvdb.com/api/GetEpisodeByAirDate.php?apikey=%s&seriesid=%s&airdate=%s"
9srslookupurl = "http://thetvdb.com/data/series/%s/en.xml"
10# I hate working with XML, so let's make them dicts.
11xml = XML2Dict.encoder.XML2Dict()
12continuing = False
13# iterate over files in current directory
14for filename in os.listdir('.'):
15 # iterate over list of shows to process, above
16 for k,v in ids.items():
17 # If filename contains one of the keys from the list
18 if k in filename:
19 # Get the airdate from the filename
20 airdate = "-".join(filename.split(".")[len(k.split(".")):len(k.split("."))+3])
21 print "[%s] Processing %s" % (k, filename)
22 # Get episode data from thetvdb
23 data = xml.parse(requests.get(eplookupurl % (apikey, v, airdate)).text)
24 # Get show data from thetvdb, to get the proper series name
25 data["Data"]["Episode"]["SeriesName"] = xml.parse(requests.get(srslookupurl % v).text)["Data"]["Series"]["SeriesName"]
26 out = "{SeriesName} s{SeasonNumber}e{EpisodeNumber}.".format(**data["Data"]["Episode"]) + filename.split(".")[-1]
27 # Rename file
28 os.rename(filename, out)
29 print "[%s] Renamed to %s" % (k, out)
30 # Continue for both for loops, because we've already identified the show.
31 continuing = True
32 continue
33 if continuing:
34 continuing = False
35 continue
36 print "Skipping " + filename