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