Add TVRage network name standardization

This commit is contained in:
Adam 2014-11-16 23:00:05 +08:00 committed by Adam
parent d23b6b684f
commit 9b3fd0b7a9
5 changed files with 79 additions and 2 deletions

View file

@ -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]

View file

@ -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:

View file

@ -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)")
"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)')

View file

@ -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

View file

@ -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)