-
Notifications
You must be signed in to change notification settings - Fork 1
/
add_data.py
178 lines (164 loc) · 6.37 KB
/
add_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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import requests
import pgeocode as pg
from pymongo import MongoClient
from requests import api
from pyzipcode import ZipCodeDatabase
client = MongoClient('localhost', 27017)
db = client.polar
# TODO:
# API_KEY 마스킹 할 것
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}
# TODO:
# 클라이언트에서 radius 받아 올 것
# zipcode는 아직 안쓸지도 모르는데, zipcode 기반 좌표함수에 넣어서 좌표 얻어낼 것
# Default radius = 5 miles (8046.72 m)
def insert_hospital(z, l, r=5):
location, radius = l, r * 1609.34
hospital_url = "https://maps.googleapis.com/maps/api/place/textsearch/json?inputtype=textquery&type=veterinary_care&key={api_key}&radius={radius}&location={location}".format(api_key=api_key, radius=radius, location=location)
data = requests.get(hospital_url, headers=headers).json()
hospitals = data["results"]
for h in hospitals:
name = h["name"]
business_status = h["business_status"]
address = h["formatted_address"]
zipcode = z
geometry = h["geometry"]["location"]
gplace_id = h["place_id"]
rating = h["rating"]
hos = db.hospital.find_one({'gplace_id': gplace_id}, {'_id':0})
doc = {
'name': name,
'business_status': business_status,
'address': address,
'zipcode': zipcode,
'geometry': geometry,
'gplace_id': gplace_id,
'rating': rating
}
db.hospital.insert_one(doc)
print('HOSPITAL INSERTION COMPLETE:', name)
# TODO:
# 상세정보용 디테일 insertion
# update part 수정할 것
def insert_details_hospital(gplace_id):
detail_url = "https://maps.googleapis.com/maps/api/place/details/json?key={api_key}&place_id={placeid}".format(api_key=api_key, placeid=gplace_id)
data = requests.get(detail_url, headers=headers).json()
details = data['result']
phone = details['formatted_phone_number']
# reviews = array type
reviews = details['reviews']
h = db.hospital.find_one({'gplace_id': gplace_id}, {'_id':0})
print(h)
doc = {
'name': h['name'],
'business_status': h['business_status'],
'address': h['address'],
'zipcode': h['zipcode'],
'geometry': h['geometry'],
'gplace_id': h['gplace_id'],
'rating': h['rating'],
'phone': phone,
'reviews': reviews
}
print(doc['name'])
db.hospital_details.insert_one(doc)
return doc
# TODO:
# 클라이언트에서 zipcode / loc , r 받아오기
# Default radius = 5 miles (8046.72 m)
def insert_shelter(z, l, r=5):
location, radius = l, r * 1609.34
shelter_url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?sensor=false&keyword='pet%20shelter'&key={api_key}&location={location}&radius={radius}".format(api_key=api_key, location=location, radius=radius)
data = requests.get(shelter_url,headers=headers).json()
shelters = data["results"]
for s in shelters:
name = s["name"]
business_status = s["business_status"]
address = s["vicinity"]
zipcode= z,
geometry = s["geometry"]["location"]
gplace_id = s["place_id"]
rating = s["rating"]
doc = {
'name': name,
'business_status': business_status,
'address': address,
'zipcode': zipcode,
'geometry': geometry,
'gplace_id': gplace_id,
'rating': rating
}
db.shelter.insert_one(doc)
print('SHELTER INSERTION COMPLETE: ', name)
# TODO:
# use petfinder API
def insert_tmppro(url):
return
# TODO:
# 클라이언트에서 zipcode 받아온 후 -> db에 없으면 calcloc 하고 db에 넣기
# 있으면 db에서 긁어오기
# calculate_loc(zipcode, country)
# used in insert_zipcode_loc(z, c)
# param: z = client input zipcode, c = client input country
# return: string of "latitude, longitude", used as input for google places API
def calculate_loc(z, c):
nomi = pg.Nominatim(c)
res = nomi.query_postal_code(z)
latitude = res["latitude"]
longitude = res["longitude"]
return "{lat}, {long}".format(lat=latitude, long=longitude)
# insert_zipcode_loc(z, c)
# param: z = client input zipcode, c = client input country
# return: None
def insert_zipcode_loc(z, c='us'):
zipcode, country = z, c
latlongstr = calculate_loc(z, country)
doc = {
'zipcode': zipcode,
'latlong': latlongstr
}
db.ziplatlong.insert_one(doc)
print('ZIPCODE INSERTION CCMOPLETE: country: {c} zipcode: {z} latlong: {l}'.format(c=country, z=zipcode, l=latlongstr))
return latlongstr
def get_near_zipcode(z, r=5):
zcdb = ZipCodeDatabase()
zipcode = str(z)
radius = int(r)
in_radius = [z.zip for z in zcdb.get_zipcodes_around_radius(zipcode, radius)] # ('ZIP', radius in miles)
for z in in_radius:
loc = db.ziplatlong.find_one({'zipcode':z},{'_id':0})
l = ""
if loc is None:
l = insert_zipcode_loc(z)
insert_shelter(z, l, radius)
insert_hospital(z, l, radius)
return in_radius
def get_near_hospital(z, r):
zipcode = z
radius = r
near_zipcode = get_near_zipcode(zipcode, radius)
res = []
for item in near_zipcode:
res.append(list(db.hospital.find({'zipcode':item},{'_id':0})))
return res
### Testing
if __name__=="__main__":
get_near_hospital(94608, 5)
# 할 일:
# 1. init_db.py
# 구글 places API에서 동물병원, 쉘터 리스트 받아온 후 name addr zipcode business_status 받아오기
# 가능하다면 google 리뷰도 받아올 것
# 2. 메인페이지 만들기
# 메인 검색창에서 우편번호 검색 후 -> 거리순 정렬로 (가까운 순으로) 병원, 쉘터 가져올 것 + 거리 출력 할 것
# 3. 검색 결과 페이지 만들기 (대부분 UI 디자인)
# 2번의 결과값 리스트를 가져와서 출력해주기
# 4. 검색 필터 넣어주기 + 임시보호 동물 API 가져올 것
# 5. 시설 상세페이지 가져오기 + 리뷰 가져오기
# 구글 리뷰 가능하면 가져올 것
# 리뷰 상세페이지 디자인
# 6. 로그인 기능 만들기 + 리뷰 작성 UI + No review UI
# 만들 페이지:
# 1. 메인 페이지
# 2. 로그인 페이지
# 3. 검색결과 페이지
# 4. 상세 페이지 (리뷰 페이지)