From 9b3fd0b7a91e3dffe6ccf49025e4378279e3e3fe Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 16 Nov 2014 23:00:05 +0800 Subject: [PATCH] Add TVRage network name standardization --- CHANGES.md | 1 + lib/tvrage_api/tvrage_api.py | 5 ++- sickbeard/databases/cache_db.py | 13 ++++++- sickbeard/helpers.py | 2 ++ sickbeard/network_timezones.py | 60 +++++++++++++++++++++++++++++++++ 5 files changed, 79 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index ae6d5c93..b79578fb 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,7 @@ ### 0.x.x (2015-xx-xx xx:xx:xx UTC) * Fix slow database operations (port from midgetspy/sickbeard) +* Add TVRage network name standardization [develop changelog] diff --git a/lib/tvrage_api/tvrage_api.py b/lib/tvrage_api/tvrage_api.py index 95a49811..b5347bdf 100644 --- a/lib/tvrage_api/tvrage_api.py +++ b/lib/tvrage_api/tvrage_api.py @@ -22,6 +22,7 @@ import datetime as dt import requests import requests.exceptions import xmltodict +from sickbeard.network_timezones import standardize_network try: import xml.etree.cElementTree as ElementTree @@ -443,7 +444,9 @@ class TVRage: if value: if isinstance(value, dict): if key == 'network': - value = value['#text'] + network = value['#text'] + country = value['@country'] + value = standardize_network(network, country) if key == 'genre': value = value['genre'] if not value: diff --git a/sickbeard/databases/cache_db.py b/sickbeard/databases/cache_db.py index 815f2660..37f60cf7 100644 --- a/sickbeard/databases/cache_db.py +++ b/sickbeard/databases/cache_db.py @@ -88,4 +88,15 @@ class AddSceneExceptionsRefresh(AddSceneExceptionsCustom): def execute(self): self.connection.action( - "CREATE TABLE scene_exceptions_refresh (list TEXT PRIMARY KEY, last_refreshed INTEGER)") \ No newline at end of file + "CREATE TABLE scene_exceptions_refresh (list TEXT PRIMARY KEY, last_refreshed INTEGER)") + + +class AddNetworkConversions(AddSceneExceptionsRefresh): + def test(self): + return self.hasTable('network_conversions') + + def execute(self): + self.connection.action('CREATE TABLE network_conversions (tvdb_network TEXT PRIMARY KEY, tvrage_network TEXT,' + ' tvrage_country TEXT)') + + self.connection.action('CREATE INDEX tvrage_idx on network_conversions (tvrage_network, tvrage_country)') diff --git a/sickbeard/helpers.py b/sickbeard/helpers.py index 7f29e943..22831021 100644 --- a/sickbeard/helpers.py +++ b/sickbeard/helpers.py @@ -1432,6 +1432,8 @@ def get_size(start_path='.'): def remove_article(text=''): return re.sub(r'(?i)^(?:(?:A(?!\s+to)n?)|The)\s(\w)', r'\1', text) +def build_dict(seq, key): + return dict((d[key], dict(d, index=index)) for (index, d) in enumerate(seq)) def client_host(server_host): '''Extracted from cherrypy libs diff --git a/sickbeard/network_timezones.py b/sickbeard/network_timezones.py index a047fc5a..acc31a8f 100644 --- a/sickbeard/network_timezones.py +++ b/sickbeard/network_timezones.py @@ -140,6 +140,7 @@ def _update_zoneinfo(): def update_network_dict(): _remove_old_zoneinfo() _update_zoneinfo() + load_network_conversions() d = {} @@ -278,3 +279,62 @@ def test_timeformat(t): return False else: return True + + +def standardize_network(network, country): + myDB = db.DBConnection('cache.db') + sqlResults = myDB.select('SELECT * FROM network_conversions WHERE tvrage_network = ? and tvrage_country = ?', + [network, country]) + if len(sqlResults) == 1: + return sqlResults[0]['tvdb_network'] + else: + return network + + +def load_network_conversions(): + + conversions = [] + + # network conversions are stored on github pages + url = 'https://raw.githubusercontent.com/prinz23/sg_network_conversions/master/conversions.txt' + + url_data = helpers.getURL(url) + if url_data is None: + # When urlData is None, trouble connecting to github + logger.log(u'Updating network conversions failed, this can happen from time to time. URL: %s' % url, logger.WARNING) + return + + try: + for line in url_data.splitlines(): + (tvdb_network, tvrage_network, tvrage_country) = line.decode('utf-8').strip().rsplit(u'::', 2) + if not (tvdb_network and tvrage_network and tvrage_country): + continue + conversions.append({'tvdb_network': tvdb_network, 'tvrage_network': tvrage_network, 'tvrage_country': tvrage_country}) + except (IOError, OSError): + pass + + my_db = db.DBConnection('cache.db') + + old_d = my_db.select('SELECT * FROM network_conversions') + old_d = helpers.build_dict(old_d, 'tvdb_network') + + # list of sql commands to update the network_conversions table + cl = [] + + for n_w in conversions: + cl.append(['INSERT OR REPLACE INTO network_conversions (tvdb_network, tvrage_network, tvrage_country)' + 'VALUES (?,?,?)', [n_w['tvdb_network'], n_w['tvrage_network'], n_w['tvrage_country']]]) + try: + del old_d[n_w['tvdb_network']] + except: + pass + + # remove deleted records + if len(old_d) > 0: + old_items = list(va for va in old_d) + cl.append(['DELETE FROM network_conversions WHERE tvdb_network' + ' IN (%s)' % ','.join(['?'] * len(old_items)), old_items]) + + # change all network conversion info at once (much faster) + if len(cl) > 0: + my_db.mass_action(cl) \ No newline at end of file