Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import urllib
- import logging
- import feedparser
- import requests
- class TV:
- def GET(self, show):
- logging.debug('Show name: {0}'.format(show))
- show_escaped = urllib.quote_plus(show)
- logging.debug('Show name for Apple: {0}'.format(show_escaped))
- apple_info = requests.get('https://itunes.apple.com/search?term={0}'.format(show_escaped)).json()
- try:
- tv_episodes = filter(self.is_tv, apple_info['results'])
- tv_episodes_sorted_by_date = sorted(tv_episodes, key=self.sort_key)
- latest_episode = tv_episodes_sorted_by_date[0]
- except KeyError, e:
- latest_episode = {}
- latest_episode['trackCensoredName'] = latest_episode['longDescription'] = latest_episode['releaseDate'] = 'Not available on iTunes'
- logging.debug('Latest episode information: {0}'.format(sorted(latest_episode.keys())))
- fp = feedparser.parse('https://kat.cr/tv/?rss=1')
- torrents = []
- for torrent in fp.entries:
- ret = {}
- name = torrent['title']
- matching_name = name.lower().encode('utf-8')
- matching_request = show.lower()
- if matching_name.find(matching_request) != -1:
- logging.debug('Found torrent!')
- torrent_data = len(torrents)+1
- if is_html():
- ret['torrent {0} name'.format(torrent_data)] = '<a href="{1}">{0}</a>'.format(torrent['link'], torrent['name'])
- else:
- ret['torrent {0} name'.format(torrent_data)] = name
- ret['torrent {0} magnet'.format(torrent_data)] = torrent['torrent_magneturi']
- ret['torrent {0} date'.format(torrent_data)] = torrent['published']
- if not is_html():
- ret['torrent {0} link'.format(torrent_data)] = torrent['link']
- ret['name'] = latest_episode['trackCensoredName']
- ret['episode description'] = latest_episode['longDescription']
- if not latest_episode.has_key('trackHdPrice'):
- ret['itunes price'] = latest_episode.get('trackPrice')
- ret['itunes format'] = 'SD'
- else:
- ret['itunes price'] = latest_episode.get('trackHdPrice')
- ret['itunes format'] = 'HD'
- ret['air date'] = latest_episode['releaseDate']
- if any([k.startswith('torrent') for k in ret.keys()]):
- torrents.append(ret)
- return torrents
- def is_tv(self, apple_stanza):
- logging.debug('media kind: {0}'.format(apple_stanza.get('kind')))
- return apple_stanza['artistViewUrl'].find('tv-show') > -1
- def sort_key(self, item):
- time_key = item['releaseDate']
- timezone_less = time_key[:-1]
- timeobj = datetime.datetime.strptime(timezone_less, '%Y-%m-%dT%H:%M:%S')
- return timeobj
Advertisement
Add Comment
Please, Sign In to add comment