import os, requests, time
YT = os.environ["YOUTUBE_API_KEY"]
MV = os.environ["MAVERA_API_KEY"]
YT_BASE = "https://www.googleapis.com/youtube/v3"
MV_BASE = "https://app.mavera.io/api/v1"
MV_H = {"Authorization": f"Bearer {MV}", "Content-Type": "application/json"}
VIDEO_ID = "dQw4w9WgXcQ" # Replace with your video ID
# 1. Fetch up to 200 comments (1 quota unit per page, ~20 per page)
comments = []
page_token = None
while len(comments) < 200:
params = {
"key": YT, "videoId": VIDEO_ID,
"part": "snippet", "maxResults": 100,
"order": "relevance", "textFormat": "plainText",
}
if page_token:
params["pageToken"] = page_token
r = requests.get(f"{YT_BASE}/commentThreads", params=params)
if r.status_code == 403:
print("Comments disabled or API quota exceeded")
break
r.raise_for_status()
data = r.json()
for item in data.get("items", []):
snippet = item["snippet"]["topLevelComment"]["snippet"]
comments.append({
"text": snippet["textDisplay"][:500],
"likes": snippet.get("likeCount", 0),
"published": snippet.get("publishedAt", ""),
})
page_token = data.get("nextPageToken")
if not page_token:
break
print(f"Collected {len(comments)} comments")
# 2. Create a research analyst persona
persona = requests.post(f"{MV_BASE}/personas", headers=MV_H, json={
"name": "YouTube Audience Analyst",
"description": (
"Senior qualitative researcher specializing in digital audience analysis. "
"Expert at identifying audience segments from unstructured text, detecting "
"sentiment patterns, and extracting the language that resonates with each segment. "
"Thinks in terms of jobs-to-be-done and psychographic clusters, not just demographics."
),
}).json()
# 3. Build comment block sorted by engagement
comments.sort(key=lambda c: -c["likes"])
comment_block = "\n".join(
f"[{c['likes']} likes] {c['text'][:300]}"
for c in comments[:200]
)
# 4. Analyze via Mave Chat with persona context
analysis = requests.post(f"{MV_BASE}/mave/chat", headers=MV_H, json={
"persona_id": persona["id"],
"message": f"""Analyze these {len(comments)} YouTube comments as audience segments.
COMMENTS:
{comment_block}
Produce:
1. **Audience Segments** (3-5 clusters): Name each segment, estimate its share of comments, describe their motivation, and quote 2-3 representative comments verbatim.
2. **Sentiment Distribution**: % positive / neutral / negative with examples.
3. **Language Patterns**: Exact phrases, slang, and emotional triggers your marketing should adopt.
4. **Objections & Concerns**: Recurring pain points or skepticism themes.
5. **Content Opportunities**: Topics commenters ask about that you haven't covered.
6. **Persona Validation**: Do these segments match typical B2C buyer personas? Where do they diverge?""",
}).json()
print(f"\nAUDIENCE ANALYSIS — {len(comments)} comments from {VIDEO_ID}")
print("=" * 60)
print(analysis.get("content", "")[:2500])
commentsDisabled. Check the video’ssnippet.liveBroadcastContentand fallback to a different video.