2014-03-10 01:18:05 -04:00
|
|
|
# Author: Nic Wolfe <nic@wolfeden.ca>
|
|
|
|
# URL: http://code.google.com/p/sickbeard/
|
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# This file is part of SickRage.
|
2014-03-10 01:18:05 -04:00
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# SickRage is free software: you can redistribute it and/or modify
|
2014-03-10 01:18:05 -04:00
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# SickRage is distributed in the hope that it will be useful,
|
2014-03-10 01:18:05 -04:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2014-05-23 08:37:22 -04:00
|
|
|
# along with SickRage. If not, see <http://www.gnu.org/licenses/>.
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
from lib.dateutil import tz
|
|
|
|
import lib.dateutil.zoneinfo
|
|
|
|
from sickbeard import db
|
|
|
|
from sickbeard import helpers
|
|
|
|
from sickbeard import logger
|
|
|
|
from sickbeard import encodingKludge as ek
|
2014-03-20 04:15:22 -04:00
|
|
|
from os.path import basename, join, isfile
|
2014-03-10 01:18:05 -04:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
# regex to parse time (12/24 hour format)
|
2014-11-05 06:33:05 -05:00
|
|
|
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)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
network_dict = None
|
|
|
|
|
|
|
|
sb_timezone = tz.tzlocal()
|
|
|
|
|
2014-11-05 06:33:05 -05:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
# helper to remove failed temp download
|
|
|
|
def _remove_zoneinfo_failed(filename):
|
|
|
|
try:
|
2014-03-25 01:57:24 -04:00
|
|
|
ek.ek(os.remove, filename)
|
2014-03-10 01:18:05 -04:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
# helper to remove old unneeded zoneinfo files
|
|
|
|
def _remove_old_zoneinfo():
|
2014-11-05 06:33:05 -05:00
|
|
|
if lib.dateutil.zoneinfo.ZONEINFOFILE is not None:
|
2014-03-10 01:18:05 -04:00
|
|
|
cur_zoneinfo = ek.ek(basename, lib.dateutil.zoneinfo.ZONEINFOFILE)
|
|
|
|
else:
|
|
|
|
return
|
2014-03-25 01:57:24 -04:00
|
|
|
|
|
|
|
cur_file = helpers.real_path(ek.ek(join, ek.ek(os.path.dirname, lib.dateutil.zoneinfo.__file__), cur_zoneinfo))
|
|
|
|
|
|
|
|
for (path, dirs, files) in ek.ek(os.walk,
|
|
|
|
helpers.real_path(ek.ek(os.path.dirname, lib.dateutil.zoneinfo.__file__))):
|
2014-03-10 01:18:05 -04:00
|
|
|
for filename in files:
|
|
|
|
if filename.endswith('.tar.gz'):
|
2014-03-25 01:57:24 -04:00
|
|
|
file_w_path = ek.ek(join, path, filename)
|
|
|
|
if file_w_path != cur_file and ek.ek(isfile, file_w_path):
|
2014-03-10 01:18:05 -04:00
|
|
|
try:
|
2014-03-25 01:57:24 -04:00
|
|
|
ek.ek(os.remove, file_w_path)
|
2014-11-05 06:33:05 -05:00
|
|
|
logger.log(u'Delete unneeded old zoneinfo File: %s' % file_w_path)
|
2014-03-10 01:18:05 -04:00
|
|
|
except:
|
2014-11-05 06:33:05 -05:00
|
|
|
logger.log(u'Unable to delete: %s' % file_w_path, logger.ERROR)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
# update the dateutil zoneinfo
|
|
|
|
def _update_zoneinfo():
|
|
|
|
global sb_timezone
|
|
|
|
sb_timezone = tz.tzlocal()
|
|
|
|
|
|
|
|
# now check if the zoneinfo needs update
|
2014-06-12 14:40:23 -04:00
|
|
|
url_zv = 'https://raw.githubusercontent.com/Prinz23/sb_network_timezones/master/zoneinfo.txt'
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-12-12 11:46:03 -05:00
|
|
|
try:
|
|
|
|
url_data = helpers.getURL(url_zv)
|
|
|
|
if not url_data:
|
|
|
|
raise
|
|
|
|
|
|
|
|
if lib.dateutil.zoneinfo.ZONEINFOFILE is not None:
|
|
|
|
cur_zoneinfo = ek.ek(basename, lib.dateutil.zoneinfo.ZONEINFOFILE)
|
|
|
|
else:
|
|
|
|
cur_zoneinfo = None
|
|
|
|
|
|
|
|
(new_zoneinfo, zoneinfo_md5) = url_data.decode('utf-8').strip().rsplit(u' ')
|
|
|
|
except:
|
2014-03-10 01:18:05 -04:00
|
|
|
# When urlData is None, trouble connecting to github
|
2014-11-05 06:33:05 -05:00
|
|
|
logger.log(u'Loading zoneinfo.txt failed, this can happen from time to time. Unable to get URL: %s' % url_zv,
|
|
|
|
logger.WARNING)
|
2014-03-10 01:18:05 -04:00
|
|
|
return
|
|
|
|
|
2014-11-05 06:33:05 -05:00
|
|
|
if (cur_zoneinfo is not None) and (new_zoneinfo == cur_zoneinfo):
|
2014-03-10 01:18:05 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
# now load the new zoneinfo
|
2014-11-05 06:33:05 -05:00
|
|
|
url_tar = u'https://raw.githubusercontent.com/Prinz23/sb_network_timezones/master/%s' % new_zoneinfo
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
zonefile = helpers.real_path(ek.ek(join, ek.ek(os.path.dirname, lib.dateutil.zoneinfo.__file__), new_zoneinfo))
|
2014-11-05 06:33:05 -05:00
|
|
|
zonefile_tmp = re.sub(r'\.tar\.gz$', '.tmp', zonefile)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-11-05 06:33:05 -05:00
|
|
|
if ek.ek(os.path.exists, zonefile_tmp):
|
2014-03-10 01:18:05 -04:00
|
|
|
try:
|
2014-03-25 01:57:24 -04:00
|
|
|
ek.ek(os.remove, zonefile_tmp)
|
2014-03-10 01:18:05 -04:00
|
|
|
except:
|
2014-11-05 06:33:05 -05:00
|
|
|
logger.log(u'Unable to delete: %s' % zonefile_tmp, logger.ERROR)
|
2014-03-10 01:18:05 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
if not helpers.download_file(url_tar, zonefile_tmp):
|
|
|
|
return
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
if not ek.ek(os.path.exists, zonefile_tmp):
|
2014-11-05 06:33:05 -05:00
|
|
|
logger.log(u'Download of %s failed.' % zonefile_tmp, logger.ERROR)
|
2014-03-20 04:15:22 -04:00
|
|
|
return
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
new_hash = str(helpers.md5_for_file(zonefile_tmp))
|
|
|
|
|
2014-11-05 06:33:05 -05:00
|
|
|
if zoneinfo_md5.upper() == new_hash.upper():
|
2014-12-16 05:24:06 -05:00
|
|
|
logger.log(u'Updating timezone info with new one: %s' % new_zoneinfo, logger.INFO)
|
2014-03-10 01:18:05 -04:00
|
|
|
try:
|
|
|
|
# remove the old zoneinfo file
|
2014-11-05 06:33:05 -05:00
|
|
|
if cur_zoneinfo is not None:
|
2014-03-25 01:57:24 -04:00
|
|
|
old_file = helpers.real_path(
|
|
|
|
ek.ek(join, ek.ek(os.path.dirname, lib.dateutil.zoneinfo.__file__), cur_zoneinfo))
|
2014-11-05 06:33:05 -05:00
|
|
|
if ek.ek(os.path.exists, old_file):
|
2014-03-25 01:57:24 -04:00
|
|
|
ek.ek(os.remove, old_file)
|
2014-03-10 01:18:05 -04:00
|
|
|
# rename downloaded file
|
2014-03-25 01:57:24 -04:00
|
|
|
ek.ek(os.rename, zonefile_tmp, zonefile)
|
2014-03-10 01:18:05 -04:00
|
|
|
# load the new zoneinfo
|
|
|
|
reload(lib.dateutil.zoneinfo)
|
|
|
|
sb_timezone = tz.tzlocal()
|
|
|
|
except:
|
|
|
|
_remove_zoneinfo_failed(zonefile_tmp)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
_remove_zoneinfo_failed(zonefile_tmp)
|
2014-11-05 06:33:05 -05:00
|
|
|
logger.log(u'MD5 hash does not match: %s File: %s' % (zoneinfo_md5.upper(), new_hash.upper()), logger.ERROR)
|
2014-03-10 01:18:05 -04:00
|
|
|
return
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
# update the network timezone table
|
|
|
|
def update_network_dict():
|
|
|
|
_remove_old_zoneinfo()
|
|
|
|
_update_zoneinfo()
|
|
|
|
|
|
|
|
d = {}
|
|
|
|
|
|
|
|
# network timezones are stored on github pages
|
2014-06-12 14:40:23 -04:00
|
|
|
url = 'https://raw.githubusercontent.com/Prinz23/sb_network_timezones/master/network_timezones.txt'
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
url_data = helpers.getURL(url)
|
|
|
|
if url_data is None:
|
|
|
|
# When urlData is None, trouble connecting to github
|
2014-11-05 06:33:05 -05:00
|
|
|
logger.log(u'Updating network timezones failed, this can happen from time to time. URL: %s' % url, logger.WARNING)
|
2014-03-10 01:18:05 -04:00
|
|
|
load_network_dict()
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
for line in url_data.splitlines():
|
2014-03-25 01:57:24 -04:00
|
|
|
(key, val) = line.decode('utf-8').strip().rsplit(u':', 1)
|
|
|
|
if key is None or val is None:
|
|
|
|
continue
|
|
|
|
d[key] = val
|
2014-03-10 01:18:05 -04:00
|
|
|
except (IOError, OSError):
|
|
|
|
pass
|
|
|
|
|
2014-11-05 06:33:05 -05:00
|
|
|
my_db = db.DBConnection('cache.db')
|
2014-06-30 07:44:36 -04:00
|
|
|
|
2014-06-21 18:46:59 -04:00
|
|
|
# load current network timezones
|
2014-11-05 06:33:05 -05:00
|
|
|
old_d = dict(my_db.select('SELECT * FROM network_timezones'))
|
2014-06-21 18:46:59 -04:00
|
|
|
|
|
|
|
# list of sql commands to update the network_timezones table
|
2014-07-14 22:00:53 -04:00
|
|
|
cl = []
|
2014-06-21 18:46:59 -04:00
|
|
|
for cur_d, cur_t in d.iteritems():
|
|
|
|
h_k = old_d.has_key(cur_d)
|
|
|
|
if h_k and cur_t != old_d[cur_d]:
|
|
|
|
# update old record
|
2014-07-14 22:00:53 -04:00
|
|
|
cl.append(
|
2014-11-05 06:33:05 -05:00
|
|
|
['UPDATE network_timezones SET network_name=?, timezone=? WHERE network_name=?', [cur_d, cur_t, cur_d]])
|
2014-06-21 18:46:59 -04:00
|
|
|
elif not h_k:
|
|
|
|
# add new record
|
2014-11-05 06:33:05 -05:00
|
|
|
cl.append(['INSERT INTO network_timezones (network_name, timezone) VALUES (?,?)', [cur_d, cur_t]])
|
2014-06-21 18:46:59 -04:00
|
|
|
if h_k:
|
|
|
|
del old_d[cur_d]
|
2014-06-30 07:44:36 -04:00
|
|
|
|
2014-06-21 18:46:59 -04:00
|
|
|
# remove deleted records
|
|
|
|
if len(old_d) > 0:
|
2014-11-05 06:33:05 -05:00
|
|
|
old_items = list(va for va in old_d)
|
|
|
|
cl.append(['DELETE FROM network_timezones WHERE network_name IN (%s)' % ','.join(['?'] * len(old_items)), old_items])
|
2014-06-30 07:44:36 -04:00
|
|
|
|
2014-06-21 18:46:59 -04:00
|
|
|
# change all network timezone infos at once (much faster)
|
2014-07-14 22:00:53 -04:00
|
|
|
if len(cl) > 0:
|
2014-11-05 06:33:05 -05:00
|
|
|
my_db.mass_action(cl)
|
2014-06-21 18:46:59 -04:00
|
|
|
load_network_dict()
|
2014-06-30 11:57:32 -04:00
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
# load network timezones from db into dict
|
|
|
|
def load_network_dict():
|
|
|
|
try:
|
2014-11-05 06:33:05 -05:00
|
|
|
my_db = db.DBConnection('cache.db')
|
|
|
|
cur_network_list = my_db.select('SELECT * FROM network_timezones')
|
2014-06-21 18:46:59 -04:00
|
|
|
if cur_network_list is None or len(cur_network_list) < 1:
|
|
|
|
update_network_dict()
|
2014-11-05 06:33:05 -05:00
|
|
|
cur_network_list = my_db.select('SELECT * FROM network_timezones')
|
2014-03-10 01:18:05 -04:00
|
|
|
d = dict(cur_network_list)
|
|
|
|
except:
|
|
|
|
d = {}
|
|
|
|
global network_dict
|
|
|
|
network_dict = d
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
# get timezone of a network or return default timezone
|
|
|
|
def get_network_timezone(network, network_dict):
|
|
|
|
if network is None:
|
|
|
|
return sb_timezone
|
|
|
|
|
|
|
|
try:
|
2014-03-11 16:18:49 -04:00
|
|
|
if lib.dateutil.zoneinfo.ZONEINFOFILE is not None:
|
2014-06-09 20:39:52 -04:00
|
|
|
try:
|
|
|
|
n_t = tz.gettz(network_dict[network])
|
|
|
|
except:
|
|
|
|
return sb_timezone
|
2014-06-11 04:34:28 -04:00
|
|
|
|
2014-03-20 04:15:22 -04:00
|
|
|
if n_t is not None:
|
|
|
|
return n_t
|
|
|
|
else:
|
|
|
|
return sb_timezone
|
2014-03-11 16:18:49 -04:00
|
|
|
else:
|
|
|
|
return sb_timezone
|
2014-03-10 01:18:05 -04:00
|
|
|
except:
|
|
|
|
return sb_timezone
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
# parse date and time string into local time
|
2014-05-17 03:30:21 -04:00
|
|
|
def parse_date_time(d, t, network):
|
2014-03-10 01:18:05 -04:00
|
|
|
if network_dict is None:
|
|
|
|
load_network_dict()
|
|
|
|
mo = time_regex.search(t)
|
2014-03-11 16:18:49 -04:00
|
|
|
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
|
2014-03-10 01:18:05 -04:00
|
|
|
else:
|
|
|
|
hr = 0
|
|
|
|
m = 0
|
|
|
|
if hr < 0 or hr > 23 or m < 0 or m > 59:
|
|
|
|
hr = 0
|
|
|
|
m = 0
|
2014-05-17 03:30:21 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
te = datetime.datetime.fromordinal(helpers.tryInt(d))
|
|
|
|
try:
|
2014-11-05 06:33:05 -05:00
|
|
|
foreign_timezone = get_network_timezone(network, network_dict)
|
|
|
|
foreign_naive = datetime.datetime(te.year, te.month, te.day, hr, m, tzinfo=foreign_timezone)
|
|
|
|
return foreign_naive
|
2014-05-17 03:30:21 -04:00
|
|
|
except:
|
2014-11-05 06:33:05 -05:00
|
|
|
return datetime.datetime(te.year, te.month, te.day, hr, m, tzinfo=sb_timezone)
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def test_timeformat(t):
|
|
|
|
mo = time_regex.search(t)
|
|
|
|
if mo is None or len(mo.groups()) < 2:
|
|
|
|
return False
|
|
|
|
else:
|
2014-06-09 20:39:52 -04:00
|
|
|
return True
|