-
Notifications
You must be signed in to change notification settings - Fork 0
/
refresh_location_data.py
67 lines (57 loc) · 1.84 KB
/
refresh_location_data.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
60
61
62
63
64
65
66
67
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import urllib, json, os
from db_manager import DBManager
from decorators import async
db = DBManager()
def get_travel_location(address):
address = urllib.quote(address)
url = 'http://api.map.baidu.com/geocoder/v2/?address=%s&output=json&ak=FD09ed40290e289709ffe50b546c3a23' % address
r=urllib.urlopen(url).read()
rlt = json.loads(r)
return rlt
def insert_travel_location(jingdian_id,province,place,name):
print str(jingdian_id),str(province),str(place),str(name),'...'
try:
address = str(province+place+name)#str(place+name) if str(name).find(str(place)) < 0 else str(name)
rlt = get_travel_location(address)
if rlt['status'] == 0:
location_lng = rlt['result']['location']['lng']
location_lat = rlt['result']['location']['lat']
sql = '''INSERT INTO dim_travel_location(`jingdian_id`,`search_value`,`location_lng`,`location_lat`)
VALUES(%s,'%s','%s','%s')''' % (str(jingdian_id),address,location_lng,location_lat)
db.executeNonQuery(sql)
else:
f = open('error.txt','a')
f.write(str(province)+' ==== '+str(place)+' ==== '+str(name)+'\n')
f.close()
print 'finished!'
except Exception,e:
print e
if __name__ == '__main__':
sql = '''SELECT a.`id`,a.`province`,a.`place`,a.`name`
FROM dim_travel AS a
WHERE NOT EXISTS
(
SELECT 1
FROM dim_travel_location AS l
WHERE l.jingdian_id = a.`id`
)
ORDER BY a.`id` LIMIT 0, 1000'''
result = db.executeQuery(sql)
import time
print 'here we go ...'
filename = r'error.txt'
if os.path.exists(filename):
os.remove(filename)
try:
for row in result:
insert_travel_location(row['id'],row['province'],row['place'],row['name'])
except Exception,ex:
print ex
time.sleep(0.5)
insert_travel_location(row['id'],row['province'],row['place'],row['name'])
# 青海湖黑马河乡
# 青海倒淌河