import os, requests, time
VM = os.environ["VIMEO_ACCESS_TOKEN"]
MV = os.environ["MAVERA_API_KEY"]
VM_BASE = "https://api.vimeo.com"
MV_BASE = "https://app.mavera.io/api/v1"
VM_H = {"Authorization": f"Bearer {VM}", "Accept": "application/vnd.vimeo.*+json;version=3.4"}
MV_H = {"Authorization": f"Bearer {MV}", "Content-Type": "application/json"}
# 1. Pull videos with engagement stats
resp = requests.get(f"{VM_BASE}/me/videos", headers=VM_H, params={
"per_page": 50, "sort": "plays", "direction": "desc",
"fields": "uri,name,link,duration,stats,metadata.connections.likes.total,"
"metadata.connections.comments.total",
}).json()
videos = []
for v in resp.get("data", []):
vid_id = v["uri"].split("/")[-1]
stats = v.get("stats", {})
videos.append({
"id": vid_id, "name": v["name"], "link": v["link"],
"duration": v.get("duration", 0),
"plays": stats.get("plays", 0),
"finishes": stats.get("finishes", 0),
"avg_watched": round(stats.get("finishes", 0) / max(stats.get("plays", 1), 1) * 100, 1),
"likes": v.get("metadata", {}).get("connections", {}).get("likes", {}).get("total", 0),
"comments": v.get("metadata", {}).get("connections", {}).get("comments", {}).get("total", 0),
})
print(f"Pulled engagement data for {len(videos)} videos")
# 2. Run Mavera Video Analysis on top 15
combined = []
for video in videos[:15]:
upload = requests.post(f"{MV_BASE}/assets", headers=MV_H, json={
"url": video["link"], "name": video["name"][:80], "type": "video",
}).json()
analysis = requests.post(f"{MV_BASE}/video-analysis", headers=MV_H, json={
"asset_id": upload["id"],
"analysis_types": [
"message_clarity", "emotional_impact", "hook_score",
"behavioral_effectiveness", "pacing", "cognitive_load",
],
}).json()
for _ in range(30):
time.sleep(3)
status = requests.get(
f"{MV_BASE}/video-analysis/{analysis['id']}", headers=MV_H
).json()
if status.get("status") == "completed":
break
r = status.get("results", {})
combined.append({
**video,
"clarity": r.get("message_clarity", {}).get("score", 0),
"emotion": r.get("emotional_impact", {}).get("score", 0),
"hook": r.get("hook_score", {}).get("score", 0),
"behavior": r.get("behavioral_effectiveness", {}).get("score", 0),
"pacing_score": r.get("pacing", {}).get("score", 0),
"cog_load": r.get("cognitive_load", {}).get("average", 0),
})
time.sleep(1)
# 3. Build correlation dataset for Mave
data_block = "\n".join(
f"Video: \"{c['name'][:40]}\" | "
f"Plays: {c['plays']:,} | Finish%: {c['avg_watched']}% | Likes: {c['likes']} | Comments: {c['comments']} || "
f"Clarity: {c['clarity']} | Emotion: {c['emotion']} | Hook: {c['hook']} | "
f"Behavior: {c['behavior']} | Pacing: {c['pacing_score']} | CogLoad: {c['cog_load']}"
for c in combined
)
correlation = requests.post(f"{MV_BASE}/mave/chat", headers=MV_H, json={
"message": f"""Analyze the correlation between Mavera creative scores and real Vimeo engagement.
DATASET ({len(combined)} videos — engagement metrics || Mavera scores):
{data_block}
Produce:
1. **Best Predictor**: Which single Mavera metric most strongly predicts real engagement (plays, finish rate, likes)?
2. **Metric-by-Metric Correlation**: For each Mavera metric, how well does it predict each engagement metric? (strong/moderate/weak/none)
3. **Surprising Findings**: Any metrics that DON'T correlate with engagement despite seeming important?
4. **Composite Formula**: Suggest a weighted combination of Mavera metrics that best predicts finish rate
5. **Actionable Rule**: "If you optimize for [X Mavera metric], you'll see the biggest lift in [Y engagement metric]"
6. **Outliers**: Videos where Mavera scores and engagement diverge — what explains the gap?""",
}).json()
print("VIDEO ENGAGEMENT × MAVERA SCORING CORRELATION")
print("=" * 65)
print(f"{'Video':<35} {'Plays':>8} {'Finish%':>8} {'Emotion':>8} {'Hook':>6} {'Clarity':>8}")
print("-" * 65)
for c in combined:
print(f" {c['name'][:33]:<35} {c['plays']:>8,} {c['avg_watched']:>7.1f}% "
f"{c['emotion']:>7} {c['hook']:>5} {c['clarity']:>7}")
print("\n" + correlation.get("content", "")[:2000])