<commit_before>import logging
from flexget.plugin import register_plugin, internet
from flexget.manager import Base, Session
from flexget.utils.tools import urlopener
from BeautifulSoup import BeautifulStoneSoup
from sqlalchemy import Column, Integer, Unicode, DateTime, String
from datetime import datetime, timedelta
import urllib2
from flexget.plugins.filter_series import FilterSeriesBase

log = logging.getLogger('thetvdb_favorites')


class ThetvdbFavorites(Base):

    __tablename__ = 'thetvdb_favorites'

    id = Column(Integer, primary_key=True)
    account_id = Column(String, index=True)
    series_name = Column(Unicode)
    added = Column(DateTime)

    def __init__(self, account_id, series_name):
        self.series_name = series_name
        self.account_id = account_id
        self.added = datetime.now()

    def __repr__(self):
        return '<series_favorites(account_id=%s, series_name=%s)>' % (self.account_id, self.series_name)


class FilterThetvdbFavorites(FilterSeriesBase):
    """
        Creates a series config containing all your thetvdb.com favorites

        Example:

        thetvdb_favorites:
          account_id: 23098230
    """

    def validator(self):
        from flexget import validator
        root = validator.factory('dict')
        root.accept('text', key='account_id', required=True)
        root.accept('text', key='series_group')
        root.accept('boolean', key='strip_dates')
        self.build_options_validator(root)
        return root

    @internet(log)
    def on_process_start(self, feed):
        config = feed.config.get('thetvdb_favorites')
        account_id = str(config['account_id'])
        session = Session()
        cache = session.query(ThetvdbFavorites).filter(ThetvdbFavorites.account_id == account_id)
        if cache.count() and cache.first().added > datetime.now() - timedelta(minutes=1):
            log.debug('Using cached thetvdb favorites')
        else:
            log.debug('Updating favorites from thetvdb.com')
            try:
                url = 'http://thetvdb.com/api/User_Favorites.php?accountid=%s' % account_id
                log.debug('requesting %s' % url)
                data = BeautifulStoneSoup(urlopener(url, log))
                favorite_ids = []
                for i in data.favorites.findAll("series", recursive=False):
                    favorite_ids.append(int(i.string))
                # We found new data, remove old cached data
                items = []
                for fid in favorite_ids:
                    log.debug('Looking up series info for %s' % str(fid))
                    data = BeautifulStoneSoup(urlopener("http://thetvdb.com/data/series/%s/" % str(fid), log), \
                        convertEntities=BeautifulStoneSoup.HTML_ENTITIES)
                    items.append(ThetvdbFavorites(account_id, data.series.seriesname.string))
            except (urllib2.URLError, IOError, AttributeError):
                import traceback
                # If there are errors getting the favorites or parsing the xml, fall back on cache
                log.error('Error retrieving favorites from thetvdb, using cache.')
                log.debug(traceback.format_exc())
            else:
                # Successfully updated from tvdb, update the database
                log.debug('Successfully updated favorites from thetvdb.com')
                cache.delete()
                session.add_all(items)
                session.commit()
                cache = session.query(ThetvdbFavorites).filter(ThetvdbFavorites.account_id == account_id)<commit_msg>fixed a bug on a very select few series, retrieval from thetvdb via XML.<commit_after>import logging
from flexget.plugin import register_plugin, internet
from flexget.manager import Base, Session
from flexget.utils.tools import urlopener
from BeautifulSoup import BeautifulStoneSoup
from sqlalchemy import Column, Integer, Unicode, DateTime, String
from datetime import datetime, timedelta
import urllib2
from flexget.plugins.filter_series import FilterSeriesBase

log = logging.getLogger('thetvdb_favorites')


class ThetvdbFavorites(Base):

    __tablename__ = 'thetvdb_favorites'

    id = Column(Integer, primary_key=True)
    account_id = Column(String, index=True)
    series_name = Column(Unicode)
    added = Column(DateTime)

    def __init__(self, account_id, series_name):
        self.series_name = series_name
        self.account_id = account_id
        self.added = datetime.now()

    def __repr__(self):
        return '<series_favorites(account_id=%s, series_name=%s)>' % (self.account_id, self.series_name)


class FilterThetvdbFavorites(FilterSeriesBase):
    """
        Creates a series config containing all your thetvdb.com favorites

        Example:

        thetvdb_favorites:
          account_id: 23098230
    """

    def validator(self):
        from flexget import validator
        root = validator.factory('dict')
        root.accept('text', key='account_id', required=True)
        root.accept('text', key='series_group')
        root.accept('boolean', key='strip_dates')
        self.build_options_validator(root)
        return root

    @internet(log)
    def on_process_start(self, feed):
        config = feed.config.get('thetvdb_favorites')
        account_id = str(config['account_id'])
        session = Session()
        cache = session.query(ThetvdbFavorites).filter(ThetvdbFavorites.account_id == account_id)
        if cache.count() and cache.first().added > datetime.now() - timedelta(minutes=1):
            log.debug('Using cached thetvdb favorites')
        else:
            log.debug('Updating favorites from thetvdb.com')
            try:
                url = 'http://thetvdb.com/api/User_Favorites.php?accountid=%s' % account_id
                log.debug('requesting %s' % url)
                data = BeautifulStoneSoup(urlopener(url, log))
                favorite_ids = []
                for i in data.favorites.findAll("series", recursive=False):
                    favorite_ids.append(int(i.string))
                # We found new data, remove old cached data
                items = []
                for fid in favorite_ids:
                    log.debug('Looking up series info for %s' % str(fid))
                    data = BeautifulStoneSoup(urlopener("http://thetvdb.com//data/series/%s/" % str(fid), log), \
                        convertEntities=BeautifulStoneSoup.HTML_ENTITIES)
                    items.append(ThetvdbFavorites(account_id, data.series.seriesname.string))
            except (urllib2.URLError, IOError, AttributeError):
                import traceback
                # If there are errors getting the favorites or parsing the xml, fall back on cache
                log.error('Error retrieving favorites from thetvdb, using cache.')
                log.debug(traceback.format_exc())
            else:
                # Successfully updated from tvdb, update the database
                log.debug('Successfully updated favorites from thetvdb.com')
                cache.delete()
                session.add_all(items)
                session.commit()
                cache = session.query(ThetvdbFavorites).filter(ThetvdbFavorites.account_id == account_id)
{'fraction_non_alphanumeric': 0.0935599778883361, 'fraction_numerical': 0.0030403537866224434, 'mean_word_length': 2.5406066536203524, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5206260', 'n_tokens_mistral': 2078, 'n_tokens_neox': 1983, 'n_words': 462}