Create trend_monitor.py
Browse files- services/trend_monitor.py +34 -0
services/trend_monitor.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from typing import List
|
| 3 |
+
import random
|
| 4 |
+
import asyncio
|
| 5 |
+
|
| 6 |
+
# 実運用では official API を使う。ここでは疑似データ or トークンがあれば差し替え。
|
| 7 |
+
# X/Instagramの本API接続は social_adapters を参照。
|
| 8 |
+
|
| 9 |
+
async def fetch_trend_samples(platforms: List[str], keywords: List[str], limit: int = 20):
|
| 10 |
+
have_real_api = bool(os.getenv("X_BEARER_TOKEN") or os.getenv("IG_ACCESS_TOKEN"))
|
| 11 |
+
if not have_real_api:
|
| 12 |
+
# 疑似データ生成(MVPデモ用)
|
| 13 |
+
samples = []
|
| 14 |
+
for _ in range(limit):
|
| 15 |
+
plat = random.choice(platforms or ["x", "instagram"])
|
| 16 |
+
kw = random.choice(keywords or ["ブランド", "競合"])
|
| 17 |
+
samples.append({
|
| 18 |
+
"platform": plat,
|
| 19 |
+
"text": f"話題の投稿(疑似): {kw} がトレンド。短評とリンク例。",
|
| 20 |
+
"author": f"user_{random.randint(1000,9999)}",
|
| 21 |
+
"url": "https://example.com/post"
|
| 22 |
+
})
|
| 23 |
+
await asyncio.sleep(0.1)
|
| 24 |
+
return samples
|
| 25 |
+
|
| 26 |
+
# ↓ ここに本番アダプタ呼び出し(例)
|
| 27 |
+
from .social_adapters.x_adapter import search_recent_x
|
| 28 |
+
from .social_adapters.instagram_adapter import search_recent_instagram
|
| 29 |
+
results = []
|
| 30 |
+
if "x" in platforms:
|
| 31 |
+
results += await search_recent_x(keywords, limit=limit)
|
| 32 |
+
if "instagram" in platforms:
|
| 33 |
+
results += await search_recent_instagram(keywords, limit=limit)
|
| 34 |
+
return results[:limit]
|