Spaces:
Sleeping
Sleeping
Update services/trend_service.py
Browse files- services/trend_service.py +79 -13
services/trend_service.py
CHANGED
|
@@ -128,24 +128,90 @@ async def search_web_trends(query: str, site_filter: Optional[str] = None) -> li
|
|
| 128 |
]
|
| 129 |
|
| 130 |
|
| 131 |
-
# ββ
|
| 132 |
|
| 133 |
-
async def
|
| 134 |
"""
|
| 135 |
-
|
| 136 |
-
|
|
|
|
| 137 |
"""
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
return_exceptions=True # Don't crash if one source fails
|
| 145 |
)
|
| 146 |
|
|
|
|
|
|
|
|
|
|
| 147 |
return {
|
| 148 |
-
"reddit":
|
| 149 |
-
"google_trending":
|
| 150 |
-
"web_discussions":
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
]
|
| 129 |
|
| 130 |
|
| 131 |
+
# ββ X (Twitter) chatter ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 132 |
|
| 133 |
+
async def fetch_x_chatter(niche: str) -> list[dict]:
|
| 134 |
"""
|
| 135 |
+
Surfaces what people are saying about the niche on X (Twitter).
|
| 136 |
+
Uses Google Custom Search restricted to twitter.com / x.com (free, 100/day).
|
| 137 |
+
Gracefully returns [] when CSE isn't configured.
|
| 138 |
"""
|
| 139 |
+
twitter = await search_web_trends(niche, site_filter="twitter.com")
|
| 140 |
+
if twitter:
|
| 141 |
+
for t in twitter:
|
| 142 |
+
t["source"] = "x"
|
| 143 |
+
return twitter
|
| 144 |
+
# Fallback to the newer x.com domain if twitter.com returns nothing
|
| 145 |
+
x_dot = await search_web_trends(niche, site_filter="x.com")
|
| 146 |
+
for t in x_dot:
|
| 147 |
+
t["source"] = "x"
|
| 148 |
+
return x_dot
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
# ββ News ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 152 |
|
| 153 |
+
async def fetch_news(niche: str) -> list[dict]:
|
| 154 |
+
"""
|
| 155 |
+
Pulls recent news / announcements relevant to the niche.
|
| 156 |
+
Uses Google Custom Search with a news-leaning query (last 7 days).
|
| 157 |
+
Free: shares the 100 queries/day CSE budget.
|
| 158 |
+
"""
|
| 159 |
+
items = await search_web_trends(f"{niche} news OR launch OR announcement OR update")
|
| 160 |
+
for it in items:
|
| 161 |
+
it["source"] = "news"
|
| 162 |
+
return items
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
# ββ Niche-aware Google Trends interest βββββββββββββββββββββββββββββββββββββββ
|
| 166 |
+
|
| 167 |
+
async def fetch_niche_interest(keywords: list[str]) -> dict:
|
| 168 |
+
"""
|
| 169 |
+
Interest-over-time for niche keywords (so 'trending' is grounded in real
|
| 170 |
+
velocity, not just whatever is globally hot). Free, no API key.
|
| 171 |
+
"""
|
| 172 |
+
kws = [k for k in (keywords or []) if k][:5]
|
| 173 |
+
if not kws:
|
| 174 |
+
return {}
|
| 175 |
+
try:
|
| 176 |
+
return await fetch_google_trends(kws, timeframe="now 7-d")
|
| 177 |
+
except Exception:
|
| 178 |
+
return {}
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
# ββ Aggregate Trend Scanner βββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 182 |
+
|
| 183 |
+
async def aggregate_trends(
|
| 184 |
+
niche: str,
|
| 185 |
+
subreddits: list[str],
|
| 186 |
+
interest_keywords: Optional[list[str]] = None,
|
| 187 |
+
) -> dict:
|
| 188 |
+
"""
|
| 189 |
+
Runs every free trend source in parallel for a given creator niche:
|
| 190 |
+
Reddit, Google Trends (global + niche interest), X/Twitter, News and the web.
|
| 191 |
+
Returns a unified trend report ready for AI analysis. No single source
|
| 192 |
+
failing can crash the scan (return_exceptions=True).
|
| 193 |
+
"""
|
| 194 |
+
reddit_task = fetch_reddit_trends(subreddits, limit=10)
|
| 195 |
+
trends_task = fetch_trending_searches()
|
| 196 |
+
web_task = search_web_trends(niche)
|
| 197 |
+
x_task = fetch_x_chatter(niche)
|
| 198 |
+
news_task = fetch_news(niche)
|
| 199 |
+
interest_task = fetch_niche_interest(interest_keywords or [niche])
|
| 200 |
+
|
| 201 |
+
(reddit_data, trending_searches, web_data,
|
| 202 |
+
x_data, news_data, niche_interest) = await asyncio.gather(
|
| 203 |
+
reddit_task, trends_task, web_task, x_task, news_task, interest_task,
|
| 204 |
return_exceptions=True # Don't crash if one source fails
|
| 205 |
)
|
| 206 |
|
| 207 |
+
def ok(v, default):
|
| 208 |
+
return v if not isinstance(v, Exception) else default
|
| 209 |
+
|
| 210 |
return {
|
| 211 |
+
"reddit": ok(reddit_data, []),
|
| 212 |
+
"google_trending": ok(trending_searches, []),
|
| 213 |
+
"web_discussions": ok(web_data, []),
|
| 214 |
+
"x_discussions": ok(x_data, []),
|
| 215 |
+
"news": ok(news_data, []),
|
| 216 |
+
"niche_interest": ok(niche_interest, {}),
|
| 217 |
+
}
|