From 7aa0a951a78696ee89078e50f6505658525a5d27 Mon Sep 17 00:00:00 2001 From: James Meyer Date: Wed, 3 Oct 2012 17:56:36 -0500 Subject: LinHES-config: timezip.py add geolocate abilities for guessing timezone, will only work for US based IPs. --- abs/core/LinHES-config/PKGBUILD | 4 +- abs/core/LinHES-config/timezip.py | 97 +++++++++++++++++++++++++++++++++------ 2 files changed, 84 insertions(+), 17 deletions(-) diff --git a/abs/core/LinHES-config/PKGBUILD b/abs/core/LinHES-config/PKGBUILD index a7ea24b..b3e135e 100755 --- a/abs/core/LinHES-config/PKGBUILD +++ b/abs/core/LinHES-config/PKGBUILD @@ -1,6 +1,6 @@ pkgname=LinHES-config pkgver=2.3 -pkgrel=167 +pkgrel=168 conflicts=(MythVantage-config MythVantage-config-dev LinHES-config-dev LinHes-config ) pkgdesc="Install and configure your system" depends=('bc' 'libstatgrab' 'mysql-python' 'expect' 'curl' 'dnsutils' 'parted' @@ -137,7 +137,7 @@ md5sums=('59e18e35359bebcd9d639c5e5b5d290d' '2a7f3b34e522acfd08283b86c8926aba' '36f5017e25c200354a954b17819aa1b2' '689b01f7636e09b2f9657c6ce6006ee7' - 'c8b5d6ad73595aa96bf04df9df2fd869' + '38d17b5415c9fce98923a1b136a3fbe0' 'a6faa20d905e2fd92ce79acab044b759' '8ba06c2ce6129371548cc360ccca27f8' '5f890ea9bda6aef652d2560ca19dac07' diff --git a/abs/core/LinHES-config/timezip.py b/abs/core/LinHES-config/timezip.py index 625350b..89b95de 100755 --- a/abs/core/LinHES-config/timezip.py +++ b/abs/core/LinHES-config/timezip.py @@ -6,6 +6,24 @@ import MySQLdb import sys import getopt import socket +import urllib2 +socket.setdefaulttimeout(10) + +try: + from json import loads as jsonparse + nojson = False +except ImportError: + try: + from simplejson import loads as jsonparse + nojson = False + except ImportError: + def jsonparse(js): + if unsafe: + return eval(js) + raise RuntimeError("Neither the json or simplejson module is available.\nUse the --unsafe option to use python eval() as an ad-hoc json decoder") + + + def usage(): @@ -14,6 +32,48 @@ def usage(): print " -m --masterdb" print " -n --hostname of current machine" +def latlong2_olsen(lat,lng): + olsenttz = '' + geo_url = "http://api.geonames.org/timezoneJSON?lat=%s&lng=%s&username=linhes_timezone" %(lat,lng) + try: + json_stream = urllib2.urlopen(geo_url,timeout=10) + json_dict = jsonparse(json_stream.read().replace("\r\n","")) + olsenttz = json_dict['timezoneId'] + except: + print "error with geonames" + + return olsenttz + +def geo_locate(): + print " Trying to geo locate in timezip.py" + gtz = '' + gtz_region = '' + gtz_subregion = '' + #get long/lat from ip + try: + geo_url = "http://www.geobytes.com/IpLocator.htm?GetLocation&template=json.txt" + json_stream = urllib2.urlopen(geo_url,timeout=10) + json_dict = jsonparse(json_stream.read().replace("\r\n",""))['geobytes'] + latitude = json_dict['latitude'] + longitude = json_dict['longitude'] + except: + print "error with geobytes/geolocate" + latitude = '' + longitude = '' + + if latitude != '': + try: + olsenttz = latlong2_olsen (latitude,longitude) + gtz = olsenttz + gtz_region , gtz_subregion = olsenttz.split("/") + except: + print "error with geolocate" + + + + return gtz, gtz_region , gtz_subregion + + def printvars(): print "masterdb: " + masterdb print "installdb:" + installdb @@ -83,27 +143,34 @@ def selectvars(): except TypeError: nfsmount="" - cursor.execute("select data from settings where value='HostTimeZone' and hostname=(%s)",(BEhostname)) + cursor.execute("select data from settings where value='HostTimeZonetemp' and hostname=(%s)",(BEhostname)) + #cursor.execute("select data from settings where value='HostTimeZone' and hostname=(%s)",(BEhostname)) result = cursor.fetchone() try: tz=result[0] except TypeError: tz="" -########## - cursor.execute("select data from settings where value='HostTimeZoneRegion' and hostname=(%s)",(BEhostname)) - result = cursor.fetchone() - try: - tz_region=result[0] - except TypeError: - tz_region="" - tempsubregion="HostTimeZoneRegion_" + tz_region - cursor.execute("select data from settings where value=%s and hostname=(%s)",(tempsubregion,BEhostname)) - result = cursor.fetchone() - try: - tz_subregion=result[0] - except TypeError: - tz_subregion="" + if tz == "": + tz,tz_region, tz_subregion = geo_locate() + else: + cursor.execute("select data from settings where value='HostTimeZoneRegion' and hostname=(%s)",(BEhostname)) + result = cursor.fetchone() + try: + tz_region=result[0] + except TypeError: + tz_region="" + + tempsubregion="HostTimeZoneRegion_" + tz_region + + cursor.execute("select data from settings where value=%s and hostname=(%s)",(tempsubregion,BEhostname)) + result = cursor.fetchone() + try: + tz_subregion=result[0] + except TypeError: + tz_subregion="" + + ############### if ( thishostname != BEhostname ): cursor.execute("select data from settings where value='GlobalServiceHobbitserver'") -- cgit v0.12