Kims12 commited on
Commit
8ac20cc
ยท
verified ยท
1 Parent(s): 182e12f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -23
app.py CHANGED
@@ -12,7 +12,6 @@ import time
12
  import hmac
13
  import hashlib
14
  import base64
15
- import concurrent.futures # ๋™์‹œ ์‹คํ–‰์„ ์œ„ํ•œ ๋ชจ๋“ˆ
16
 
17
  # ๋””๋ฒ„๊น…(๋กœ๊ทธ)์šฉ ํ•จ์ˆ˜
18
  def debug_log(message: str):
@@ -144,7 +143,7 @@ def get_header(method, uri, api_key, secret_key, customer_id):
144
  "X-Signature": signature
145
  }
146
 
147
- # ๊ธฐ์กด ๋‹จ์ผ ํ‚ค์›Œ๋“œ์šฉ ํ•จ์ˆ˜ (์ฐธ์กฐ์šฉ)
148
  def fetch_related_keywords(keyword):
149
  debug_log(f"fetch_related_keywords ํ˜ธ์ถœ, ํ‚ค์›Œ๋“œ: {keyword}")
150
  API_KEY = os.environ["NAVER_API_KEY"]
@@ -181,7 +180,8 @@ def fetch_related_keywords(keyword):
181
  debug_log("fetch_related_keywords ์™„๋ฃŒ")
182
  return result_df
183
 
184
- # ์‹ ๊ทœ ์ถ”๊ฐ€: ํ‚ค์›Œ๋“œ 10๊ฐœ์”ฉ ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์–ด ํ•œ ๋ฒˆ์— ๊ฒ€์ƒ‰๋Ÿ‰์„ ์กฐํšŒํ•˜๋Š” ํ•จ์ˆ˜
 
185
  def fetch_related_keywords_batch(keywords: list):
186
  debug_log(f"fetch_related_keywords_batch ํ˜ธ์ถœ, ํ‚ค์›Œ๋“œ ๊ทธ๋ฃน: {keywords}")
187
  API_KEY = os.environ["NAVER_API_KEY"]
@@ -192,9 +192,8 @@ def fetch_related_keywords_batch(keywords: list):
192
  uri = "/keywordstool"
193
  method = "GET"
194
  headers = get_header(method, uri, API_KEY, SECRET_KEY, CUSTOMER_ID)
195
- # API์— ์—ฌ๋Ÿฌ ํ‚ค์›Œ๋“œ๋ฅผ ์ „๋‹ฌํ•  ๋•Œ๋Š” ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ ๊ทธ๋Œ€๋กœ ์ „๋‹ฌ
196
  params = {
197
- "hintKeywords": keywords,
198
  "showDetail": "1"
199
  }
200
  response = requests.get(BASE_URL + uri, params=params, headers=headers)
@@ -219,29 +218,18 @@ def fetch_related_keywords_batch(keywords: list):
219
  debug_log("fetch_related_keywords_batch ์™„๋ฃŒ")
220
  return result_df
221
 
222
- # process_keyword ํ•จ์ˆ˜๋ฅผ ๋™์‹œ ์‹คํ–‰์œผ๋กœ ๊ฐ ๊ทธ๋ฃน(์ตœ๋Œ€ 10๊ฐœ์”ฉ)์— ๋Œ€ํ•ด ํ•œ ๋ฒˆ์”ฉ API ํ˜ธ์ถœํ•˜๋„๋ก ๊ฐœ์„ 
223
  def process_keyword(keywords: str, include_related: bool):
224
  debug_log(f"process_keyword ํ˜ธ์ถœ, ํ‚ค์›Œ๋“œ๋“ค: {keywords}, ์—ฐ๊ด€๊ฒ€์ƒ‰์–ด ํฌํ•จ: {include_related}")
225
  input_keywords = [k.strip() for k in keywords.splitlines() if k.strip()]
226
  groups = [input_keywords[i:i+10] for i in range(0, len(input_keywords), 10)]
227
  result_dfs = []
228
- group_results = {}
229
-
230
- # ๊ทธ๋ฃน๋ณ„ API ํ˜ธ์ถœ์„ ๋™์‹œ ์‹คํ–‰
231
- with concurrent.futures.ThreadPoolExecutor() as executor:
232
- future_to_group = {executor.submit(fetch_related_keywords_batch, group): group for group in groups}
233
- for future in concurrent.futures.as_completed(future_to_group):
234
- group = future_to_group[future]
235
- try:
236
- df_batch = future.result()
237
- group_results[tuple(group)] = df_batch
238
- except Exception as exc:
239
- debug_log(f"๊ทธ๋ฃน {group}์—์„œ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {exc}")
240
-
241
- # ๊ฐ ๊ทธ๋ฃน์˜ ๊ฒฐ๊ณผ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์ฒ˜๋ฆฌ
242
  for idx, group in enumerate(groups):
243
- df_batch = group_results.get(tuple(group))
244
- if df_batch is None or df_batch.empty:
 
245
  continue
246
  # ๊ทธ๋ฃน ๋‚ด ๊ฐ ํ‚ค์›Œ๋“œ์— ๋Œ€ํ•ด ๊ฒฐ๊ณผ ์ถ”์ถœ
247
  for kw in group:
@@ -250,12 +238,13 @@ def process_keyword(keywords: str, include_related: bool):
250
  result_dfs.append(row_kw)
251
  else:
252
  result_dfs.append(df_batch.head(1))
253
- # ์ฒซ ๋ฒˆ์งธ ๊ทธ๋ฃน์— ๋Œ€ํ•ด ์—ฐ๊ด€๊ฒ€์ƒ‰์–ด ์˜ต์…˜ ์ ์šฉ (์ฒซ ํ‚ค์›Œ๋“œ ์ œ์™ธ)
254
  if include_related and idx == 0:
