seawolf2357 commited on
Commit
3d7008a
·
verified ·
1 Parent(s): 3355310

Update file_api.py

Browse files
Files changed (1) hide show
  1. file_api.py +49 -24
file_api.py CHANGED
@@ -340,36 +340,61 @@ def fetch_all_from_api(category: str = "전체", region: str = "전체(지역)",
340
 
341
  def fetch_with_cache(category: str = "전체", region: str = "전체(지역)", keyword: str = "") -> Tuple[List[Dict], str]:
342
  """
343
- 캐시를 활용한 공고 조회
344
- - 필터(카테고리, 지역, 키워드) 없으면 캐시에서 빠르게 로드
345
- - 필터가 있으면 캐시에서 또는 API 직접 호출
346
  """
347
  if not CACHE_AVAILABLE:
348
  return fetch_all_from_api(category, region, keyword)
349
 
350
- # 필터가 없으면 캐시에서 전체 로드 (빠름!)
351
- if category == "전체" and region == "전체(지역)" and not keyword.strip():
352
  items, status = get_cached_announcements()
353
- return items, status
354
-
355
- # 키워드 검색이 있으면 캐시에서 검색
356
- if keyword.strip() and CACHE_AVAILABLE:
357
- try:
 
 
358
  cache = get_cache()
359
- items = cache.search(keyword.strip(), n_results=100)
360
- if items:
361
- # 추가 필터 적용
362
- filtered = items
363
- if category != "전체":
364
- filtered = [i for i in filtered if category.lower() in (i.get("lcategory", "") or "").lower()]
365
- if region != "전체(지역)":
366
- filtered = [i for i in filtered if region in (i.get("hashTags", "") or "") or region in (i.get("author", "") or "")]
367
- return filtered, f"🔍 캐시에서 '{keyword}' 검색: {len(filtered)}건"
368
- except Exception as e:
369
- print(f"Cache search error: {e}")
370
-
371
- # 필터가 있으면 API에서 직접 가져오기 (캐시 미사용)
372
- return fetch_all_from_api(category, region, keyword)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
373
 
374
 
375
  def download_file(url: str, save_dir: str, hint_filename: str = None) -> Tuple[Optional[str], Optional[str]]:
 
340
 
341
  def fetch_with_cache(category: str = "전체", region: str = "전체(지역)", keyword: str = "") -> Tuple[List[Dict], str]:
342
  """
343
+ 캐시를 활용한 공고 조회 (개선됨)
344
+ - 가능하면 캐시에서 필터링 (빠름!)
345
+ - 키워드가 있으면 캐시에서 검색
346
  """
347
  if not CACHE_AVAILABLE:
348
  return fetch_all_from_api(category, region, keyword)
349
 
350
+ try:
351
+ # 캐시에서 전체 로드 필터링 (API 호출보다 훨씬 빠름!)
352
  items, status = get_cached_announcements()
353
+
354
+ if not items:
355
+ # 캐시가 비어있으면 API에서 가져오기
356
+ return fetch_all_from_api(category, region, keyword)
357
+
358
+ # 키워드 검색 (벡터 검색)
359
+ if keyword.strip():
360
  cache = get_cache()
361
+ items = cache.search(keyword.strip(), n_results=500)
362
+ status = f"🔍 캐시에서 '{keyword}' 검색"
363
+
364
+ # 필터 적용 (캐시 데이터에서 필터링)
365
+ filtered = items
366
+ filter_info = []
367
+
368
+ # 카테고리 필터
369
+ if category and category != "전체":
370
+ filtered = [
371
+ i for i in filtered
372
+ if category.lower() in (i.get("lcategory", "") or i.get("pldirSportRealmLclasCodeNm", "") or "").lower()
373
+ ]
374
+ filter_info.append(f"분야:{category}")
375
+
376
+ # 지역 필터 (hashTags, author, title에서 검색)
377
+ if region and region != "전체(지역)":
378
+ region_filtered = []
379
+ for i in filtered:
380
+ hash_tags = i.get("hashTags", "") or ""
381
+ author = i.get("author", "") or i.get("jrsdInsttNm", "") or ""
382
+ title = i.get("title", "") or i.get("pblancNm", "") or ""
383
+
384
+ if region in hash_tags or region in author or region in title:
385
+ region_filtered.append(i)
386
+ filtered = region_filtered
387
+ filter_info.append(f"지역:{region}")
388
+
389
+ filter_str = ", ".join(filter_info) if filter_info else "전체"
390
+ result_status = f"⚡ 캐시에서 {len(filtered)}건 필터링 ({filter_str})"
391
+
392
+ return filtered, result_status
393
+
394
+ except Exception as e:
395
+ print(f"Cache filter error: {e}")
396
+ # 오류 시 API 폴백
397
+ return fetch_all_from_api(category, region, keyword)
398
 
399
 
400
  def download_file(url: str, save_dir: str, hint_filename: str = None) -> Tuple[Optional[str], Optional[str]]: