Exemplo n.º 1
0
    def on_task_output(self, task, config, session=None):

        tvdb_favorites = self.get_favs(config['username'], session=session)

        for entry in task.accepted:
            if entry.get('tvdb_id'):
                tvdb_id = entry['tvdb_id']
                series_name = entry.get('series_name', tvdb_id)

                if tvdb_id not in tvdb_favorites.series_ids:
                    self.log.verbose('Not a fav %s (%s), skipping...' %
                                     (series_name, tvdb_id))
                    continue

                try:
                    req = TVDBRequest(username=config['username'],
                                      account_id=config['account_id'])
                    req.delete('/user/favorites/%s' % tvdb_id)
                except RequestException as e:
                    # 409 is thrown if it was not in the favs
                    if e.response.status_code != 409:
                        entry.fail(
                            'Error deleting %s from tvdb favorites: %s' %
                            (tvdb_id, str(e)))
                        continue

                tvdb_favorites.series_ids.remove(tvdb_id)
Exemplo n.º 2
0
 def items(self):
     if self._items is None:
         try:
             req = TVDBRequest(
                 username=self.config['username'],
                 account_id=self.config['account_id']).get('user/favorites')
             series_ids = [
                 int(f_id) for f_id in req['favorites'] if f_id != ''
             ]
         except RequestException as e:
             raise PluginError(
                 'Error retrieving favorites from thetvdb: %s' % str(e))
         self._items = []
         for series_id in series_ids:
             # Lookup the series name from the id
             try:
                 series = lookup_series(tvdb_id=series_id)
             except LookupError as e:
                 log.error('Error looking up %s from thetvdb: %s' %
                           (series_id, e.args[0]))
             else:
                 series_name = series.name
                 if self.config.get('strip_dates'):
                     # Remove year from end of series name if present
                     series_name, _ = split_title_year(series_name)
                 entry = Entry()
                 entry['title'] = entry['series_name'] = series_name
                 entry[
                     'url'] = 'http://thetvdb.com/index.php?tab=series&id={}'.format(
                         str(series.id))
                 entry['tvdb_id'] = str(series.id)
                 self._items.append(entry)
     return self._items
Exemplo n.º 3
0
 def add(self, entry):
     if not entry.get('tvdb_id'):
         log.verbose(
             'entry does not have `tvdb_id`, cannot add to list. Consider using a lookup plugin`'
         )
         return
     try:
         TVDBRequest(username=self.config['username'],
                     account_id=self.config['account_id']).put(
                         'user/favorites/{}'.format(entry['tvdb_id']))
     except RequestException as e:
         log.error('Could not add tvdb_id {} to favourites list: {}'.format(
             entry['tvdb_id'], e))
     self.invalidate_cache()
Exemplo n.º 4
0
    def on_task_output(self, task, config, session=None):

        tvdb_favorites = self.get_favs(config['username'], session=session)

        for entry in task.accepted:
            if entry.get('tvdb_id'):
                tvdb_id = entry['tvdb_id']
                series_name = entry.get('series_name', tvdb_id)

                if tvdb_id not in tvdb_favorites.series_ids:
                    self.log.verbose('Not a fav %s (%s), skipping...' % (series_name, tvdb_id))
                    continue

                try:
                    req = TVDBRequest(username=config['username'], account_id=config['account_id'])
                    req.delete('/user/favorites/%s' % tvdb_id)
                except RequestException as e:
                    # 409 is thrown if it was not in the favs
                    if e.response.status_code != 409:
                        entry.fail('Error deleting %s from tvdb favorites: %s' % (tvdb_id, str(e)))
                        continue

                tvdb_favorites.series_ids.remove(tvdb_id)
Exemplo n.º 5
0
    def on_task_output(self, task, config, session=None):

        tvdb_favorites = self.get_favs(config['username'], session)

        for entry in task.accepted:
            if entry.get('tvdb_id'):
                tvdb_id = entry['tvdb_id']
                series_name = entry.get('series_name', tvdb_id)

                if tvdb_id in tvdb_favorites.series_ids:
                    self.log.verbose('Already a fav %s (%s), skipping...' % (series_name, tvdb_id))
                    continue

                try:
                    req = TVDBRequest(username=config['username'], password=config['password'])
                    req.put('/user/favorites/%s' % tvdb_id)
                except RequestException as e:
                    # 409 is thrown if it was already in the favs
                    if e.response.status_code != 409:
                        entry.fail('Error adding %s to tvdb favorites: %s' % (tvdb_id, str(e)))
                        continue

                tvdb_favorites.series_ids.append(tvdb_id)
Exemplo n.º 6
0
    def on_task_input(self, task, config, session=None):

        # Get the cache for this user
        user_favorites = session.query(TVDBUserFavorite).filter(
            TVDBUserFavorite.username == config['username']).first()

        if not user_favorites:
            user_favorites = TVDBUserFavorite(username=config['username'])
            session.add(user_favorites)

        if user_favorites.updated and user_favorites.updated > datetime.now(
        ) - timedelta(minutes=10):
            log.debug(
                'Using cached thetvdb favorite series information for account %s'
                % config['username'])
        else:
            try:
                req = TVDBRequest(
                    username=config['username'],
                    account_id=config['account_id']).get('user/favorites')
                user_favorites.series_ids = [
                    int(f_id) for f_id in req['favorites']
                ]
            except RequestException as e:
                log.error('Error retrieving favorites from thetvdb: %s' %
                          str(e))

            # Successfully updated from tvdb, update the database
            user_favorites.updated = datetime.now()

        # Construct list of entries with our series names
        entries = []
        for series_id in user_favorites.series_ids:
            # Lookup the series name from the id
            try:
                series = lookup_series(tvdb_id=series_id)
            except LookupError as e:
                log.error('Error looking up %s from thetvdb: %s' %
                          (series_id, e.args[0]))
            else:
                series_name = series.name
                if config.get('strip_dates'):
                    # Remove year from end of series name if present
                    series_name = re.sub(r'\s+\(\d{4}\)$', '', series_name)
                entries.append(Entry(series_name, '', tvdb_id=series.id))
        return entries