-
-
Save symoon94/53466b13bcccc133ce859395bbd4d81f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-*- coding: utf-8 -*- | |
import json | |
import requests | |
from tqdm import tqdm | |
DIST = 0.01 | |
HEADERS = { | |
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36', | |
'content-encoding': 'gzip', | |
'accept': 'application/json, text/javascript, */*; q=0.01', | |
} | |
TYPE = { | |
'': 'DINING', | |
'한식': 'DINING_KOREAN', | |
'일식': 'DINING_JAPANESE', | |
'중식': 'DINING_CHINESE', | |
} | |
SPOT_URL = 'https://map.naver.com/search2/interestSpot.nhn' | |
SPOT_PARAMS = { | |
'type': None, | |
'boundary': None, | |
'pageSize': 100, | |
} | |
SITE_INFO_URL = 'https://map.naver.com/search2/getSiteInfo.nhn' | |
SITE_INFO_DATA = { | |
'id': 'PerimeterInfo_{}', | |
'_': '', | |
} | |
LIKE_URL = 'https://common.like.naver.com/likeIt/v1/likeItServiceContentList.jsonp' | |
LIKE_DATA = { | |
'params': 'MAP[{}]' | |
} | |
def update(base, new): | |
assert all(key in base for key in new), \ | |
"Wrong keys: {}".format( | |
[key for key in new if key not in base]) | |
return dict(base, **new) | |
def read_json(text): | |
return json.loads(text) | |
class Restaurant: | |
def __init__(self, info): | |
self.info = { 'like': 0 } | |
self.info.update(info) | |
for k, v in info.items(): | |
setattr(self, k, v) | |
def __unicode__(self): | |
return u"\n".join(u"{}: {}".format(k, unicode(v)) \ | |
for k, v in self.info.items()) | |
def __repr__(self): | |
return self.__unicode__().encode('utf-8') | |
def __gt__(self, rest2): | |
return self.like > rest2.like | |
################# | |
# Spots | |
################# | |
def get_spots(params): | |
new_params = update(SPOT_PARAMS, params) | |
r = requests.get(SPOT_URL, params=new_params, headers=HEADERS) | |
return read_json(r.text)['result']['site'] | |
def get_boundary(x, y): | |
return ";".join("{:.07f}".format(num) for num in (x, y, x+DIST, y+DIST)) | |
def get_boundaries(x, y, n): | |
return [get_boundary(x+idx*DIST, y+idx*DIST) for idx in range(n)] | |
################# | |
# Site Info | |
################# | |
def get_site_info(data): | |
new_data = update(SITE_INFO_DATA, data) | |
r = requests.post(SITE_INFO_URL, data=new_data, headers=HEADERS) | |
return read_json(r.text)['result'] | |
################# | |
# Like | |
################# | |
def get_like(params): | |
new_params = update(LIKE_DATA, params) | |
r = requests.get(LIKE_URL, params=new_params, headers=HEADERS) | |
return read_json(r.text)['result']['contents'][0]['likeItCount'] | |
if __name__ == '__main__': | |
start_x, start_y = 126.9734451, 37.5602117 | |
chosen_type = raw_input('Category ([전체]/한식/중식/일식)? ') | |
_type = TYPE[chosen_type] | |
boundary_num = 1 | |
boundaries = get_boundaries(start_x, start_y, boundary_num) | |
spots = [] | |
for boundary in tqdm(boundaries, desc="Search boundaries"): | |
spot_params = { | |
'type': _type, | |
'boundary': boundary, | |
} | |
spots.extend(get_spots(spot_params)) | |
# INFO: only for debugging purpose | |
spots = spots[:5] | |
restaurants = [] | |
for spot in tqdm(spots, desc="Search spots"): | |
name = spot['name'] | |
_id = spot['id'] | |
site_info_data = { | |
'id': 'PerimeterInfo_{}'.format(_id), | |
} | |
site_info = get_site_info(site_info_data) | |
category = site_info['category'] | |
phone = site_info['phone'] | |
biz_hour = site_info['bizHour'] | |
like_data = { | |
'params': 'MAP[{}]'.format(_id[1:]), | |
} | |
like = get_like(like_data) | |
info = { | |
'name': name, | |
'phone': phone, | |
'like': like, | |
} | |
restaurant = Restaurant(info) | |
restaurants.append(restaurant) | |
restaurants.sort(reverse=True) | |
for restaurant in restaurants: | |
print("="*20) | |
print(restaurant) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment