def __init__(self, config): self.config = config if self.config.get('account') and not self.config.get('username'): self.config['username'] = '******' self.session = get_session(self.config.get('account')) # Lists may not have modified results if modified then accessed in quick succession. self.session.add_domain_limiter(TimedLimiter('trakt.tv', '2 seconds')) self._items = None
def on_task_input(self, task, config): start_date = datetime.datetime.now().date() + datetime.timedelta( days=config['start_day']) log.debug('Start date for calendar: %s, end date: %s', start_date, start_date + datetime.timedelta(days=config['days'])) url = get_api_url('calendars', 'my' if config.get('account') else 'all', 'shows', start_date, config['days']) try: results = get_session(config.get('account')).get(url, params={ 'extended': 'full' }).json() log.debug('Found %s calendar entries', len(results)) except RequestException as e: raise plugin.PluginError( 'Error while fetching calendar: {0}'.format(e)) entries = set() for result in results: e = Entry() e.update_using_map(self.series_map, result['show']) if config['type'] == 'episodes': e.update_using_map(self.episode_map, result['episode']) e['title'] = e['trakt_series_name'] if not config['strip_dates']: e['title'] = '{0} ({1})'.format(e['title'], e['trakt_series_year']) e['url'] = e['trakt_series_url'] if config['type'] == 'episodes': e['title'] = '{0} S{1:02d}E{2:02d}'.format( e['title'], e['trakt_season'], e['trakt_episode']) e['url'] = '{0}/seasons/{1}/episodes/{2}'.format( e['url'], e['trakt_season'], e['trakt_episode']) entries.add(e) return list(entries)
def on_task_input(self, task, config): start_date = datetime.datetime.now().date() + datetime.timedelta(days=config['start_day']) log.debug('Start date for calendar: %s, end date: %s', start_date, start_date + datetime.timedelta(days=config['days'])) url = get_api_url('calendars', 'my' if config.get('account') else 'all', 'shows', start_date, config['days']) try: results = get_session(config.get('account')).get(url, params={'extended': 'full'}).json() log.debug('Found %s calendar entries', len(results)) except RequestException as e: raise plugin.PluginError('Error while fetching calendar: {0}'.format(e)) entries = set() for result in results: e = Entry() e.update_using_map(self.series_map, result['show']) if config['type'] == 'episodes': e.update_using_map(self.episode_map, result['episode']) e['title'] = e['trakt_series_name'] if not config['strip_dates']: e['title'] = '{0} ({1})'.format(e['title'], e['trakt_series_year']) e['url'] = e['trakt_series_url'] if config['type'] == 'episodes': e['title'] = '{0} S{1:02d}E{2:02d}'.format(e['title'], e['trakt_season'], e['trakt_episode']) e['url'] = '{0}/seasons/{1}/episodes/{2}'.format(e['url'], e['trakt_season'], e['trakt_episode']) entries.add(e) return list(entries)
def on_task_input(self, task, config): if config.get('account') and not config.get('username'): config['username'] = '******' session = get_session(account=config.get('account')) listed_series = {} if config.get('list'): args = ('users', config['username']) if config['list'] in ['collection', 'watchlist', 'watched']: args += (config['list'], 'shows') else: args += ('lists', make_list_slug(config['list']), 'items') try: data = session.get(get_api_url(args)).json() except RequestException as e: raise plugin.PluginError('Unable to get trakt list `%s`: %s' % (config['list'], e)) if not data: log.warning('The list "%s" is empty.' % config['list']) return for item in data: if item.get('show'): if not item['show']['title']: # Seems we can get entries with a blank show title sometimes log.warning( 'Found trakt list show with no series name.') continue trakt_id = item['show']['ids']['trakt'] listed_series[trakt_id] = { 'series_name': '%s (%s)' % (item['show']['title'], item['show']['year']), 'trakt_id': trakt_id, 'trakt_series_name': item['show']['title'], 'trakt_series_year': item['show']['year'], 'trakt_list': config.get('list') } if item['show']['ids'].get('tvdb'): listed_series[trakt_id]['tvdb_id'] = item['show'][ 'ids']['tvdb'] if item['show']['ids'].get('tvrage'): listed_series[trakt_id]['tvrage_id'] = item['show'][ 'ids']['tvrage'] if item['show']['ids'].get('imdb'): listed_series[trakt_id]['imdb_id'] = item['show'][ 'ids']['imdb'] if item['show']['ids'].get('tmdb'): listed_series[trakt_id]['tmdb_id'] = item['show'][ 'ids']['tmdb'] if item['show']['ids'].get('slug'): listed_series[trakt_id]['trakt_slug'] = item['show'][ 'ids']['slug'] context = config['context'] if context == 'collected': context = 'collection' entries = [] for trakt_id, fields in listed_series.items(): url = get_api_url('shows', trakt_id, 'progress', context) try: data = session.get(url).json() except RequestException as e: raise plugin.PluginError( 'An error has occurred looking up: Trakt_id: %s Error: %s' % (trakt_id, e)) if config['position'] == 'next' and data.get('next_episode'): # If the next episode is already in the trakt database, we'll get it here eps = data['next_episode']['season'] epn = data['next_episode']['number'] else: # If we need last ep, or next_episode was not provided, search for last ep for seas in reversed(data['seasons']): # Find the first season with collected/watched episodes if seas['completed'] > 0: eps = seas['number'] # Pick the highest collected/watched episode epn = max(item['number'] for item in seas['episodes'] if item['completed']) # If we are in next episode mode, we have to increment this number if config['position'] == 'next': if seas['completed'] >= seas['aired']: # TODO: next_episode doesn't count unaired episodes right now, this will skip to next # season too early when there are episodes left to air this season. eps += 1 epn = 1 else: epn += 1 break else: if config['position'] == 'next': eps = epn = 1 else: # There were no watched/collected episodes, nothing to emit in 'last' mode continue if eps and epn: if config.get('strip_dates'): # remove year from end of series_name if present fields['series_name'] = re.sub(r'\s+\(\d{4}\)$', '', fields['series_name']) entry = self.make_entry(fields, eps, epn) entries.append(entry) return entries
def on_task_input(self, task, config): if config.get('account') and not config.get('username'): config['username'] = '******' session = get_session(account=config.get('account')) listed_series = {} args = ('users', config['username']) if config['list'] in ['collection', 'watchlist', 'watched']: args += (config['list'], 'shows') else: args += ('lists', make_list_slug(config['list']), 'items') try: data = session.get(get_api_url(args)).json() except RequestException as e: raise plugin.PluginError('Unable to get trakt list `%s`: %s' % (config['list'], e)) if not data: log.warning('The list "%s" is empty.', config['list']) return for item in data: if item.get('show'): if not item['show']['title']: # Seems we can get entries with a blank show title sometimes log.warning('Found trakt list show with no series name.') continue ids = item['show']['ids'] trakt_id = ids['trakt'] listed_series[trakt_id] = { 'series_name': '%s (%s)' % (item['show']['title'], item['show']['year']), 'trakt_id': trakt_id, 'trakt_series_name': item['show']['title'], 'trakt_series_year': item['show']['year'], 'trakt_list': config.get('list') } for id_name, id_value in ids.items(): entry_field_name = 'trakt_slug' if id_name == 'slug' else id_name # rename slug to trakt_slug listed_series[trakt_id][entry_field_name] = id_value context = 'collection' if config['context'] == 'collected' else config['context'] entries = [] for trakt_id, fields in listed_series.items(): url = get_api_url('shows', trakt_id, 'progress', context) try: data = session.get(url).json() except RequestException as e: raise plugin.PluginError('An error has occurred looking up: Trakt_id: %s Error: %s' % (trakt_id, e)) if config['position'] == 'next' and data.get('next_episode'): # If the next episode is already in the trakt database, we'll get it here season_number = data['next_episode']['season'] episode_number = data['next_episode']['number'] else: # If we need last ep, or next_episode was not provided, search for last ep for season in reversed(data['seasons']): # Find the first season with collected/watched episodes if not season['completed']: continue season_number = season['number'] # Pick the highest collected/watched episode episode_number = max(item['number'] for item in season['episodes'] if item['completed']) # If we are in next episode mode, we have to increment this number if config['position'] == 'next': if season['completed'] >= season['aired']: # TODO: next_episode doesn't count unaired episodes right now, this will skip to next # season too early when there are episodes left to air this season. season_number += 1 episode_number = 1 break else: if config['position'] != 'next': # There were no watched/collected episodes, nothing to emit in 'last' mode continue season_number = episode_number = 1 if season_number and episode_number: if config.get('strip_dates'): # remove year from end of series_name if present fields['series_name'] = re.sub(r'\s+\(\d{4}\)$', '', fields['series_name']) entry = self.make_entry(fields, season_number, episode_number) entries.append(entry) return entries