Commit 8b470cf0 authored by echel0n's avatar echel0n

Refactored storing last XEM refresh data to TVShows table, dropped XEM refresh table

parent 3ce11370
......@@ -33,7 +33,7 @@ class MainDBBase(SRDatabaseBase):
class MainDB(SRDatabase):
def __init__(self, db_type, db_prefix, db_host, db_port, db_username, db_password):
super(MainDB, self).__init__('main', 13, db_type, db_prefix, db_host, db_port, db_username, db_password)
super(MainDB, self).__init__('main', 14, db_type, db_prefix, db_host, db_port, db_username, db_password)
MainDBBase.metadata.create_all(self.engine)
for model in MainDBBase._decl_class_registry.values():
if hasattr(model, '__tablename__'):
......@@ -132,6 +132,7 @@ class MainDB(SRDatabase):
search_delay = Column(Integer, default=0)
scene_exceptions = Column(Text, default='')
last_scene_exceptions_refresh = Column(Integer, default=0)
last_xem_refresh = Column(Integer, default=0)
last_update = Column(Integer, default=datetime.datetime.now().toordinal())
last_refresh = Column(Integer, default=datetime.datetime.now().toordinal())
last_backlog_search = Column(Integer, default=0)
......@@ -209,13 +210,6 @@ class MainDB(SRDatabase):
plot = Column(Text)
last_update = Column(Integer, nullable=False)
class XEMRefresh(MainDBBase):
__tablename__ = 'xem_refresh'
indexer_id = Column(Integer, primary_key=True)
indexer = Column(Integer, primary_key=True)
last_refreshed = Column(Integer, nullable=False)
class SceneNumbering(MainDBBase):
__tablename__ = 'scene_numbering'
......
from sqlalchemy import *
def upgrade(migrate_engine):
meta = MetaData(bind=migrate_engine)
tv_shows = Table('tv_shows', meta, autoload=True)
if not hasattr(tv_shows.c, 'last_xem_refresh'):
last_xem_refresh = Column('last_xem_refresh', Integer, default=0)
last_xem_refresh.create(tv_shows)
xem_refresh = Table('xem_refresh', meta, autoload=True)
if xem_refresh is not None:
with migrate_engine.begin() as conn:
for row in migrate_engine.execute(xem_refresh.select()):
conn.execute(tv_shows.update().where(tv_shows.c.indexer_id == row.indexer_id).values(last_xem_refresh=row.last_refreshed))
xem_refresh.drop()
def downgrade(migrate_engine):
pass
......@@ -320,7 +320,7 @@ class NameParser(object):
new_episode_numbers.append(e)
new_season_numbers.append(s)
# need to do a quick sanity check heregex. It's possible that we now have episodes
# need to do a quick sanity check here. It's possible that we now have episodes
# from more than one season (by tvdb numbering), and this is just too much
# for sickrage, so we'd need to flag it.
new_season_numbers = list(set(new_season_numbers)) # remove duplicates
......
......@@ -468,39 +468,26 @@ def xem_refresh(indexer_id, indexer, force=False):
Refresh data from xem for a tv show
:param indexer_id: int
:param indexer: int
:param force: boolean
"""
if not indexer_id:
return
session = sickrage.app.main_db.session()
max_refresh_age_secs = 86400 # 1 day
indexer_id = int(indexer_id)
indexer = int(indexer)
MAX_REFRESH_AGE_SECS = 86400 # 1 day
try:
query = session.query(MainDB.XEMRefresh).filter_by(indexer_id=indexer_id).one()
last_refresh = query.last_refreshed
refresh = int(time.mktime(datetime.datetime.today().timetuple())) > last_refresh + MAX_REFRESH_AGE_SECS
except orm.exc.NoResultFound:
refresh = True
show_object = find_show(indexer_id, indexer)
if refresh or force:
if int(time.mktime(datetime.datetime.today().timetuple())) > show_object.last_xem_refresh + max_refresh_age_secs or force:
sickrage.app.log.debug('Looking up XEM scene mapping for show %s on %s' % (indexer_id, IndexerApi(indexer).name))
# mark refreshed
try:
query = session.query(MainDB.XEMRefresh).filter_by(indexer_id=indexer_id).one()
query.last_refreshed = int(time.mktime(datetime.datetime.today().timetuple()))
except orm.exc.NoResultFound:
session.add(MainDB.XEMRefresh(**{
'indexer': indexer,
'last_refreshed': int(time.mktime(datetime.datetime.today().timetuple())),
'indexer_id': indexer_id
}))
finally:
session.commit()
# mark xem refreshed
show_object.last_xem_refresh = int(time.mktime(datetime.datetime.today().timetuple()))
show_object.save()
try:
try:
......@@ -527,24 +514,23 @@ def xem_refresh(indexer_id, indexer, force=False):
sickrage.app.log.info('No XEM data for show "%s on %s"' % (indexer_id, IndexerApi(indexer).name,))
return
tv_show = find_show(indexer_id)
for entry in parsed_json['data']:
try:
tv_episode = tv_show.get_episode(season=entry[IndexerApi(indexer).config['xem_origin']]['season'],
episode=entry[IndexerApi(indexer).config['xem_origin']]['episode'])
episode_object = show_object.get_episode(season=entry[IndexerApi(indexer).config['xem_origin']]['season'],
episode=entry[IndexerApi(indexer).config['xem_origin']]['episode'])
except SiCKRAGETVEpisodeException:
continue
if 'scene' in entry:
tv_episode.scene_season = entry['scene']['season']
tv_episode.scene_episode = entry['scene']['episode']
tv_episode.scene_absolute_number = entry['scene']['absolute']
episode_object.scene_season = entry['scene']['season']
episode_object.scene_episode = entry['scene']['episode']
episode_object.scene_absolute_number = entry['scene']['absolute']
if 'scene_2' in entry: # for doubles
tv_episode.scene_season = entry['scene_2']['season']
tv_episode.scene_episode = entry['scene_2']['episode']
tv_episode.scene_absolute_number = entry['scene_2']['absolute']
episode_object.scene_season = entry['scene_2']['season']
episode_object.scene_episode = entry['scene_2']['episode']
episode_object.scene_absolute_number = entry['scene_2']['absolute']
tv_episode.save()
episode_object.save()
except Exception as e:
sickrage.app.log.debug("Exception while refreshing XEM data for show {} on {}: {}".format(indexer_id, IndexerApi(indexer).name, e))
sickrage.app.log.debug(traceback.format_exc())
......
......@@ -354,6 +354,14 @@ class TVShow(object):
def last_scene_exceptions_refresh(self, value):
self._data_local['last_scene_exceptions_refresh'] = value
@property
def last_xem_refresh(self):
return self._data_local['last_xem_refresh']
@last_xem_refresh.setter
def last_xem_refresh(self, value):
self._data_local['last_xem_refresh'] = value
@property
def episodes(self):
if not self._episodes:
......@@ -1034,7 +1042,6 @@ class TVShow(object):
session.query(MainDB.TVShow).filter_by(indexer_id=self.indexer_id).delete()
session.query(MainDB.TVEpisode).filter_by(showid=self.indexer_id).delete()
session.query(MainDB.IMDbInfo).filter_by(indexer_id=self.indexer_id).delete()
session.query(MainDB.XEMRefresh).filter_by(indexer_id=self.indexer_id).delete()
session.query(MainDB.SceneNumbering).filter_by(indexer_id=self.indexer_id).delete()
self.save()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment