'Filter series'

def on_task_filter(self, task, config):

    config = self.prepare_config(config)
    found_series = {}
    for entry in task.entries:
        if (entry.get(u'series_name') and (entry.get(u'series_id') is not None) and entry.get(u'series_parser')):
            found_series.setdefault(entry[u'series_name'], []).append(entry)
    for series_item in config:
        with Session() as session:
            (series_name, series_config) = list(series_item.items())[0]
            if series_config.get(u'parse_only'):
                log.debug(u'Skipping    filtering    of    series    `%s`    because    of    parse_only', series_name)
                continue
            series_name = str(series_name)
            db_series = session.query(Series).filter((Series.name == series_name)).first()
            if (not db_series):
                log.debug(u'adding    series    `%s`    into    db', series_name)
                db_series = Series()
                db_series.name = series_name
                db_series.identified_by = series_config.get(u'identified_by', u'auto')
                session.add(db_series)
                log.debug(u'->    added    `%s`', db_series)
                session.flush()
                alts = series_config.get(u'alternate_name', [])
                if (not isinstance(alts, list)):
                    alts = [alts]
                for alt in alts:
                    _add_alt_name(alt, db_series, series_name, session)
            if (series_name not in found_series):
                continue
            series_entries = {}
            for entry in found_series[series_name]:
                releases = store_parser(session, entry[u'series_parser'], series=db_series, quality=entry.get(u'quality'))
                entry[u'series_releases'] = [r.id for r in releases]
                if hasattr(releases[0], u'episode'):
                    entity = releases[0].episode
                else:
                    entity = releases[0].season
                series_entries.setdefault(entity, []).append(entry)
            if (not series_entries):
                log.trace(u'No    entries    found    for    `%s`    this    run.', series_name)
                continue
            if (series_config.get(u'identified_by', u'auto') != u'auto'):
                db_series.identified_by = series_config[u'identified_by']
            if ((not db_series.identified_by) or (db_series.identified_by == u'auto')):
                db_series.identified_by = auto_identified_by(db_series)
                log.debug(u'identified_by    set    to    `%s`    based    on    series    history', db_series.identified_by)
            log.trace(u'series_name:    `%s`,    series_config:    `%s`', series_name, series_config)
            start_time = time.clock()
            self.process_series(task, series_entries, series_config)
            took = (time.clock() - start_time)
            log.trace(u'processing    `%s`    took    %s', series_name, took)
