document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import urllib
  2. import logging
  3.  
  4. import feedparser
  5. import requests
  6.  
  7. class TV:
  8.     def GET(self, show):
  9.         logging.debug(\'Show name: {0}\'.format(show))
  10.         show_escaped = urllib.quote_plus(show)
  11.         logging.debug(\'Show name for Apple: {0}\'.format(show_escaped))
  12.         apple_info = requests.get(\'https://itunes.apple.com/search?term={0}\'.format(show_escaped)).json()
  13.         try:
  14.             tv_episodes = filter(self.is_tv, apple_info[\'results\'])
  15.             tv_episodes_sorted_by_date = sorted(tv_episodes, key=self.sort_key)
  16.             latest_episode = tv_episodes_sorted_by_date[0]
  17.         except KeyError, e:
  18.             latest_episode = {}
  19.             latest_episode[\'trackCensoredName\'] = latest_episode[\'longDescription\'] = latest_episode[\'releaseDate\'] = \'Not available on iTunes\'
  20.            
  21.         logging.debug(\'Latest episode information: {0}\'.format(sorted(latest_episode.keys())))
  22.         fp = feedparser.parse(\'https://kat.cr/tv/?rss=1\')
  23.         torrents = []
  24.         for torrent in fp.entries:
  25.             ret = {}
  26.             name = torrent[\'title\']
  27.             matching_name = name.lower().encode(\'utf-8\')
  28.             matching_request = show.lower()
  29.             if matching_name.find(matching_request) != -1:
  30.                 logging.debug(\'Found torrent!\')
  31.                 torrent_data = len(torrents)+1
  32.                 if is_html():
  33.                     ret[\'torrent {0} name\'.format(torrent_data)] = \'<a href="{1}">{0}</a>\'.format(torrent[\'link\'], torrent[\'name\'])
  34.                 else:
  35.                     ret[\'torrent {0} name\'.format(torrent_data)] = name
  36.                 ret[\'torrent {0} magnet\'.format(torrent_data)] = torrent[\'torrent_magneturi\']
  37.                 ret[\'torrent {0} date\'.format(torrent_data)] = torrent[\'published\']
  38.                 if not is_html():
  39.                     ret[\'torrent {0} link\'.format(torrent_data)] = torrent[\'link\']
  40.                 ret[\'name\'] = latest_episode[\'trackCensoredName\']
  41.                 ret[\'episode description\'] = latest_episode[\'longDescription\']
  42.                 if not latest_episode.has_key(\'trackHdPrice\'):
  43.                     ret[\'itunes price\'] = latest_episode.get(\'trackPrice\')
  44.                     ret[\'itunes format\'] = \'SD\'
  45.                 else:
  46.                     ret[\'itunes price\'] = latest_episode.get(\'trackHdPrice\')
  47.                     ret[\'itunes format\'] = \'HD\'
  48.                 ret[\'air date\'] = latest_episode[\'releaseDate\']
  49.             if any([k.startswith(\'torrent\') for k in ret.keys()]):
  50.                 torrents.append(ret)
  51.         return torrents
  52.  
  53.     def is_tv(self, apple_stanza):
  54.         logging.debug(\'media kind: {0}\'.format(apple_stanza.get(\'kind\')))
  55.         return apple_stanza[\'artistViewUrl\'].find(\'tv-show\') > -1
  56.  
  57.     def sort_key(self, item):
  58.         time_key = item[\'releaseDate\']
  59.         timezone_less = time_key[:-1]
  60.         timeobj = datetime.datetime.strptime(timezone_less, \'%Y-%m-%dT%H:%M:%S\')
  61.         return timeobj
');