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
|
2016-07-14 10:37:07 +00:00
|
|
|
from objects import glob
|
2016-04-19 17:40:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def getCountry(ip):
|
|
|
|
"""
|
2016-11-17 18:13:06 +00:00
|
|
|
Get country from IP address using geoip api
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-11-17 18:13:06 +00:00
|
|
|
:param ip: IP address
|
|
|
|
:return: country code. XX if invalid.
|
2016-04-19 17:40:59 +00:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
# Try to get country from Pikolo Aul's Go-Sanic ip API
|
2016-07-14 10:37:07 +00:00
|
|
|
result = json.loads(urllib.request.urlopen("{}/{}".format(glob.conf.config["localize"]["ipapiurl"], ip), timeout=3).read().decode())["country"]
|
2016-05-17 19:18:03 +00:00
|
|
|
return result.upper()
|
2016-04-19 17:40:59 +00:00
|
|
|
except:
|
2016-06-04 10:44:54 +00:00
|
|
|
log.error("Error in get country")
|
2016-05-17 19:18:03 +00:00
|
|
|
return "XX"
|
2016-04-19 17:40:59 +00:00
|
|
|
|
|
|
|
def getLocation(ip):
|
|
|
|
"""
|
2016-11-17 18:13:06 +00:00
|
|
|
Get latitude and longitude from IP address using geoip api
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-11-17 18:13:06 +00:00
|
|
|
:param ip: IP address
|
|
|
|
:return: (latitude, longitude)
|
2016-04-19 17:40:59 +00:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
# Try to get position from Pikolo Aul's Go-Sanic ip API
|
2016-07-14 10:37:07 +00:00
|
|
|
result = json.loads(urllib.request.urlopen("{}/{}".format(glob.conf.config["localize"]["ipapiurl"], ip), timeout=3).read().decode())["loc"].split(",")
|
2016-12-26 09:33:05 +00:00
|
|
|
return float(result[0]), float(result[1])
|
2016-04-19 17:40:59 +00:00
|
|
|
except:
|
2016-06-04 10:44:54 +00:00
|
|
|
log.error("Error in get position")
|
2016-12-26 09:33:05 +00:00
|
|
|
return 0, 0
|