· 9 years ago · Oct 28, 2016, 03:34 PM
1import urllib
2import sqlite3
3import json
4import time
5import ssl
6import urllib.parse
7import urllib.request
8
9# If you are in China use this URL:
10# serviceurl = "http://maps.google.cn/maps/api/geocode/json?"
11serviceurl = "http://maps.googleapis.com/maps/api/geocode/json?"
12
13# Deal with SSL certificate anomalies Python > 2.7
14# scontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
15scontext = None
16
17conn = sqlite3.connect('geodata.sqlite')
18cur = conn.cursor()
19
20cur.execute('''
21CREATE TABLE IF NOT EXISTS Locations (address TEXT, geodata TEXT)''')
22
23fh = open("where.data")
24count = 0
25for line in fh:
26 if count > 200 : break
27 address = line.strip()
28 print ('') # itt a bufferre elszáll találtam olyat hogy viewmemory + .encode elvileg egyenl a bufferrel
29 cur.execute("SELECT geodata FROM Locations WHERE address= ?", (buffer(address), ))
30
31 try:
32 data = cur.fetchone()[0]
33 print ("Found in database ",address)
34 continue
35 except:
36 pass
37
38 print ('Resolving', address)
39 url = serviceurl + urllib.parse.urlencode({"sensor":"false", "address": address})
40 print ('Retrieving', url)
41 uh = urllib.request.urlopen(url, context=scontext)
42 data = uh.read() # itt a print elszáll a .replacere azt mondja bytelike kell neki nem str (ez nonszensz hibaüzenet szmomra)
43 print ('Retrieved',len(data),'characters',data[:20].replace('\n',' '))
44 count = count + 1
45 try:
46 js = json.loads(str(data))
47 # print js # We print in case unicode causes an error
48 except:
49 continue
50
51 if 'status' not in js or (js['status'] != 'OK' and js['status'] != 'ZERO_RESULTS') :
52 print ('==== Failure To Retrieve ====')
53 print (data)
54 break
55
56 cur.execute('''INSERT INTO Locations (address, geodata)
57 VALUES ( ?, ? )''', ( buffer(address),buffer(data) ) )
58 conn.commit()
59 time.sleep(1)
60
61print ("Run geodump.py to read the data from the database so you can visualize it on a map.")