Advertisement
gregwa

FCM69 - TVRage part 1

Jan 10th, 2013
630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.31 KB | None | 0 0
  1. """
  2. Filename        : tvragetest.py
  3. Author          : G.D. Walters
  4. Purpose         : Query TVRage.com for info about tv shows
  5. """
  6. #=============================================================
  7. #                        IMPORTS
  8. #=============================================================
  9. from xml.etree import ElementTree as ET
  10. import urllib
  11. import sys
  12.  
  13. #=============================================================
  14. #                   Main Class
  15. #=============================================================
  16.  
  17. class TvRage:  
  18.     def __init__(self):
  19.         self.ApiKey = "Itnl8IyY1hsR9n0IP6zI"
  20.         self.FindSeriesString = "http://services.tvrage.com/myfeeds/search.php?key="
  21.         self.GetShowInfoString = "http://services.tvrage.com/myfeeds/showinfo.php?key="
  22.         self.GetEpisodeListString = "http://services.tvrage.com/myfeeds/episode_list.php?key="
  23.         self.ShowList = []
  24.         self.ShowInfo = []
  25.         self.EpisodeList = []
  26.         self.EpisodeItem = []
  27.  
  28.  
  29.     def FindIdByName(self,showname,debug = 0):
  30.         strng = self.FindSeriesString + self.ApiKey + "&show=" + showname
  31.         urllib.socket.setdefaulttimeout(8)
  32.         usock = urllib.urlopen(strng)
  33.         tree = ET.parse(usock).getroot()
  34.         usock.close()
  35.         foundcounter = 0
  36.         self.showlist = []
  37.         for node in tree.findall('show'):
  38.             showinfo = []
  39.             genrestring = None
  40.             dict = {}
  41.             for n in node:
  42.                 if n.tag == 'showid':
  43.                     showid = n.text
  44.                     dict['ID'] = showid
  45.                 elif n.tag == 'name':
  46.                     showname = n.text
  47.                     dict['Name'] = showname
  48.                 elif n.tag == 'link':
  49.                     showlink = n.text
  50.                     dict['Link'] = showlink
  51.                 elif n.tag == 'country':
  52.                     showcountry = n.text
  53.                     dict['Country'] = showcountry
  54.                 elif n.tag == 'started':
  55.                     showstarted = n.text
  56.                     dict['Started'] = showstarted
  57.                 elif n.tag == 'ended':
  58.                     showended = n.text
  59.                     dict['Ended'] = showended
  60.                 elif n.tag == 'seasons':
  61.                     showseasons = n.text
  62.                     dict['Seasons'] = showseasons
  63.                 elif n.tag == 'status':
  64.                     showstatus = n.text
  65.                     dict['Status'] = showstatus
  66.                 elif n.tag == 'classification':
  67.                     showclassification = n.text
  68.                     dict['Classification'] = showclassification
  69.                 elif n.tag == 'genres':
  70.                     for subelement in n:
  71.                         if subelement.tag == 'genre':
  72.                             if subelement.text != None:
  73.                                 if genrestring == None:
  74.                                     genrestring = subelement.text
  75.                                 else:
  76.                                     genrestring += " | " + subelement.text
  77.                     dict['Genres'] = genrestring
  78.             foundcounter += 1
  79.             self.showlist.append(dict)
  80.         return self.showlist
  81. #========================================================
  82.  
  83.     def DisplayShowResult(self, ShowListDict):
  84.         lcnt = len(ShowListDict)
  85.         print "%d Found" % lcnt
  86.         print "------------------------"
  87.         cntr = 1
  88.         for c in ShowListDict:
  89.             print "%d - %s - %s" % (cntr,c['Name'],c['ID']) #,c['Ended'],c['Link']
  90.             cntr += 1    
  91.         sel = raw_input("Enter Selection or 0 to exit -> ")
  92.         return sel
  93.                      
  94. def main():
  95.     tr = TvRage()
  96.     #--------------------
  97.     # Find Series by name
  98.     #--------------------
  99.     nam = raw_input("Enter Series Name -> ")
  100.     if nam != None:
  101.         sl = tr.FindIdByName(nam)
  102.         which = tr.DisplayShowResult(sl)
  103.         if which == 0:
  104.             sys.exit()
  105.         else:
  106.             option = int(which)-1
  107.             id = sl[option]['ID']
  108.             print "ShowID selected was %s" % id
  109.            
  110. #===========================================================
  111. #       Main loop
  112. #===========================================================        
  113. if __name__ == "__main__":    
  114.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement