pep.py/helpers/locationHelper.py

37 lines
959 B
Python
Raw Normal View History

2016-04-19 17:40:59 +00:00
import json
2016-10-02 20:48:14 +00:00
import urllib.request
from common.log import logUtils as log
from objects import glob
2016-04-19 17:40:59 +00:00
def getCountry(ip):
"""
Get country from IP address
ip -- IP Address
return -- Country code (2 letters)
"""
try:
# Try to get country from Pikolo Aul's Go-Sanic ip API
result = json.loads(urllib.request.urlopen("{}/{}".format(glob.conf.config["localize"]["ipapiurl"], ip), timeout=3).read().decode())["country"]
return result.upper()
2016-04-19 17:40:59 +00:00
except:
log.error("Error in get country")
return "XX"
2016-04-19 17:40:59 +00:00
def getLocation(ip):
"""
Get latitude and longitude from IP address
ip -- IP address
return -- [latitude, longitude]
"""
try:
# Try to get position from Pikolo Aul's Go-Sanic ip API
result = json.loads(urllib.request.urlopen("{}/{}".format(glob.conf.config["localize"]["ipapiurl"], ip), timeout=3).read().decode())["loc"].split(",")
return [float(result[0]), float(result[1])]
2016-04-19 17:40:59 +00:00
except:
log.error("Error in get position")
return [0,0]