summaryrefslogtreecommitdiffstats
path: root/abs/core/LinHES-config/timezip.py
diff options
context:
space:
mode:
Diffstat (limited to 'abs/core/LinHES-config/timezip.py')
-rwxr-xr-xabs/core/LinHES-config/timezip.py97
1 files changed, 82 insertions, 15 deletions
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'")