Spaces:
Running
Running
| # ๋ถ์ ๊ฒฐ๊ณผ๋ฅผ Supabase ํ๋ซ ํ ์ด๋ธ(analysis_run/cluster/demographic)์ ์ ์ฌํ๋ ๋ชจ๋ | |
| import os | |
| from datetime import date | |
| from functools import lru_cache | |
| from supabase import create_client, Client | |
| def _client() -> Client: | |
| return create_client(os.environ["SUPABASE_URL"], os.environ["SUPABASE_SERVICE_KEY"]) | |
| def enabled() -> bool: | |
| # Supabase ์๊ฒฉ์ฆ๋ช ์ด ์์ ๋๋ง ์ ์ฌ | |
| return bool(os.environ.get("SUPABASE_URL") and os.environ.get("SUPABASE_SERVICE_KEY")) | |
| def persist(result: dict) -> None: | |
| # analyze ๊ฒฐ๊ณผ dict๋ฅผ (keyword, run_date) ๊ธฐ์ค์ผ๋ก upsert | |
| sb = _client() | |
| kw = result["keyword"] | |
| day = date.today().isoformat() | |
| ib = result.get("intent_breakdown") or {} | |
| sb.table("analysis_run").upsert({ | |
| "keyword": kw, "run_date": day, | |
| "total_search_volume": result.get("total_search_volume"), | |
| "keyword_count": result.get("keyword_count"), | |
| "cluster_count": result.get("cluster_count", 0), | |
| "intent_info": ib.get("info", 0), | |
| "intent_transactional": ib.get("transactional", 0), | |
| "intent_navigational": ib.get("navigational", 0), | |
| "intent_mixed": ib.get("mixed", 0), | |
| "marketing_suggestion": result.get("marketing_suggestion"), | |
| }, on_conflict="keyword,run_date").execute() | |
| # ์ฌ์ ์ฌ ์ ์ค๋ณต ๋ฐฉ์ง: ๊ฐ์ ํค์๋ยท๋ ์ง ํ์ ์ง์ฐ๊ณ ๋ค์ ๋ฃ์ | |
| clusters = result.get("clusters") or [] | |
| sb.table("analysis_cluster").delete().eq("keyword", kw).eq("run_date", day).execute() | |
| if clusters: | |
| sb.table("analysis_cluster").insert([{ | |
| "keyword": kw, "run_date": day, | |
| "theme": c.get("theme"), "rep_keyword": c.get("cluster_label"), | |
| "intent": c.get("intent"), "total_search_volume": c.get("total_search_volume"), | |
| "keyword_count": c.get("keyword_count"), | |
| "top_keywords": ", ".join(c.get("top_keywords", [])), | |
| } for c in clusters]).execute() | |
| demo = result.get("demographics") | |
| sb.table("analysis_demographic").delete().eq("keyword", kw).eq("run_date", day).execute() | |
| if demo and demo.get("cells"): | |
| sb.table("analysis_demographic").insert([{ | |
| "keyword": kw, "run_date": day, | |
| "gender": x.get("gender"), "age": x.get("age"), "pct": x.get("pct"), | |
| } for x in demo["cells"]]).execute() | |