def __get_show_data(self, name): data = {'season': None, 'episode': None, 'timestamp': None, 'tvrage_show_id': None, 'duration': None} #print (TVRAGE_API_URL % name.replace(' ', '%20')) response = requests.get(TVRAGE_API_URL % name.replace(' ', '%20')) if response.status_code == requests.codes['\o/']: root = ElementTree.XML(response.text.encode('ascii', 'ignore')) rdata = XmlDictConfig(root) print rdata if 'status' in rdata.keys() and rdata['status'] in ACTIVE_SHOW_STATUS: if 'nextepisode' in rdata.keys(): epid = rdata['nextepisode']['number'] data['season'] = epid.split('x')[0] data['episode'] = epid.split('x')[1] if rdata['nextepisode']['airtime']['text'] is not None: data['timestamp'] = int(rdata['nextepisode']['airtime']['text']) - TZ_OFFSET else: data['timestamp'] = 0 data['tvrage_show_id'] = rdata['id'] data['duration'] = int(rdata['runtime']) * 60 data['show_name'] = rdata['name'] else: print "ended" else: print "BEEP BEEEEEP TVRAGE IS DOWN(%s)" % response.status_code raise TVRageDownException() return data
def __get_next_episode(self, name): data = {'season': None, 'episode': None, 'timestamp': None} try: response = requests.get(TVRAGE_API_URL % name.replace(' ', '%20')) except: print "BEEP. TVRage isn't responding to our request for this one, we'll have to try again later." return data if response.status_code == requests.codes['\o/']: root = ElementTree.XML(response.text.encode('ascii', 'ignore')) rdata = XmlDictConfig(root) print rdata if 'status' in rdata.keys() and rdata['status'] in ACTIVE_SHOW_STATUS: required_keys = ['nextepisode', 'name', 'runtime'] if set(required_keys).issubset(rdata.keys()) and 'number' in rdata['nextepisode'].keys(): epid = rdata['nextepisode']['number'] data['season'] = epid.split('x')[0] data['episode'] = epid.split('x')[1] if rdata['nextepisode']['airtime']['text'] is not None: data['timestamp'] = int(rdata['nextepisode']['airtime']['text']) - TZ_OFFSET else: data['timestamp'] = 0 data['duration'] = int(rdata['runtime']) * 60 data['show_name'] = rdata['name'] else: print "incomplete data from TVRage. Missing keys:" print [x for x in required_keys if x not in rdata.keys()] else: print "ended" return False else: print "BEEP BEEEEEP TVRAGE IS DOWN(%s)" % response.status_code raise TVRageDownException() return data
def __get_episode_after(self, name, season, episode, try_increment_season = True): data = {'season': None, 'episode': None, 'timestamp': None} attempted_season = int(season) attempted_episode = int(episode) + 1 try: response = requests.get(TVRAGE_EPISODE_API % (name.replace(' ', '%20'), attempted_season, attempted_episode)) except: print "BEEP. TVRage isn't responding to our request for this one, we'll have to try again later." return data if response.status_code == requests.codes['\o/']: root = ElementTree.XML(response.text.encode('ascii', 'ignore')) rdata = XmlDictConfig(root) print rdata if 'status' in rdata.keys() and rdata['status'] in ACTIVE_SHOW_STATUS: if 'episode' not in rdata.keys(): if try_increment_season: attempted_season = int(season) + 1 attempted_episode = 1 print "episode not found. searching next season for S%sE%s" % (str(attempted_season).zfill(2), str(attempted_episode).zfill(2)) return self.__get_episode_after(name, attempted_season, attempted_episode, False) else: return data if (rdata['episode']['number'] == "%sx%s" % (str(attempted_season).zfill(2), str(attempted_episode).zfill(2))) and not rdata['episode']['airdate'] == '0000-00-00' and 'airtime' in rdata.keys(): # EPAPI doesnt have timestamps HOORAY airtime = rdata['airtime'].rsplit(' at ', 1)[1] timestring = "%s %s" % (rdata['episode']['airdate'], airtime) try: timestamp = int(datetime.datetime.strptime(timestring, '%Y-%m-%d %I:%M %p').strftime("%s")) except: print "malformed timestring: %s" % timestring return data data['timestamp'] = timestamp data['season'] = attempted_season data['episode'] = attempted_episode # data['timestamp'] = timestamp - TZ_OFFSET data['duration'] = int(rdata['runtime']) * 60 data['show_name'] = rdata['name'] else: # handle this better print "BOOP. Can't find the next episode of %s." % name return data else: print "ended" return False else: print "BEEP BEEEEEP TVRAGE IS DOWN(%s)" % response.status_code raise TVRageDownException() return data