-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairbnb_superhost_geocoding.py
executable file
·51 lines (44 loc) · 1.42 KB
/
airbnb_superhost_geocoding.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
# -*- coding: utf-8 -*-
import geocoder
import json
import os
import random
def read_profile():
file_in = open('./Data/Superhost/Profile_Super.txt')
superhost_list_tmp = file_in.read().split('\n')
superhost_list = []
for superhost in superhost_list_tmp:
if superhost != '':
superhost_list.append(superhost.split('\t'))
return superhost_list
def get_location():
with open('./geocoding_key.txt', 'r') as f:
Keys = f.read().split('\n')
while '' in Keys:
Keys.remove('')
superhost_list = read_profile()
for superhost in superhost_list:
user_id = superhost[0]
print(user_id)
address = superhost[2]
print(address)
if os.path.exists('./Data/Superhost/Location/' + user_id + '.json'):
print('exists!')
continue
num = random.randint(0, len(Keys) - 1)
try:
geo = geocoder.google(address, key=Keys[num])
except:
del Keys[num]
print('failed!')
continue
print(geo.json['status'])
if geo.json['status'] != "OK":
print('failed!')
continue
with open('./Data/Superhost/Location/' + user_id + '.json', 'w') as f:
json.dump(geo.json, f, indent=4)
if __name__ == '__main__':
if not os.path.exists('./Data/Superhost/Location'):
os.mkdir('./Data/Superhost/Location')
get_location()