Spaces:
Sleeping
Sleeping
Commit ·
3ef98d0
1
Parent(s): 2680851
fix: normalize search_volume (int→str), relevance (float→int), competition (lower→Title), post_count (int→str) for LongCat API responses
Browse files- services/utils.py +15 -0
services/utils.py
CHANGED
|
@@ -162,8 +162,23 @@ def normalize_response(data: dict) -> dict:
|
|
| 162 |
|
| 163 |
if "core_keywords" in data:
|
| 164 |
data["core_keywords"] = normalize_field(data["core_keywords"], "keyword_list")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
if "viral_hashtags" in data:
|
| 166 |
data["viral_hashtags"] = normalize_field(data["viral_hashtags"], "hashtag_list")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
if "pin_ideas" in data:
|
| 169 |
data["pin_ideas"] = normalize_field(data["pin_ideas"], "pin_list")
|
|
|
|
| 162 |
|
| 163 |
if "core_keywords" in data:
|
| 164 |
data["core_keywords"] = normalize_field(data["core_keywords"], "keyword_list")
|
| 165 |
+
# LongCat returns search_volume as int, relevance as float → fix types
|
| 166 |
+
for kw in data["core_keywords"]:
|
| 167 |
+
if isinstance(kw, dict):
|
| 168 |
+
if not isinstance(kw.get("search_volume"), str):
|
| 169 |
+
kw["search_volume"] = str(kw.get("search_volume", "Medium"))
|
| 170 |
+
if isinstance(kw.get("relevance"), float):
|
| 171 |
+
kw["relevance"] = int(kw["relevance"] * 100) if kw["relevance"] < 1 else int(kw["relevance"])
|
| 172 |
+
if kw.get("competition") is None:
|
| 173 |
+
kw["competition"] = "Medium"
|
| 174 |
+
elif isinstance(kw["competition"], str):
|
| 175 |
+
kw["competition"] = kw["competition"].title()
|
| 176 |
if "viral_hashtags" in data:
|
| 177 |
data["viral_hashtags"] = normalize_field(data["viral_hashtags"], "hashtag_list")
|
| 178 |
+
# LongCat returns post_count as int → convert to str
|
| 179 |
+
for h in data["viral_hashtags"]:
|
| 180 |
+
if isinstance(h, dict) and not isinstance(h.get("post_count"), str):
|
| 181 |
+
h["post_count"] = str(h.get("post_count", "N/A"))
|
| 182 |
|
| 183 |
if "pin_ideas" in data:
|
| 184 |
data["pin_ideas"] = normalize_field(data["pin_ideas"], "pin_list")
|