/
location_geocoder.py
42 lines (35 loc) · 1.23 KB
/
location_geocoder.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
import sys
import logging
import time
from lib.geocoder import Geocoder
from lib.db import DB
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(levelname)s %(message)s',
filename='./log/location_geocoder.log',
filemode='w')
conf_file = 'data/geocoder.conf'
geocoder = Geocoder(logging, conf_file)
db = DB(logging)
while True:
users = db.get_geocoding_target_users(100)
if len(users) == 0:
logging.info("All users in DB are geocoded.")
time.sleep(10*60)
continue
for user in users:
if user['location_property'] == '':
db.update_user_by_geocoded_info(user['id'], -1)
logging.info("Cannot geocoded %s", user['id'])
time.sleep(10)
continue
locations = geocoder.get(user['location_property'])
if len(locations) == 0:
db.update_user_by_geocoded_info(user['id'], -1)
logging.info("Cannot geocoded %s", user['id'])
time.sleep(10)
continue
location = locations.pop(0)
location['id'] = location['id'].split('.')[0]
db.insert_location(location)
db.update_user_by_geocoded_info(user['id'], location['id'])
logging.info("Geocoded %s", user['id'])