Commit b5384358 authored by echel0n's avatar echel0n

Refactored network timezone functions.

parent 79d9ea15
# 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.
- * 8d1c1a6 - 2018-11-18: Refactored pypi publish to use twine
- * d49c03b - 2018-11-18: Release v9.4.39
......
......@@ -34,7 +34,6 @@ from sickrage.core.websession import WebSession
class TimeZoneUpdater(object):
def __init__(self):
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',
re.I)
......@@ -42,62 +41,46 @@ class TimeZoneUpdater(object):
# set thread name
threading.currentThread().setName(self.name)
self.update_network_dict()
self.update_network_timezones()
# update the network timezone table
def update_network_dict(self):
def update_network_timezones(self):
"""Update timezone information from SR repositories"""
url = 'https://cdn.sickrage.ca/network_timezones/'
network_timezones = {}
try:
url_data = WebSession().get(url).text
url_data = WebSession().get('https://cdn.sickrage.ca/network_timezones/').text
except Exception:
sickrage.app.log.warning(
'Updating network timezones failed, this can happen from time to time. URL: %s' % url)
sickrage.app.log.warning('Updating network timezones failed.')
return
d = {}
try:
for line in url_data.splitlines():
(key, val) = line.strip().rsplit(':', 1)
if key is None or val is None:
continue
d[key] = val
if all([key, val]):
network_timezones[key] = val
except (IOError, OSError):
pass
for network, timezone in d.items():
existing = network in self.network_dict
if not existing:
if not sickrage.app.cache_db.get('network_timezones', network):
sickrage.app.cache_db.insert({
'_t': 'network_timezones',
'network_name': ss(network),
'timezone': timezone
})
elif self.network_dict[network] is not timezone:
dbData = sickrage.app.cache_db.get('network_timezones', network)
if dbData:
dbData['timezone'] = timezone
sickrage.app.cache_db.update(dbData)
if existing:
del self.network_dict[network]
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')])
for x in sickrage.app.cache_db.all('network_timezones'):
if x['network_name'] not in network_timezones:
sickrage.app.cache_db.delete(x)
for network, timezone in network_timezones.items():
dbData = sickrage.app.cache_db.get('network_timezones', network)
if not dbData:
sickrage.app.cache_db.insert({
'_t': 'network_timezones',
'network_name': ss(network),
'timezone': timezone
})
elif dbData['timezone'] != timezone:
dbData['timezone'] = timezone
sickrage.app.cache_db.update(dbData)
# cleanup
del network_timezones
# get timezone of a network or return default timezone
def get_network_timezone(self, network):
......@@ -111,7 +94,7 @@ class TimeZoneUpdater(object):
return sickrage.app.tz
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:
return sickrage.app.tz
......@@ -125,9 +108,6 @@ class TimeZoneUpdater(object):
:return: datetime object containing local time
"""
if not self.network_dict:
self.load_network_dict()
parsed_time = self.time_regex.search(t)
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