-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
59 lines (46 loc) · 1.64 KB
/
code.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
47
48
49
50
51
52
53
54
55
56
57
58
59
import urllib.request, urllib.parse, urllib.error
import json
import os
import webbrowser
import ssl
import configparser
config = configparser.ConfigParser()
config.read('config.ini')
api_key = config['keys']['api_key']
serviceurl = config['keys']['service_url']
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
with open("where.txt") as fh, open("where.js","w",encoding="utf-8") as where:
adrs=[]
parms={}
for line in fh:
address = line.strip()
parms["address"] = address
parms['key'] = api_key
url = serviceurl + urllib.parse.urlencode(parms)
if url.lower().startswith('http'):
req = urllib.request.Request(url)
else:
raise ValueError from None
with urllib.request.urlopen(req, context=ctx) as resp:
data = resp.read().decode()
try:
js = json.loads(data)
except:
print(data)
continue
try:
adrs.append([js['results'][0]['geometry']['lat'],js['results'][0]['geometry']['lng'],js['results'][0]['formatted']])
print('Retrieved ', url)
except:
print("Not Found " + line.strip())
print("\n\nOpening Webpage")
where.write("myData = [\n")
for item in adrs:
st="[" + str(item[0]) + ", " + str(item[1]) + ", '" + str(item[2]) + "' ], \n"
where.write(st)
where.write(",\n")
where.write("];\n")
webbrowser.open('file://' + os.path.realpath("index.html"))