'Store finished / downloaded entries at exit'

def on_task_exit(self, task, config):

    if (not rss2gen):
        raise plugin.PluginWarning(u'plugin    make_rss    requires    PyRSS2Gen    library.')
    config = self.prepare_config(config)
    if (not config[u'history']):
        log.debug(u'disabling    history')
        for item in task.session.query(RSSEntry).filter((RSSEntry.file == config[u'file'])).all():
            task.session.delete(item)
    for entry in task.accepted:
        rss = RSSEntry()
        try:
            rss.title = entry.render(config[u'title'])
        except RenderError as e:
            log.error(u'Error    rendering    jinja    title    for    `%s`    falling    back    to    entry    title:    %s', entry[u'title'], e)
            rss.title = entry[u'title']
        for field in config[u'link']:
            if (field in entry):
                rss.link = entry[field]
                break
        try:
            template = get_template(config[u'template'], scope=u'task')
        except ValueError as e:
            raise plugin.PluginError((u'Invalid    template    specified:    %s' % e))
        try:
            rss.description = render_from_entry(template, entry)
        except RenderError as e:
            log.error(u'Error    while    rendering    entry    %s,    falling    back    to    plain    title:    %s', entry, e)
            rss.description = (entry[u'title'] + u'    -    (Render    Error)')
        rss.file = config[u'file']
        log.debug(u'Saving    %s    into    rss    database', entry[u'title'])
        task.session.add(rss)
    if (not rss2gen):
        return
    if task.options.learn:
        return
    db_items = task.session.query(RSSEntry).filter((RSSEntry.file == config[u'file'])).order_by(RSSEntry.published.desc()).all()
    rss_items = []
    for db_item in db_items:
        add = True
        if (config[u'items'] != (-1)):
            if (len(rss_items) > config[u'items']):
                add = False
        if (config[u'days'] != (-1)):
            if ((datetime.datetime.today() - datetime.timedelta(days=config[u'days'])) > db_item.published):
                add = False
        if add:
            hasher = hashlib.sha1()
            hasher.update(db_item.title.encode(u'utf8'))
            hasher.update(db_item.description.encode(u'utf8'))
            hasher.update(db_item.link.encode(u'utf8'))
            guid = base64.urlsafe_b64encode(hasher.digest()).decode(u'ascii')
            guid = PyRSS2Gen.Guid(guid, isPermaLink=False)
            gen = {u'title': db_item.title, u'description': db_item.description, u'link': db_item.link, u'pubDate': db_item.published, u'guid': guid}
            log.trace(u'Adding    %s    into    rss    %s', gen[u'title'], config[u'file'])
            rss_items.append(PyRSS2Gen.RSSItem(**gen))
        else:
            task.session.delete(db_item)
    rss = PyRSS2Gen.RSS2(title=u'FlexGet', link=config.get(u'rsslink', u'http://flexget.com'), description=u'FlexGet    generated    RSS    feed', lastBuildDate=datetime.datetime.utcnow(), items=rss_items)
    if task.options.test:
        log.info(u'Would    write    rss    file    with    %d    entries.', len(rss_items))
        return
    fn = os.path.expanduser(config[u'file'])
    with io.open(fn, u'wb') as file:
        try:
            log.verbose(u'Writing    output    rss    to    %s', fn)
            rss.write_xml(file, encoding=config[u'encoding'])
        except LookupError:
            log.critical(u'Unknown    encoding    %s', config[u'encoding'])
            return
        except IOError:
            log.critical(u'Unable    to    write    %s', fn)
            return
