Toggle navigation
Toggle navigation
This project
Loading...
Sign in
wlstp8473
/
telegram_Messenger_Chatbot
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
wlstp8473
2021-06-10 06:18:27 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
bd9fa0bf7cdb27f713f4c15ec3053631284712b6
bd9fa0bf
1 parent
714ea274
search_restaurant
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
153 additions
and
0 deletions
search_restaurant.py
search_restaurant.py
0 → 100644
View file @
bd9fa0b
# kakao local api 컨트롤러 참고자료: https://wooiljeong.github.io/python/kakao_local_api/
# 파이썬 json 특정값 추출 참고자료: https://hashcode.co.kr/questions/8852/%ED%8C%8C%EC%9D%B4%EC%8D%AC-json-%ED%8A%B9%EC%A0%95%EA%B0%92-%EC%B6%94%EC%B6%9C
import
json
import
requests
from
pprint
import
pprint
from
io
import
StringIO
#return_print 하기 위해
class
KakaoLocalAPI
:
"""
Kakao Local API 컨트롤러
"""
def
__init__
(
self
,
rest_api_key
):
"""
Rest API키 초기화 및 기능 별 URL 설정
"""
# REST API 키 설정
self
.
rest_api_key
=
"1c74642c092d28c438467486c366c6cf"
self
.
headers
=
{
"Authorization"
:
"KakaoAK {}"
.
format
(
rest_api_key
)}
# 서비스 URL 설정
# 05 키워드 검색
self
.
URL_05
=
"https://dapi.kakao.com/v2/local/search/keyword.json"
def
search_keyword
(
self
,
query
,
category_group_code
=
None
,
x
=
None
,
y
=
None
,
radius
=
None
,
rect
=
None
,
page
=
None
,
size
=
None
,
sort
=
None
):
"""
05 키워드 검색
"""
#params = {'query': f"{query}",
# 'x': f"{x}",
# 'y': f"{y}"}
params
=
{
'query'
:
f
"{query}"
,
'x'
:
f
"{x}"
,
'y'
:
f
"{y}"
,}
if
radius
!=
None
:
params
[
'radius'
]
=
f
"{radius}"
if
rect
!=
None
:
params
[
'rect'
]
=
f
"{rect}"
if
page
!=
None
:
params
[
'page'
]
=
f
"{page}"
if
size
!=
None
:
params
[
'size'
]
=
f
"{params}"
if
sort
!=
None
:
params
[
'sort'
]
=
f
"{sort}"
res
=
requests
.
get
(
self
.
URL_05
,
headers
=
self
.
headers
,
params
=
params
)
document
=
json
.
loads
(
res
.
text
)
return
document
# REST API 키
rest_api_key
=
"1c74642c092d28c438467486c366c6cf"
kakao
=
KakaoLocalAPI
(
rest_api_key
)
## Set params
query
=
"인덕원 닭갈비"
#검색 키워드
x
=
126.98083352703802
#내 위치 위도와 경도
y
=
37.39403042340368
radius
=
1
#반경 km 검색
## Request
result_5
=
kakao
.
search_keyword
(
query
,
x
,
y
,
radius
)
# + json파일 확인해보기 이후 데이터를 추출해야함
#json_string = json.dumps(result_5, indent=2)
#print(json_string)
#json파일 특정 키워드 추출
#추출할 리스트 place_name, place_url, x, y (x y는 위치임), 검색키워드, 선택 지역(위치)
#from pprint import pprint
same_name_list
=
[
x_
for
x_
in
result_5
[
'meta'
][
'same_name'
]
.
values
()]
keyword
=
"검색 키워드: "
+
same_name_list
[
0
]
selected_region
=
"검색 위치: "
+
same_name_list
[
2
]
total_count
=
{
result_5
[
'meta'
][
'total_count'
]}
#set은 파싱이 불가능하기 때문에 list나 튜플로 바꿔야함, list로 바꾼 후 인덱스를 활용하여 int로 바꾼 후 문자열과 더하기 위해 str로 형변환
total_count_list
=
list
(
total_count
)
total_count
=
"총 검색 개수: "
+
str
(
total_count_list
[
0
])
#print(keyword) #메제지들은 텔레그렘 봇으로 보내기!!
#print(selected_region)
#print(total_count)
#print("----------")
place_name_list
=
[
x_
[
'place_name'
]
for
x_
in
result_5
[
'documents'
]]
#print(place_name_list)
#print('------')
phone_list
=
[
x_
[
'phone'
]
for
x_
in
result_5
[
'documents'
]]
#print(phone_list)
#print('------')
place_url_list
=
[
x_
[
'place_url'
]
for
x_
in
result_5
[
'documents'
]]
#print(place_url_list)
#print('------')
#print(x_list) 위치 시각화 할때 사용
x_list
=
[
x_
[
'x'
]
for
x_
in
result_5
[
'documents'
]]
y_list
=
[
x_
[
'y'
]
for
x_
in
result_5
[
'documents'
]]
#print(y_list)
#print(x_list)
#print('------')
road_address_name_list
=
[
x_
[
'road_address_name'
]
for
x_
in
result_5
[
'documents'
]]
#print(road_address_name_list)
#print('------')
resturant_info_list
=
[]
total_count_num
=
str
(
total_count_list
[
0
])
total_count_num
=
int
(
total_count_num
)
resturant_info
=
"[ 맛집 정보 ]"
#프린트 결과를 리턴받기 위한 함수
#from io import StringIO
def
return_print
(
*
message
):
io
=
StringIO
()
print
(
*
message
,
file
=
io
,
end
=
""
)
return
io
.
getvalue
()
print
(
resturant_info
)
# 봇 메세지 ㄱㄱ
for
i
in
range
(
0
,
10
):
#total_count_num가 너무 크면 인덱스 범위가 벗어나서 실행이 안됨, 임시로 5개까지만
resturant_info_print
=
print
([
place_name_list
[
i
]
+
" "
+
phone_list
[
i
]
+
" "
+
road_address_name_list
[
i
]
+
" "
+
place_url_list
[
i
]])
#resturant_info_list = [place_name_list[i] + " " +phone_list[i] + " " + road_address_name_list[i] + " " + place_url_list[i]]
#resturant_info += return_print(resturant_info_list)
Please
register
or
login
to post a comment