Update file_api.py
Browse files- 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 |
-
-
|
| 346 |
"""
|
| 347 |
if not CACHE_AVAILABLE:
|
| 348 |
return fetch_all_from_api(category, region, keyword)
|
| 349 |
|
| 350 |
-
|
| 351 |
-
|
| 352 |
items, status = get_cached_announcements()
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
|
|
|
|
|
|
| 358 |
cache = get_cache()
|
| 359 |
-
items = cache.search(keyword.strip(), n_results=
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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]]:
|