if nothing has changed don't execute the transaction for network table and don't reload the table

Merge pull request #659 from Prinz23/network_update_fix2

don't call unnecessary 2 functions in network_timezones
Check that timezone is not None, in that case return Sickbeard Timezone
Merge pull request #662 from Prinz23/network_none_fix

Check that timezone is not None, in that case return Sickbeard Timezone
This commit is contained in:
echel0n 2014-03-11 13:18:49 -07:00
parent 3bdebb4e6d
commit 6adbdb5e86
1 changed files with 42 additions and 28 deletions

View File

@ -28,7 +28,9 @@ import re
import datetime
# regex to parse time (12/24 hour format)
time_regex = re.compile(r"(\d{1,2}):(\d{2,2})( [PA]M)?\b", flags=re.IGNORECASE)
time_regex = re.compile(r"(\d{1,2})(([:.](\d{2,2}))? ?([PA][. ]? ?M)|[:.](\d{2,2}))\b", flags=re.IGNORECASE)
am_regex = re.compile(r"(A[. ]? ?M)", flags=re.IGNORECASE)
pm_regex = re.compile(r"(P[. ]? ?M)", flags=re.IGNORECASE)
network_dict = None
@ -37,7 +39,7 @@ sb_timezone = tz.tzlocal()
# helper to remove failed temp download
def _remove_zoneinfo_failed(filename):
try:
os.remove(filename)
ek.ek(os.remove,filename)
except:
pass
@ -56,7 +58,7 @@ def _remove_old_zoneinfo():
file_w_path = ek.ek(join,path,filename)
if file_w_path != cur_file and ek.ek(isfile,file_w_path):
try:
os.remove(file_w_path)
ek.ek(os.remove,file_w_path)
logger.log(u"Delete unneeded old zoneinfo File: " + file_w_path)
except:
logger.log(u"Unable to delete: " + file_w_path,logger.ERROR)
@ -68,7 +70,7 @@ def _update_zoneinfo():
sb_timezone = tz.tzlocal()
# now check if the zoneinfo needs update
url_zv = 'http://github.com/Prinz23/sb_network_timezones/raw/master/zoneinfo.txt'
url_zv = 'https://github.com/Prinz23/sb_network_timezones/raw/master/zoneinfo.txt'
url_data = helpers.getURL(url_zv)
@ -87,13 +89,13 @@ def _update_zoneinfo():
return
# now load the new zoneinfo
url_tar = u'http://github.com/Prinz23/sb_network_timezones/raw/master/' + new_zoneinfo
url_tar = u'https://github.com/Prinz23/sb_network_timezones/raw/master/' + new_zoneinfo
zonefile = ek.ek(realpath, u'lib/dateutil/zoneinfo/' + new_zoneinfo)
zonefile_tmp = re.sub(r"\.tar\.gz$",'.tmp', zonefile)
if (os.path.exists(zonefile_tmp)):
if (ek.ek(os.path.exists,zonefile_tmp)):
try:
os.remove(zonefile_tmp)
ek.ek(os.remove,zonefile_tmp)
except:
logger.log(u"Unable to delete: " + zonefile_tmp,logger.ERROR)
return
@ -109,10 +111,10 @@ def _update_zoneinfo():
# remove the old zoneinfo file
if (cur_zoneinfo is not None):
old_file = ek.ek(realpath, u'lib/dateutil/zoneinfo/' + cur_zoneinfo)
if (os.path.exists(old_file)):
os.remove(old_file)
if (ek.ek(os.path.exists,old_file)):
ek.ek(os.remove,old_file)
# rename downloaded file
os.rename(zonefile_tmp,zonefile)
ek.ek(os.rename,zonefile_tmp,zonefile)
# load the new zoneinfo
reload(lib.dateutil.zoneinfo)
sb_timezone = tz.tzlocal()
@ -133,7 +135,7 @@ def update_network_dict():
d = {}
# network timezones are stored on github pages
url = 'http://github.com/Prinz23/sb_network_timezones/raw/master/network_timezones.txt'
url = 'https://github.com/Prinz23/sb_network_timezones/raw/master/network_timezones.txt'
url_data = helpers.getURL(url)
@ -173,8 +175,9 @@ def update_network_dict():
L = list(va for va in old_d)
ql.append(["DELETE FROM network_timezones WHERE network_name IN ("+','.join(['?'] * len(L))+")", L])
# change all network timezone infos at once (much faster)
myDB.mass_action(ql)
load_network_dict()
if len(ql) > 0:
myDB.mass_action(ql)
load_network_dict()
# load network timezones from db into dict
def load_network_dict():
@ -197,7 +200,10 @@ def get_network_timezone(network, network_dict):
return sb_timezone
try:
return tz.gettz(network_dict[network])
if lib.dateutil.zoneinfo.ZONEINFOFILE is not None:
return tz.gettz(network_dict[network])
else:
return sb_timezone
except:
return sb_timezone
@ -206,20 +212,28 @@ def parse_date_time(d, t, network):
if network_dict is None:
load_network_dict()
mo = time_regex.search(t)
if mo is not None and len(mo.groups()) >= 2:
try:
hr = helpers.tryInt(mo.group(1))
m = helpers.tryInt(mo.group(2))
ap = mo.group(3)
# convert am/pm to 24 hour clock
if ap is not None:
if ap.lower() == u" pm" and hr != 12:
hr += 12
elif ap.lower() == u" am" and hr == 12:
hr -= 12
except:
hr = 0
m = 0
if mo is not None and len(mo.groups()) >= 5:
if mo.group(5) is not None:
try:
hr = helpers.tryInt(mo.group(1))
m = helpers.tryInt(mo.group(4))
ap = mo.group(5)
# convert am/pm to 24 hour clock
if ap is not None:
if pm_regex.search(ap) is not None and hr != 12:
hr += 12
elif am_regex.search(ap) is not None and hr == 12:
hr -= 12
except:
hr = 0
m = 0
else:
try:
hr = helpers.tryInt(mo.group(1))
m = helpers.tryInt(mo.group(6))
except:
hr = 0
m = 0
else:
hr = 0
m = 0