-
Notifications
You must be signed in to change notification settings - Fork 8
/
geoloc.py
executable file
·46 lines (43 loc) · 1.53 KB
/
geoloc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/python3
import os, sys
ip = sys.argv[-1]
# Test for valid ip and if not supplied use external.
if ip.count(".") != 3:
ip = eval(os.popen("curl \'https://api.ipify.org?format=json\'").read())
ip = ip["ip"]
if "printIp" in sys.argv:
print(ip)
exit(0)
# try to read a location
location = os.popen("curl http://www.geoplugin.net/json.gp?ip=" + ip)
try:
loc = eval(location.read())
print(loc["geoplugin_city"], ",", loc["geoplugin_countryName"])
if "debug" in sys.argv:
print(loc)
# test for location file and create if not availible
if not os.path.lexists("./latlong.json"):
pos = "{\"lat\":" + loc["geoplugin_latitude"] + ",\"long\":" + loc["geoplugin_longitude"] + "}"
# print pos
with open("latlong.json", "w") as f:
f.write(pos)
except NameError:
# Try another api if the first test fails
try:
location = os.popen(" curl http://ip-api.com/json/" + ip).read()
loc = eval(location)
print(loc["city"], ",", loc["country"])
if "debug" in sys.argv:
print(loc)
# test for location file and create if not availible
if not os.path.lexists("./latlong.json"):
pos = "{\"lat\":\"" + str(loc["lat"]) + "\",\"long\":\"" + str(loc["lon"]) + "\"}"
# print pos
with open("latlong.json", "w") as f:
f.write(pos)
except NameError:
print("unknown")
if "debug" in sys.argv:
print(loc)
except KeyError:
print("unknown")