import os, requests, time
X = os.environ["X_BEARER_TOKEN"]; MV = os.environ["MAVERA_API_KEY"]
X_BASE = "https://api.x.com/2"; MV_BASE = "https://app.mavera.io/api/v1"
X_H = {"Authorization": f"Bearer {X}"}
MV_H = {"Authorization": f"Bearer {MV}", "Content-Type": "application/json"}
WOEID = 23424977 # United States
BRAND = "B2B marketing analytics. Audience: marketing leaders. Tone: insightful, data-driven, slightly witty. Own: attribution, analytics, MarTech. Avoid: politics, crypto."
# 1. Fetch trends
r = requests.get(f"{X_BASE}/trends/by/woeid/{WOEID}", headers=X_H)
if r.status_code == 429:
time.sleep(int(r.headers.get("x-rate-limit-reset", time.time()+60)) - int(time.time()))
r = requests.get(f"{X_BASE}/trends/by/woeid/{WOEID}", headers=X_H)
if r.status_code == 403:
print("Trends requires elevated access. Using fallback.")
trends = [{"name": "marketing analytics", "tweet_volume": 0}]
else:
r.raise_for_status()
td = r.json()
trends = [{"name": t.get("name",""), "tweet_volume": t.get("tweet_volume",0)}
for t in td.get("data", td.get("trends",[]))[:30]]
# 2. Filter via Mave
trend_list = "\n".join(f"- {t['name']} (vol: {t['tweet_volume'] or 'N/A'})" for t in trends[:25])
relevance = requests.post(f"{MV_BASE}/mave/chat", headers=MV_H, json={
"message": f"Content strategist: review X trends for brand relevance.\n\nTRENDS:\n{trend_list}\n\nBRAND: {BRAND}\n\nScore each 1-10. Return only 7+ with angle, format, urgency."
}).json()
# 3. Content sprint
sprint = requests.post(f"{MV_BASE}/generations", headers=MV_H, json={
"prompt": f"Content sprint from trend analysis.\n\n{relevance.get('content','')[:2000]}\n\n"
"For each relevant trend:\n1. Tweet thread (5-7 tweets, <280 chars, hook first)\n"
"2. LinkedIn post (150-200 words)\n3. Blog outline (title + 5 sections)\n\nTie to marketing analytics."
}).json()
print(sprint.get("output", sprint.get("content",""))[:2000])