fix(insta-lab): Google Trends — RSS endpoint도 404 폐기, dailytrends JSON API로 교체
Google이 /trends/trendingsearches/daily/rss?geo=KR도 404로 폐기 (직전 fix에서 RSS로 교체했으나 NAS에서 실제 호출 시 404 확인). 대안으로 비공식 /trends/api/dailytrends?hl=ko&tz=-540&geo=KR&ns=15 JSON API로 교체. 응답 앞 `)]}'` XSSI 보호 prefix는 정규식으로 자르고 JSON 파싱. 중복 키워드 제거 + 등장 순서 보존.
This commit is contained in:
@@ -77,18 +77,27 @@ def test_classify_keyword_with_cache(monkeypatch):
|
||||
assert calls["n"] == 1
|
||||
|
||||
|
||||
def test_fetch_google_trends_parses_rss_and_classifies(tmp_db, monkeypatch):
|
||||
fake_rss = """<?xml version="1.0" encoding="UTF-8"?>
|
||||
<rss version="2.0">
|
||||
<channel>
|
||||
<title>Daily Search Trends</title>
|
||||
<item><title>기준금리</title></item>
|
||||
<item><title>BTS 컴백</title></item>
|
||||
<item><title>스트레스 관리</title></item>
|
||||
</channel>
|
||||
</rss>"""
|
||||
def test_fetch_google_trends_parses_json_and_classifies(tmp_db, monkeypatch):
|
||||
import json as _json
|
||||
payload = {
|
||||
"default": {
|
||||
"trendingSearchesDays": [
|
||||
{
|
||||
"date": "20260517",
|
||||
"trendingSearches": [
|
||||
{"title": {"query": "기준금리"}},
|
||||
{"title": {"query": "BTS 컴백"}},
|
||||
{"title": {"query": "스트레스 관리"}},
|
||||
# 다음 날 데이터에 중복 키워드 — 중복 제거 확인
|
||||
{"title": {"query": "기준금리"}},
|
||||
],
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
fake_resp = MagicMock()
|
||||
fake_resp.text = fake_rss
|
||||
# 실제 Google 응답 형태: `)]}',\n` XSSI prefix가 앞에 붙음
|
||||
fake_resp.text = ")]}',\n" + _json.dumps(payload, ensure_ascii=False)
|
||||
fake_resp.raise_for_status.return_value = None
|
||||
monkeypatch.setattr(trend_collector.requests, "get", lambda *a, **kw: fake_resp)
|
||||
monkeypatch.setattr(trend_collector, "classify_keyword",
|
||||
@@ -97,6 +106,7 @@ def test_fetch_google_trends_parses_rss_and_classifies(tmp_db, monkeypatch):
|
||||
|
||||
trends = trend_collector.fetch_google_trends()
|
||||
by_kw = {t["keyword"]: t for t in trends}
|
||||
assert set(by_kw.keys()) == {"기준금리", "BTS 컴백", "스트레스 관리"} # 중복 제거됨
|
||||
assert by_kw["기준금리"]["category"] == "economy"
|
||||
assert by_kw["BTS 컴백"]["category"] == "celebrity"
|
||||
assert by_kw["스트레스 관리"]["category"] == "psychology"
|
||||
@@ -112,7 +122,7 @@ def test_collect_all_invokes_both_sources(tmp_db, monkeypatch):
|
||||
assert out == {"naver_popular": 5, "google_trends": 3}
|
||||
|
||||
|
||||
def test_fetch_google_trends_graceful_on_rss_failure(monkeypatch):
|
||||
def test_fetch_google_trends_graceful_on_api_failure(monkeypatch):
|
||||
fake_resp = MagicMock()
|
||||
fake_resp.raise_for_status.side_effect = RuntimeError("Google returned 404")
|
||||
monkeypatch.setattr(trend_collector.requests, "get", lambda *a, **kw: fake_resp)
|
||||
|
||||
Reference in New Issue
Block a user