255
  first_keyword = group[0]
256
  df_related = df_batch[df_batch["์ •๋ณดํ‚ค์›Œ๋“œ"] != first_keyword]
257
  if not df_related.empty:
258
  result_dfs.append(df_related)
 
259
 
260
  if result_dfs:
261
  result_df = pd.concat(result_dfs, ignore_index=True)
 
12
  import hmac
13
  import hashlib
14
  import base64
 
15
 
16
  # ๋””๋ฒ„๊น…(๋กœ๊ทธ)์šฉ ํ•จ์ˆ˜
17
  def debug_log(message: str):
 
143
  "X-Signature": signature
144
  }
145
 
146
+ # ๊ธฐ์กด ๋‹จ์ผ ํ‚ค์›Œ๋“œ์šฉ ํ•จ์ˆ˜ (์ฐธ๊ณ ์šฉ)
147
  def fetch_related_keywords(keyword):
148
  debug_log(f"fetch_related_keywords ํ˜ธ์ถœ, ํ‚ค์›Œ๋“œ: {keyword}")
149
  API_KEY = os.environ["NAVER_API_KEY"]
 
180
  debug_log("fetch_related_keywords ์™„๋ฃŒ")
181
  return result_df
182
 
183
+ # ์‹ ๊ทœ ์ถ”๊ฐ€: ํ‚ค์›Œ๋“œ 10๊ฐœ์”ฉ ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์–ด ํ•œ ๋ฒˆ์˜ API ํ˜ธ์ถœ์„ ํ•˜๋Š” ํ•จ์ˆ˜
184
+ # (๋‹จ, ๊ฐ ๊ทธ๋ฃน์€ ์ˆœ์ฐจ์ ์œผ๋กœ ํ˜ธ์ถœ๋จ)
185
  def fetch_related_keywords_batch(keywords: list):
186
  debug_log(f"fetch_related_keywords_batch ํ˜ธ์ถœ, ํ‚ค์›Œ๋“œ ๊ทธ๋ฃน: {keywords}")
187
  API_KEY = os.environ["NAVER_API_KEY"]
 
192
  uri = "/keywordstool"
193
  method = "GET"
194
  headers = get_header(method, uri, API_KEY, SECRET_KEY, CUSTOMER_ID)
 
195
  params = {
196
+ "hintKeywords": keywords, # ๋ฆฌ์ŠคํŠธ ๊ทธ๋Œ€๋กœ ์ „๋‹ฌ (์ตœ๋Œ€ 10๊ฐœ)
197
  "showDetail": "1"
198
  }
199
  response = requests.get(BASE_URL + uri, params=params, headers=headers)
 
218
  debug_log("fetch_related_keywords_batch ์™„๋ฃŒ")
219
  return result_df
220
 
221
+ # process_keyword ํ•จ์ˆ˜๋ฅผ ๊ทธ๋ฃน๋ณ„๋กœ(๊ฐ ๊ทธ๋ฃน์€ ์ˆœ์ฐจ์ ์œผ๋กœ) ์ฒ˜๋ฆฌํ•˜๋„๋ก ๊ฐœ์„ 
222
  def process_keyword(keywords: str, include_related: bool):
223
  debug_log(f"process_keyword ํ˜ธ์ถœ, ํ‚ค์›Œ๋“œ๋“ค: {keywords}, ์—ฐ๊ด€๊ฒ€์ƒ‰์–ด ํฌํ•จ: {include_related}")
224
  input_keywords = [k.strip() for k in keywords.splitlines() if k.strip()]
225
  groups = [input_keywords[i:i+10] for i in range(0, len(input_keywords), 10)]
226
  result_dfs = []
227
+
228
+ # ๊ฐ ๊ทธ๋ฃน์„ ์ˆœ์ฐจ์ ์œผ๋กœ ์ฒ˜๋ฆฌ (๋™์‹œ์— ํ˜ธ์ถœํ•˜์ง€ ์•Š์Œ)
 
 
 
 
 
 
 
 
 
 
 
 
229
  for idx, group in enumerate(groups):
230
+ debug_log(f"๊ทธ๋ฃน {idx+1} ์ฒ˜๋ฆฌ ์‹œ์ž‘: {group}")
231
+ df_batch = fetch_related_keywords_batch(group)
232
+ if df_batch.empty:
233
  continue
234
  # ๊ทธ๋ฃน ๋‚ด ๊ฐ ํ‚ค์›Œ๋“œ์— ๋Œ€ํ•ด ๊ฒฐ๊ณผ ์ถ”์ถœ
235
  for kw in group:
 
238
  result_dfs.append(row_kw)
239
  else:
240
  result_dfs.append(df_batch.head(1))
241
+ # ์ฒซ ๋ฒˆ์งธ ๊ทธ๋ฃน์— ๋Œ€ํ•ด์„œ๋งŒ ์—ฐ๊ด€๊ฒ€์ƒ‰์–ด ์˜ต์…˜ ์ ์šฉ (์ฒซ ํ‚ค์›Œ๋“œ ์ œ์™ธ)
242
  if include_related and idx == 0:
243
  first_keyword = group[0]
244
  df_related = df_batch[df_batch["์ •๋ณดํ‚ค์›Œ๋“œ"] != first_keyword]
245
  if not df_related.empty:
246
  result_dfs.append(df_related)
247
+ debug_log(f"๊ทธ๋ฃน {idx+1} ์ฒ˜๋ฆฌ ์™„๋ฃŒ")
248
 
249
  if result_dfs:
250
  result_df = pd.concat(result_dfs, ignore_index=True)