1
1
from fastapi import Request , APIRouter , Depends , HTTPException
2
2
import requests , os , random
3
- import xml .etree .ElementTree as ET
4
- import googlemaps
3
+ from sqlalchemy .orm import Session
4
+ from .auth import get_current_user
5
+ from ..db .session import get_db
5
6
from dotenv import load_dotenv
7
+ from ..models .Spot import Spot
8
+ from ..models .Stay import Stay
6
9
7
10
load_dotenv ()
8
11
API_KEY = os .getenv ("API_KEY" )
25
28
26
29
27
30
def get_additional_info (location , query ):
28
- print ("api" , location , query )
29
31
url = "http://apis.data.go.kr/B551011/KorService1/searchKeyword1"
30
32
params = {
31
33
"serviceKey" : API_KEY ,
@@ -64,11 +66,8 @@ def get_places(query, location, radius):
64
66
data = response .json ()
65
67
return data ["results" ]
66
68
except requests .exceptions .JSONDecodeError as e :
67
- print (f"JSONDecodeError: { e } " )
68
- print ("Response content:" , response .text )
69
69
raise e
70
70
else :
71
- print (f"Error: { response .status_code } " )
72
71
raise HTTPException (status_code = 400 , detail = "No data found in get_places" )
73
72
74
73
@@ -78,15 +77,12 @@ def get_lat_lng(location):
78
77
response = requests .get (GOOGLE_URL , params = params )
79
78
if response .status_code == 200 :
80
79
data = response .json ()
81
- # print("Geocoding API response:", data)
82
80
if data ["results" ]:
83
81
location = data ["results" ][0 ]["geometry" ]["location" ]
84
82
return location ["lat" ], location ["lng" ]
85
83
else :
86
- print ("No results found for the location." )
87
84
raise ValueError ("No results found for the location." )
88
85
else :
89
- print (f"Error: { response .status_code } " )
90
86
raise HTTPException (status_code = 400 , detail = "No data found in get_lat_lng" )
91
87
92
88
@@ -120,44 +116,44 @@ def sentence(request: Request):
120
116
121
117
# 1. 문장 먼저 주기
122
118
@router .get ("/list" )
123
- async def main (request : Request , region_idx : int , keyword_idx : int ):
119
+ async def main (
120
+ request : Request , region_idx : int , keyword_idx : int , db : Session = Depends (get_db )
121
+ ):
122
+ current_user = get_current_user (request , db )
123
+ if current_user is None :
124
+ raise HTTPException (
125
+ status_code = 400 ,
126
+ detail = f"user not found. request.header.Authorization: { request .headers .get ('Authorization' )} " ,
127
+ )
128
+ else :
129
+ stay_wish = db .query (Stay ).filter (Stay .user_id == current_user .id ).all ()
130
+ spot_wish = db .query (Spot ).filter (Spot .user_id == current_user .id ).all ()
131
+ wishs = [wish .content_id for wish in stay_wish + spot_wish ]
132
+
124
133
if region_idx < 0 or region_idx >= len (keys ):
125
134
raise HTTPException (status_code = 400 , detail = "Invalid region index" )
126
135
if keyword_idx < 0 or keyword_idx >= len (LOCATION_QUERY [keys [region_idx ]]):
127
136
raise HTTPException (status_code = 400 , detail = "Invalid keyword index" )
128
137
location = keys [region_idx ] # 예: "부산"
129
138
query = LOCATION_QUERY [location ][keyword_idx ] # 예: "부산 관광지"
130
- # print(location, query)
131
139
radius = 10000 # 반경 10km
132
- # 도시 이름으로 위도와 경도 가져오기
133
- lat , lng = get_lat_lng (location )
134
- # if lat is None or lng is None:
135
- # print("위도와 경도를 가져올 수 없습니다.")
136
- # return
137
-
138
- lat_lng = f"{ lat } ,{ lng } "
139
140
140
141
# 관광지 검색
141
142
places = get_places (location + query , location , radius )
142
- # if not places:
143
- # print("관광지 없음")
144
- # return
145
143
146
144
# 각 관광지의 리뷰 수와 이름 가져오기
147
145
places_with_reviews = []
148
146
for place in places :
149
- place_id = place ["place_id" ]
150
147
place_name = place ["name" ]
151
148
reviews = place .get ("user_ratings_total" ) # 리뷰 수
152
149
if reviews is not None :
153
150
places_with_reviews .append ((place_name , reviews ))
154
151
155
152
# 리뷰 수로 정렬 (내림차순: 리뷰가 많은 순)
156
153
places_with_reviews .sort (key = lambda x : x [1 ], reverse = True )
157
- # print(places_with_reviews)
158
154
159
155
tour_result = get_additional_info (location , query )
160
- # print(tour_result)
156
+
161
157
titles = [item ["title" ] for item in tour_result ]
162
158
result = []
163
159
# # 결과 출력 및 추가 정보 가져오기 (추가 정보가 없는 경우 제외)
@@ -167,6 +163,9 @@ async def main(request: Request, region_idx: int, keyword_idx: int):
167
163
result .append (tour_result [idx ])
168
164
169
165
if len (result ):
166
+ if wishs :
167
+ for item in result :
168
+ item ["inWish" ] = item ["contentid" ] in wishs
170
169
return result
171
170
else :
172
171
raise HTTPException (status_code = 500 , detail = "No data found in google reccomend" )
0 commit comments