Private GIT

Skip to content
Snippets Groups Projects
Commit c6d2240a authored by Dustyn Gibson's avatar Dustyn Gibson
Browse files

Merge branch 'scene-exceptions-memory-hog' into develop

parents 1eaebaab 5ee5c003
Branches
Tags
No related merge requests found
...@@ -24,15 +24,14 @@ class InitialSchema(db.SchemaUpgrade): ...@@ -24,15 +24,14 @@ class InitialSchema(db.SchemaUpgrade):
return self.hasTable("db_version") return self.hasTable("db_version")
def execute(self): def execute(self):
queries = [ queries = [
("CREATE TABLE db_version (db_version INTEGER);",),
("CREATE TABLE lastUpdate (provider TEXT, time NUMERIC);",), ("CREATE TABLE lastUpdate (provider TEXT, time NUMERIC);",),
("CREATE TABLE lastSearch (provider TEXT, time NUMERIC);",), ("CREATE TABLE lastSearch (provider TEXT, time NUMERIC);",),
("CREATE TABLE scene_exceptions (exception_id INTEGER PRIMARY KEY, indexer_id INTEGER KEY, show_name TEXT, season NUMERIC DEFAULT -1, custom NUMERIC DEFAULT 0);",), ("CREATE TABLE scene_exceptions (exception_id INTEGER PRIMARY KEY, indexer_id INTEGER KEY, show_name TEXT, season NUMERIC DEFAULT -1, custom NUMERIC DEFAULT 0);",),
("CREATE TABLE scene_names (indexer_id INTEGER, name TEXT);",), ("CREATE TABLE scene_names (indexer_id INTEGER, name TEXT);",),
("CREATE TABLE network_timezones (network_name TEXT PRIMARY KEY, timezone TEXT);",), ("CREATE TABLE network_timezones (network_name TEXT PRIMARY KEY, timezone TEXT);",),
("CREATE TABLE scene_exceptions_refresh (list TEXT PRIMARY KEY, last_refreshed INTEGER);",), ("CREATE TABLE scene_exceptions_refresh (list TEXT PRIMARY KEY, last_refreshed INTEGER);",),
("CREATE TABLE db_version (db_version INTEGER);",),
("INSERT INTO db_version(db_version) VALUES (1);",), ("INSERT INTO db_version(db_version) VALUES (1);",),
] ]
for query in queries: for query in queries:
...@@ -48,14 +47,14 @@ class AddSceneExceptions(InitialSchema): ...@@ -48,14 +47,14 @@ class AddSceneExceptions(InitialSchema):
def execute(self): def execute(self):
self.connection.action( self.connection.action(
"CREATE TABLE scene_exceptions (exception_id INTEGER PRIMARY KEY, indexer_id INTEGER KEY, show_name TEXT)") "CREATE TABLE scene_exceptions (exception_id INTEGER PRIMARY KEY, indexer_id INTEGER KEY, show_name TEXT);")
class AddSceneNameCache(AddSceneExceptions): class AddSceneNameCache(AddSceneExceptions):
def test(self): def test(self):
return self.hasTable("scene_names") return self.hasTable("scene_names")
def execute(self): def execute(self):
self.connection.action("CREATE TABLE scene_names (indexer_id INTEGER, name TEXT)") self.connection.action("CREATE TABLE scene_names (indexer_id INTEGER, name TEXT);")
class AddNetworkTimezones(AddSceneNameCache): class AddNetworkTimezones(AddSceneNameCache):
...@@ -63,16 +62,16 @@ class AddNetworkTimezones(AddSceneNameCache): ...@@ -63,16 +62,16 @@ class AddNetworkTimezones(AddSceneNameCache):
return self.hasTable("network_timezones") return self.hasTable("network_timezones")
def execute(self): def execute(self):
self.connection.action("CREATE TABLE network_timezones (network_name TEXT PRIMARY KEY, timezone TEXT)") self.connection.action("CREATE TABLE network_timezones (network_name TEXT PRIMARY KEY, timezone TEXT);")
class AddLastSearch(AddNetworkTimezones): class AddLastSearch(AddNetworkTimezones):
def test(self): def test(self):
return self.hasTable("lastSearch") return self.hasTable("lastSearch")
def execute(self): def execute(self):
self.connection.action("CREATE TABLE lastSearch (provider TEXT, time NUMERIC)") self.connection.action("CREATE TABLE lastSearch (provider TEXT, time NUMERIC);")
class AddSceneExceptionsSeasons(AddSceneNameCache): class AddSceneExceptionsSeasons(AddLastSearch):
def test(self): def test(self):
return self.hasColumn("scene_exceptions", "season") return self.hasColumn("scene_exceptions", "season")
...@@ -92,4 +91,4 @@ class AddSceneExceptionsRefresh(AddSceneExceptionsCustom): ...@@ -92,4 +91,4 @@ class AddSceneExceptionsRefresh(AddSceneExceptionsCustom):
def execute(self): def execute(self):
self.connection.action( self.connection.action(
"CREATE TABLE scene_exceptions_refresh (list TEXT PRIMARY KEY, last_refreshed INTEGER)") "CREATE TABLE scene_exceptions_refresh (list TEXT PRIMARY KEY, last_refreshed INTEGER);")
...@@ -171,22 +171,22 @@ def update_network_dict(): ...@@ -171,22 +171,22 @@ def update_network_dict():
my_db = db.DBConnection('cache.db') my_db = db.DBConnection('cache.db')
network_list = dict(my_db.select('SELECT * FROM network_timezones')) network_list = dict(my_db.select('SELECT * FROM network_timezones;'))
queries = [] queries = []
for network, timezone in d.iteritems(): for network, timezone in d.iteritems():
existing = network_list.has_key(network) existing = network_list.has_key(network)
if not existing: if not existing:
queries.append(['INSERT OR IGNORE INTO network_timezones VALUES (?,?)', [network, timezone]]) queries.append(['INSERT OR IGNORE INTO network_timezones VALUES (?,?);', [network, timezone]])
elif network_list[network] is not timezone: elif network_list[network] is not timezone:
queries.append(['UPDATE OR IGNORE network_timezones SET timezone = ? WHERE network_name = ?', [timezone, network]]) queries.append(['UPDATE OR IGNORE network_timezones SET timezone = ? WHERE network_name = ?;', [timezone, network]])
if existing: if existing:
del network_list[network] del network_list[network]
if network_list: if network_list:
purged = list(x for x in network_list) purged = list(x for x in network_list)
queries.append(['DELETE FROM network_timezones WHERE network_name IN (%s)' % ','.join(['?'] * len(purged)), purged]) queries.append(['DELETE FROM network_timezones WHERE network_name IN (%s);' % ','.join(['?'] * len(purged)), purged])
if queries: if queries:
my_db.mass_action(queries) my_db.mass_action(queries)
...@@ -197,10 +197,10 @@ def update_network_dict(): ...@@ -197,10 +197,10 @@ def update_network_dict():
def load_network_dict(): def load_network_dict():
try: try:
my_db = db.DBConnection('cache.db') my_db = db.DBConnection('cache.db')
cur_network_list = my_db.select('SELECT * FROM network_timezones') cur_network_list = my_db.select('SELECT * FROM network_timezones;')
if cur_network_list is None or len(cur_network_list) < 1: if cur_network_list is None or len(cur_network_list) < 1:
update_network_dict() update_network_dict()
cur_network_list = my_db.select('SELECT * FROM network_timezones') cur_network_list = my_db.select('SELECT * FROM network_timezones;')
d = dict(cur_network_list) d = dict(cur_network_list)
except: except:
d = {} d = {}
......
...@@ -223,30 +223,22 @@ def retrieve_exceptions(): ...@@ -223,30 +223,22 @@ def retrieve_exceptions():
else: else:
exception_dict[anidb_ex] = anidb_exception_dict[anidb_ex] exception_dict[anidb_ex] = anidb_exception_dict[anidb_ex]
changed_exceptions = False queries = []
# write all the exceptions we got off the net into the database
myDB = db.DBConnection('cache.db') myDB = db.DBConnection('cache.db')
for cur_indexer_id in exception_dict: for cur_indexer_id in exception_dict:
sql_ex = myDB.select("SELECT * FROM scene_exceptions WHERE indexer_id = ?;", [cur_indexer_id])
# get a list of the existing exceptions for this ID existing_exceptions = [x["show_name"] for x in sql_ex]
existing_exceptions = [x["show_name"] for x in
myDB.select("SELECT * FROM scene_exceptions WHERE indexer_id = ?", [cur_indexer_id])]
if not cur_indexer_id in exception_dict: if not cur_indexer_id in exception_dict:
continue continue
for cur_exception_dict in exception_dict[cur_indexer_id]: for cur_exception_dict in exception_dict[cur_indexer_id]:
cur_exception, curSeason = cur_exception_dict.items()[0] for ex in cur_exception_dict.iteritems():
cur_exception, curSeason = ex
# if this exception isn't already in the DB then add it
if cur_exception not in existing_exceptions: if cur_exception not in existing_exceptions:
myDB.action("INSERT INTO scene_exceptions (indexer_id, show_name, season) VALUES (?,?,?)", queries.append(["INSERT OR IGNORE INTO scene_exceptions (indexer_id, show_name, season) VALUES (?,?,?);",
[cur_indexer_id, cur_exception, curSeason]) [cur_indexer_id, cur_exception, curSeason]])
changed_exceptions = True if queries:
myDB.mass_action(queries)
# since this could invalidate the results of the cache we clear it out after updating
if changed_exceptions:
logger.log(u"Updated scene exceptions", logger.DEBUG) logger.log(u"Updated scene exceptions", logger.DEBUG)
else: else:
logger.log(u"No scene exceptions update needed", logger.DEBUG) logger.log(u"No scene exceptions update needed", logger.DEBUG)
...@@ -316,7 +308,7 @@ def _xem_exceptions_fetcher(): ...@@ -316,7 +308,7 @@ def _xem_exceptions_fetcher():
if parsedJSON['result'] == 'failure': if parsedJSON['result'] == 'failure':
continue continue
for indexerid, names in parsedJSON['data'].items(): for indexerid, names in parsedJSON['data'].iteritems():
try: try:
xem_exception_dict[int(indexerid)] = names xem_exception_dict[int(indexerid)] = names
except Exception as e: except Exception as e:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment