import os, requests, time
GOOG = os.environ["GOOGLE_ACCESS_TOKEN"]
ACCT = os.environ["GOOGLE_ACCOUNT_ID"]
MV = os.environ["MAVERA_API_KEY"]
GB_BASE = "https://mybusiness.googleapis.com/v4"
MV_H = {"Authorization": f"Bearer {MV}", "Content-Type": "application/json"}
GB_H = {"Authorization": f"Bearer {GOOG}"}
STAR_MAP = {"ONE": 1, "TWO": 2, "THREE": 3, "FOUR": 4, "FIVE": 5}
locations = requests.get(f"{GB_BASE}/{ACCT}/locations",
headers=GB_H, params={"pageSize": 50}).json().get("locations", [])
for loc in locations[:5]:
loc_id = loc.get("name", "")
loc_name = loc.get("locationName", "Unknown")
city = loc.get("address", {}).get("locality", "Unknown")
r = requests.get(f"{GB_BASE}/{loc_id}/reviews",
headers=GB_H, params={"pageSize": 50})
if r.status_code != 200:
continue
reviews = r.json().get("reviews", [])
positive = [rev.get("comment", "")[:250] for rev in reviews
if STAR_MAP.get(rev.get("starRating"), 3) >= 4 and rev.get("comment")]
negative = [rev.get("comment", "")[:250] for rev in reviews
if STAR_MAP.get(rev.get("starRating"), 3) <= 2 and rev.get("comment")]
# 1. Theme extraction
themes = requests.post("https://app.mavera.io/api/v1/mave/chat", headers=MV_H,
json={"message": f"""Extract themes from reviews for {loc_name} ({city}).
POSITIVE ({len(positive)} reviews):
{chr(10).join(positive[:10])}
NEGATIVE ({len(negative)} reviews):
{chr(10).join(negative[:10])}
Return: top 5 positive themes, top 3 negative themes, each with frequency and representative quote."""}).json()
print(f"\n{'='*50}")
print(f"THEMES: {loc_name} ({city})")
print(themes.get("content", "")[:500])
# 2. Generate location-specific content
gen = requests.post("https://app.mavera.io/api/v1/generations", headers=MV_H,
json={"prompt": (
f"Generate location-specific marketing content for {loc_name} in {city}.\n\n"
f"Themes from customer reviews:\n{themes.get('content','')[:500]}\n\n"
f"Create:\n"
f"1. Google Business Profile post (150 words, highlight top strength)\n"
f"2. Local Instagram caption (casual, with hashtags)\n"
f"3. Location-specific tagline (10 words max)\n"
f"4. Local SEO paragraph for website (100 words, include city name naturally)"
)}).json()
print(f"\nCONTENT:")
print(gen.get("output", gen.get("content", gen.get("text", "")))[:600])
time.sleep(1)