Advertisement
gregwa

Full Circle Magazine #70 - TvRage Library part 3

Feb 1st, 2013
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.86 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.                      
  95. #========================================================
  96.     """
  97.        GetShowInfo(showid,debug)
  98.        Legal Keys:
  99.        ID,Name,Link,Country,Seasons,Image,Started,StartDate,Ended,
  100.        Status,Classification,Summary,Genres,Runtime,NetworkCountry,
  101.        Network,Airtime,Airday,Timezone
  102.    """
  103.     def GetShowInfo(self,showid,debug=0):
  104.         showidstr = str(showid)
  105.         strng = self.GetShowInfoString + self.ApiKey + "&sid=" + showidstr
  106.         urllib.socket.setdefaulttimeout(8)
  107.         usock = urllib.urlopen(strng)
  108.         tree = ET.parse(usock).getroot()
  109.         usock.close()
  110.         dict = {}
  111.         for child in tree:
  112.             if child.tag == 'showid':
  113.                 dict['ID'] = child.text
  114.             elif child.tag == 'showname':
  115.                 dict['Name'] = child.text
  116.             elif child.tag == 'showlink':
  117.                 dict['Link'] = child.text
  118.             elif child.tag == 'origin_country':
  119.                 dict['Country'] = child.text
  120.             elif child.tag == 'seasons':
  121.                 dict['Seasons'] = child.text
  122.             elif child.tag == 'image':
  123.                 dict['Image'] = child.text
  124.             elif child.tag == 'started':
  125.                 dict['Started'] = child.text
  126.             elif child.tag == 'startdate':
  127.                 dict['StartDate'] = child.text
  128.             elif child.tag == 'ended':
  129.                 dict['Ended'] = child.text
  130.             elif child.tag == 'status':
  131.                 dict['Status'] = child.text
  132.             elif child.tag == 'classification':
  133.                 dict['Classification'] = child.text
  134.             elif child.tag == 'summary':
  135.                 dict['Summary'] = child.text
  136.             elif child.tag == 'genres':
  137.                 genrestring = None
  138.                 for subelement in child:
  139.                     if subelement.tag == 'genre':
  140.                         if subelement.text != None:
  141.                             if genrestring == None:
  142.                                 genrestring = subelement.text
  143.                             else:
  144.                                 genrestring += " | " + subelement.text
  145.                 dict['Genres'] = genrestring                
  146.             elif child.tag == 'runtime':
  147.                 dict['Runtime'] = child.text
  148.             elif child.tag == 'network': # has attribute
  149.                 #print child.attrib['country'],child.text
  150.                 dict['NetworkCountry'] = child.attrib['country']
  151.                 dict['Network'] = child.text
  152.             elif child.tag == 'airtime':
  153.                 dict['Airtime'] = child.text
  154.             elif child.tag == 'airday':
  155.                 dict['Airday'] = child.text
  156.             elif child.tag == 'timezone':
  157.                 dict['Timezone'] = child.text
  158.  
  159.         return dict    
  160.        
  161.     def DisplayShowInfo(self,dict):
  162.         print "Show: %s" % dict['Name']
  163.         print "ID: %s  Started: %s  Ended: %s  Start Date: %s  Seasons: %s" % (dict['ID'],dict['Started'],dict['Ended'],dict['StartDate'],dict['Seasons'])
  164.         print "Link: %s" % dict['Link']
  165.         print "Image: %s" % dict['Image']
  166.         print "Country: %s  Status: %s   Classification: %s" % (dict['Country'],dict['Status'],dict['Classification'])
  167.         print "Runtime: %s  Network: %s   Airday: %s   Airtime: %s" % (dict['Runtime'],dict['Network'],dict['Airday'],dict['Airtime'])
  168.         print "Genres: %s" % dict['Genres']
  169.         print "Summary: \n%s" % dict['Summary']
  170.            
  171.            
  172.     def GetEpisodeList(self,showid,debug=0):
  173.         showidstr = str(showid)
  174.         strng = self.GetEpisodeListString + self.ApiKey + "&sid=" + showidstr
  175.         urllib.socket.setdefaulttimeout(8)
  176.         usock = urllib.urlopen(strng)
  177.         tree = ET.parse(usock).getroot()
  178.         usock.close()        
  179.         for child in tree:
  180.             if child.tag == 'name':
  181.                 ShowName = child.text
  182.             elif child.tag == 'totalseasons':
  183.                 TotalSeasons = child.text
  184.             elif child.tag == 'Episodelist':
  185.                 for c in child:
  186.                     if c.tag == 'Season':
  187.                         dict = {}
  188.                         seasonnum = c.attrib['no']
  189.                         for el in c:
  190.                             if el.tag == 'episode':
  191.                                 dict={}
  192.                                 dict['Season'] = seasonnum                                
  193.                                 for ep in el:
  194.                                     if ep.tag == 'epnum':
  195.                                         dict['EpisodeNumber'] = ep.text
  196.                                     elif ep.tag == 'seasonnum':
  197.                                         dict['SeasonEpisodeNumber'] = ep.text
  198.                                     elif ep.tag == 'prodnum':
  199.                                         dict['ProductionNumber'] = ep.text
  200.                                     elif ep.tag == 'airdate':
  201.                                         dict['AirDate'] = ep.text
  202.                                     elif ep.tag == 'link':
  203.                                         dict['Link'] = ep.text
  204.                                     elif ep.tag == 'title':
  205.                                         dict['Title'] = ep.text
  206.                                     elif ep.tag == 'summary':
  207.                                         dict['Summary'] = ep.text
  208.                                     elif ep.tag == 'rating':
  209.                                         dict['Rating'] = ep.text
  210.                                     elif ep.tag == 'screencap':
  211.                                         dict['ScreenCap'] = ep.text
  212.                                 self.EpisodeItem.append(dict)
  213.                                            
  214.         return ShowName,TotalSeasons,self.EpisodeItem
  215.        
  216. #========================================================
  217.  
  218.  
  219. #========================================================        
  220.  
  221.     def DisplayEpisodeList(self,SeriesName,SeasonCount,EpisodeList):
  222.         print "--------------------------------------"
  223.         print "Series Name: %s" % SeriesName
  224.         print "Total number of seasons: %s" % SeasonCount
  225.         print "Total number of episodes: %d" % len(EpisodeList)
  226.         print "--------------------------------------"
  227.         for e in EpisodeList:
  228.             print "Season: %s" % e['Season']
  229.             print "   Season Episode Number: %s - Series Episode Number: %s" % (e['SeasonEpisodeNumber'],e['EpisodeNumber'])
  230.             print "   Title: %s" % e['Title']
  231.             if e.has_key('Rating'):
  232.                 print "   Airdate: %s   Rating: %s" % (e['AirDate'],e['Rating'])
  233.             else:
  234.                 print "   Airdate: %s   Rating: NONE" % e['AirDate']
  235.             if e.has_key('Summary'):
  236.                 print "   Summary: \n%s" % e['Summary']
  237.             else:
  238.                 print "   Summary: NA"
  239.             print "==========================="
  240.         print "------------ End of episode list ------------"
  241.        
  242. def main():
  243.     tr = TvRage()
  244.     #--------------------
  245.     # Find Series by name
  246.     #--------------------
  247.     nam = raw_input("Enter Series Name -> ")
  248.     if nam != None:
  249.         sl = tr.FindIdByName(nam)
  250.         which = tr.DisplayShowResult(sl)
  251.         if which == 0:
  252.             sys.exit()
  253.         else:
  254.             option = int(which)-1
  255.             id = sl[option]['ID']
  256.             print "ShowID selected was %s" % id
  257.     #------------------------------
  258.     # Get Show Info
  259.     #------------------------------    
  260.     showinfo = tr.GetShowInfo(id)
  261.     #------------------------------
  262.     # Display Show Info
  263.     #------------------------------
  264.     tr.DisplayShowInfo(showinfo)      
  265.     #-----------------------------
  266.     # Get Episode List
  267.     #-----------------------------
  268.     SeriesName,TotalSeasons,episodelist = tr.GetEpisodeList(id)
  269.     #-----------------------------
  270.     # Display Episode List
  271.     #-----------------------------
  272.     tr.DisplayEpisodeList(SeriesName,TotalSeasons,episodelist)
  273.     #-----------------------------    
  274. #===========================================================
  275. #       Main loop
  276. #===========================================================        
  277. if __name__ == "__main__":    
  278.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement