Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Filename : tvragetest.py
- Author : G.D. Walters
- Purpose : Query TVRage.com for info about tv shows
- """
- #=============================================================
- # IMPORTS
- #=============================================================
- from xml.etree import ElementTree as ET
- import urllib
- import sys
- #=============================================================
- # Main Class
- #=============================================================
- class TvRage:
- def __init__(self):
- self.ApiKey = "Itnl8IyY1hsR9n0IP6zI"
- self.FindSeriesString = "http://services.tvrage.com/myfeeds/search.php?key="
- self.GetShowInfoString = "http://services.tvrage.com/myfeeds/showinfo.php?key="
- self.GetEpisodeListString = "http://services.tvrage.com/myfeeds/episode_list.php?key="
- self.ShowList = []
- self.ShowInfo = []
- self.EpisodeList = []
- self.EpisodeItem = []
- def FindIdByName(self,showname,debug = 0):
- strng = self.FindSeriesString + self.ApiKey + "&show=" + showname
- urllib.socket.setdefaulttimeout(8)
- usock = urllib.urlopen(strng)
- tree = ET.parse(usock).getroot()
- usock.close()
- foundcounter = 0
- self.showlist = []
- for node in tree.findall('show'):
- showinfo = []
- genrestring = None
- dict = {}
- for n in node:
- if n.tag == 'showid':
- showid = n.text
- dict['ID'] = showid
- elif n.tag == 'name':
- showname = n.text
- dict['Name'] = showname
- elif n.tag == 'link':
- showlink = n.text
- dict['Link'] = showlink
- elif n.tag == 'country':
- showcountry = n.text
- dict['Country'] = showcountry
- elif n.tag == 'started':
- showstarted = n.text
- dict['Started'] = showstarted
- elif n.tag == 'ended':
- showended = n.text
- dict['Ended'] = showended
- elif n.tag == 'seasons':
- showseasons = n.text
- dict['Seasons'] = showseasons
- elif n.tag == 'status':
- showstatus = n.text
- dict['Status'] = showstatus
- elif n.tag == 'classification':
- showclassification = n.text
- dict['Classification'] = showclassification
- elif n.tag == 'genres':
- for subelement in n:
- if subelement.tag == 'genre':
- if subelement.text != None:
- if genrestring == None:
- genrestring = subelement.text
- else:
- genrestring += " | " + subelement.text
- dict['Genres'] = genrestring
- foundcounter += 1
- self.showlist.append(dict)
- return self.showlist
- #========================================================
- def DisplayShowResult(self, ShowListDict):
- lcnt = len(ShowListDict)
- print "%d Found" % lcnt
- print "------------------------"
- cntr = 1
- for c in ShowListDict:
- print "%d - %s - %s" % (cntr,c['Name'],c['ID']) #,c['Ended'],c['Link']
- cntr += 1
- sel = raw_input("Enter Selection or 0 to exit -> ")
- return sel
- #========================================================
- """
- GetShowInfo(showid,debug)
- Legal Keys:
- ID,Name,Link,Country,Seasons,Image,Started,StartDate,Ended,
- Status,Classification,Summary,Genres,Runtime,NetworkCountry,
- Network,Airtime,Airday,Timezone
- """
- def GetShowInfo(self,showid,debug=0):
- showidstr = str(showid)
- strng = self.GetShowInfoString + self.ApiKey + "&sid=" + showidstr
- urllib.socket.setdefaulttimeout(8)
- usock = urllib.urlopen(strng)
- tree = ET.parse(usock).getroot()
- usock.close()
- dict = {}
- for child in tree:
- if child.tag == 'showid':
- dict['ID'] = child.text
- elif child.tag == 'showname':
- dict['Name'] = child.text
- elif child.tag == 'showlink':
- dict['Link'] = child.text
- elif child.tag == 'origin_country':
- dict['Country'] = child.text
- elif child.tag == 'seasons':
- dict['Seasons'] = child.text
- elif child.tag == 'image':
- dict['Image'] = child.text
- elif child.tag == 'started':
- dict['Started'] = child.text
- elif child.tag == 'startdate':
- dict['StartDate'] = child.text
- elif child.tag == 'ended':
- dict['Ended'] = child.text
- elif child.tag == 'status':
- dict['Status'] = child.text
- elif child.tag == 'classification':
- dict['Classification'] = child.text
- elif child.tag == 'summary':
- dict['Summary'] = child.text
- elif child.tag == 'genres':
- genrestring = None
- for subelement in child:
- if subelement.tag == 'genre':
- if subelement.text != None:
- if genrestring == None:
- genrestring = subelement.text
- else:
- genrestring += " | " + subelement.text
- dict['Genres'] = genrestring
- elif child.tag == 'runtime':
- dict['Runtime'] = child.text
- elif child.tag == 'network': # has attribute
- #print child.attrib['country'],child.text
- dict['NetworkCountry'] = child.attrib['country']
- dict['Network'] = child.text
- elif child.tag == 'airtime':
- dict['Airtime'] = child.text
- elif child.tag == 'airday':
- dict['Airday'] = child.text
- elif child.tag == 'timezone':
- dict['Timezone'] = child.text
- return dict
- def DisplayShowInfo(self,dict):
- print "Show: %s" % dict['Name']
- print "ID: %s Started: %s Ended: %s Start Date: %s Seasons: %s" % (dict['ID'],dict['Started'],dict['Ended'],dict['StartDate'],dict['Seasons'])
- print "Link: %s" % dict['Link']
- print "Image: %s" % dict['Image']
- print "Country: %s Status: %s Classification: %s" % (dict['Country'],dict['Status'],dict['Classification'])
- print "Runtime: %s Network: %s Airday: %s Airtime: %s" % (dict['Runtime'],dict['Network'],dict['Airday'],dict['Airtime'])
- print "Genres: %s" % dict['Genres']
- print "Summary: \n%s" % dict['Summary']
- def GetEpisodeList(self,showid,debug=0):
- showidstr = str(showid)
- strng = self.GetEpisodeListString + self.ApiKey + "&sid=" + showidstr
- urllib.socket.setdefaulttimeout(8)
- usock = urllib.urlopen(strng)
- tree = ET.parse(usock).getroot()
- usock.close()
- for child in tree:
- if child.tag == 'name':
- ShowName = child.text
- elif child.tag == 'totalseasons':
- TotalSeasons = child.text
- elif child.tag == 'Episodelist':
- for c in child:
- if c.tag == 'Season':
- dict = {}
- seasonnum = c.attrib['no']
- for el in c:
- if el.tag == 'episode':
- dict={}
- dict['Season'] = seasonnum
- for ep in el:
- if ep.tag == 'epnum':
- dict['EpisodeNumber'] = ep.text
- elif ep.tag == 'seasonnum':
- dict['SeasonEpisodeNumber'] = ep.text
- elif ep.tag == 'prodnum':
- dict['ProductionNumber'] = ep.text
- elif ep.tag == 'airdate':
- dict['AirDate'] = ep.text
- elif ep.tag == 'link':
- dict['Link'] = ep.text
- elif ep.tag == 'title':
- dict['Title'] = ep.text
- elif ep.tag == 'summary':
- dict['Summary'] = ep.text
- elif ep.tag == 'rating':
- dict['Rating'] = ep.text
- elif ep.tag == 'screencap':
- dict['ScreenCap'] = ep.text
- self.EpisodeItem.append(dict)
- return ShowName,TotalSeasons,self.EpisodeItem
- #========================================================
- #========================================================
- def DisplayEpisodeList(self,SeriesName,SeasonCount,EpisodeList):
- print "--------------------------------------"
- print "Series Name: %s" % SeriesName
- print "Total number of seasons: %s" % SeasonCount
- print "Total number of episodes: %d" % len(EpisodeList)
- print "--------------------------------------"
- for e in EpisodeList:
- print "Season: %s" % e['Season']
- print " Season Episode Number: %s - Series Episode Number: %s" % (e['SeasonEpisodeNumber'],e['EpisodeNumber'])
- print " Title: %s" % e['Title']
- if e.has_key('Rating'):
- print " Airdate: %s Rating: %s" % (e['AirDate'],e['Rating'])
- else:
- print " Airdate: %s Rating: NONE" % e['AirDate']
- if e.has_key('Summary'):
- print " Summary: \n%s" % e['Summary']
- else:
- print " Summary: NA"
- print "==========================="
- print "------------ End of episode list ------------"
- def main():
- tr = TvRage()
- #--------------------
- # Find Series by name
- #--------------------
- nam = raw_input("Enter Series Name -> ")
- if nam != None:
- sl = tr.FindIdByName(nam)
- which = tr.DisplayShowResult(sl)
- if which == 0:
- sys.exit()
- else:
- option = int(which)-1
- id = sl[option]['ID']
- print "ShowID selected was %s" % id
- #------------------------------
- # Get Show Info
- #------------------------------
- showinfo = tr.GetShowInfo(id)
- #------------------------------
- # Display Show Info
- #------------------------------
- tr.DisplayShowInfo(showinfo)
- #-----------------------------
- # Get Episode List
- #-----------------------------
- SeriesName,TotalSeasons,episodelist = tr.GetEpisodeList(id)
- #-----------------------------
- # Display Episode List
- #-----------------------------
- tr.DisplayEpisodeList(SeriesName,TotalSeasons,episodelist)
- #-----------------------------
- #===========================================================
- # Main loop
- #===========================================================
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement