pep.py/helpers/locationHelper.py

49 lines
1.1 KiB
Python
Raw Normal View History

2016-04-19 17:40:59 +00:00
import urllib.request
import json
from helpers import consoleHelper
from constants import bcolors
2016-04-19 17:40:59 +00:00
# API URL
2016-05-01 16:09:35 +00:00
URL = "http://ip.zxq.co/"
2016-04-19 17:40:59 +00:00
def getCountry(ip):
"""
Get country from IP address
ip -- IP Address
return -- Country code (2 letters)
"""
# Default value, sent if API is memeing
country = "XX"
try:
# Try to get country from Pikolo Aul's Go-Sanic ip API
result = json.loads(urllib.request.urlopen("{}/{}".format(URL, ip), timeout=3).read().decode())["country"]
return result.upper()
2016-04-19 17:40:59 +00:00
except:
consoleHelper.printColored("[!] Error in get country", bcolors.RED)
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]
"""
# Default value, sent if API is memeing
data = [0,0]
try:
# Try to get position from Pikolo Aul's Go-Sanic ip API
result = json.loads(urllib.request.urlopen("{}/{}".format(URL, ip), timeout=3).read().decode())["loc"].split(",")
return [float(result[0]), float(result[1])]
2016-04-19 17:40:59 +00:00
except:
consoleHelper.printColored("[!] Error in get position", bcolors.RED)
return [0,0]