-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpylocate.py
42 lines (31 loc) · 1.31 KB
/
pylocate.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
#!/usr/bin/env python3
import requests
base_url='https://maps.googleapis.com/maps/api/geocode/json?'
def geo2add(geo):
try:
my_coord = {'latlng': geo, 'sensor':'true'}
response = requests.get(base_url, params = my_coord)
results = response.json()['results']
x_add = results[0]['formatted_address']
except (IndexError):
print("error. check your arguments and try again")
else:
print("Approximate address: ",x_add)
def add2geo(add):
try:
get_tag = {'address': add, 'language':'en'}
response = requests.get(base_url, params = get_tag)
results = response.json()['results']
addy= results[0]['geometry']['location']
except (IndexError):
print("error. check your arguments and try again")
else:
print("Approximate geotag: ",addy['lat'],addy['lng'])
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description='''PyLocate: Address and geotag lookup script''')
parser.add_argument("-g", "--geo", dest="geo", type=geo2add,
help="enter lat,lng to lookup. seperate fields with commas.")
parser.add_argument("-a", "--addy", dest="add", type=add2geo,
help="enter address. seperate fields with commas.")
args = parser.parse_args()