Commit b5384358 authored by echel0n's avatar echel0n

Refactored network timezone functions.

parent 79d9ea15
# Changelog # Changelog
- * 5e19197 - 2018-11-18: Fixed issue with database integrity checks. - * 64071b7 - 2018-11-18: Refactored network timezone functions.
- * 79d9ea1 - 2018-11-18: Fixed issue with database integrity checks.
- * b005c24 - 2018-11-18: Refactored app to use pip2 instead of pip. Fixed issues with source upgrading. - * b005c24 - 2018-11-18: Refactored app to use pip2 instead of pip. Fixed issues with source upgrading.
- * 8d1c1a6 - 2018-11-18: Refactored pypi publish to use twine - * 8d1c1a6 - 2018-11-18: Refactored pypi publish to use twine
- * d49c03b - 2018-11-18: Release v9.4.39 - * d49c03b - 2018-11-18: Release v9.4.39
......
...@@ -34,7 +34,6 @@ from sickrage.core.websession import WebSession ...@@ -34,7 +34,6 @@ from sickrage.core.websession import WebSession
class TimeZoneUpdater(object): class TimeZoneUpdater(object):
def __init__(self): def __init__(self):
self.name = "TZUPDATER" self.name = "TZUPDATER"
self.network_dict = {}
self.time_regex = re.compile(r'(?P<hour>\d{1,2})(?:[:.]?(?P<minute>\d{2})?)? ?(?P<meridiem>[PA]\.? ?M?)?\b', self.time_regex = re.compile(r'(?P<hour>\d{1,2})(?:[:.]?(?P<minute>\d{2})?)? ?(?P<meridiem>[PA]\.? ?M?)?\b',
re.I) re.I)
...@@ -42,62 +41,46 @@ class TimeZoneUpdater(object): ...@@ -42,62 +41,46 @@ class TimeZoneUpdater(object):
# set thread name # set thread name
threading.currentThread().setName(self.name) threading.currentThread().setName(self.name)
self.update_network_dict() self.update_network_timezones()
# update the network timezone table # update the network timezone table
def update_network_dict(self): def update_network_timezones(self):
"""Update timezone information from SR repositories""" """Update timezone information from SR repositories"""
url = 'https://cdn.sickrage.ca/network_timezones/' network_timezones = {}
try: try:
url_data = WebSession().get(url).text url_data = WebSession().get('https://cdn.sickrage.ca/network_timezones/').text
except Exception: except Exception:
sickrage.app.log.warning( sickrage.app.log.warning('Updating network timezones failed.')
'Updating network timezones failed, this can happen from time to time. URL: %s' % url)
return return
d = {}
try: try:
for line in url_data.splitlines(): for line in url_data.splitlines():
(key, val) = line.strip().rsplit(':', 1) (key, val) = line.strip().rsplit(':', 1)
if key is None or val is None: if all([key, val]):
continue network_timezones[key] = val
d[key] = val
except (IOError, OSError): except (IOError, OSError):
pass pass
for network, timezone in d.items(): for x in sickrage.app.cache_db.all('network_timezones'):
existing = network in self.network_dict if x['network_name'] not in network_timezones:
if not existing: sickrage.app.cache_db.delete(x)
if not sickrage.app.cache_db.get('network_timezones', network):
sickrage.app.cache_db.insert({ for network, timezone in network_timezones.items():
'_t': 'network_timezones', dbData = sickrage.app.cache_db.get('network_timezones', network)
'network_name': ss(network), if not dbData:
'timezone': timezone sickrage.app.cache_db.insert({
}) '_t': 'network_timezones',
elif self.network_dict[network] is not timezone: 'network_name': ss(network),
dbData = sickrage.app.cache_db.get('network_timezones', network) 'timezone': timezone
if dbData: })
dbData['timezone'] = timezone elif dbData['timezone'] != timezone:
sickrage.app.cache_db.update(dbData) dbData['timezone'] = timezone
sickrage.app.cache_db.update(dbData)
if existing:
del self.network_dict[network] # cleanup
del network_timezones
for x in self.network_dict:
sickrage.app.cache_db.delete(sickrage.app.cache_db.get('network_timezones', x))
self.load_network_dict()
# load network timezones from db into dict
def load_network_dict(self):
"""
Return network timezones from db
"""
self.network_dict = dict(
[(x['network_name'], x['timezone']) for x in sickrage.app.cache_db.all('network_timezones')])
# get timezone of a network or return default timezone # get timezone of a network or return default timezone
def get_network_timezone(self, network): def get_network_timezone(self, network):
...@@ -111,7 +94,7 @@ class TimeZoneUpdater(object): ...@@ -111,7 +94,7 @@ class TimeZoneUpdater(object):
return sickrage.app.tz return sickrage.app.tz
try: try:
return tz.gettz(self.network_dict[network]) or sickrage.app.tz return tz.gettz(sickrage.app.cache_db.get('network_timezones', network)['timezone'])
except Exception: except Exception:
return sickrage.app.tz return sickrage.app.tz
...@@ -125,9 +108,6 @@ class TimeZoneUpdater(object): ...@@ -125,9 +108,6 @@ class TimeZoneUpdater(object):
:return: datetime object containing local time :return: datetime object containing local time
""" """
if not self.network_dict:
self.load_network_dict()
parsed_time = self.time_regex.search(t) parsed_time = self.time_regex.search(t)
network_tz = self.get_network_timezone(network) network_tz = self.get_network_timezone(network)
......
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