import os, requests, time
TP_KEY = os.environ["TRUSTPILOT_API_KEY"]
MV = os.environ["MAVERA_API_KEY"]
TP_BASE = "https://api.trustpilot.com/v1"
MV_H = {"Authorization": f"Bearer {MV}", "Content-Type": "application/json"}
DOMAINS = ["yourdomain.com", "competitor-a.com", "competitor-b.com", "competitor-c.com"]
# 1. Pull TrustScores for all domains
scores = []
for domain in DOMAINS:
r = requests.get(f"{TP_BASE}/business-units/find",
params={"apikey": TP_KEY, "name": domain})
if r.status_code != 200:
print(f"Could not find {domain}")
continue
bu = r.json()
score_data = bu.get("score", {})
scores.append({
"domain": domain,
"name": bu.get("displayName", domain),
"trust_score": score_data.get("trustScore", 0),
"stars": score_data.get("stars", 0),
"total_reviews": bu.get("numberOfReviews", {}).get("total", 0),
"star_distribution": bu.get("score", {}).get("distribution", {}),
})
time.sleep(0.2)
# 2. Pull sample reviews for each competitor
for s in scores:
if s["domain"] == DOMAINS[0]:
continue
bu_id = requests.get(f"{TP_BASE}/business-units/find",
params={"apikey": TP_KEY, "name": s["domain"]}).json().get("id", "")
if not bu_id:
continue
revs = requests.get(f"{TP_BASE}/business-units/{bu_id}/reviews",
params={"apikey": TP_KEY, "perPage": 10, "orderBy": "createdat.desc"}).json()
s["recent_reviews"] = [
f"[{r.get('stars',0)}★] {r.get('text','')[:150]}"
for r in revs.get("reviews", [])[:5]
]
time.sleep(0.2)
# 3. Mave competitive analysis
score_block = "\n".join(
f"- {s['name']} ({s['domain']}): TrustScore={s['trust_score']}, Stars={s['stars']}, Reviews={s['total_reviews']}"
for s in scores
)
review_block = "\n\n".join(
f"## {s['name']}\n" + "\n".join(s.get("recent_reviews", []))
for s in scores if s.get("recent_reviews")
)
analysis = requests.post("https://app.mavera.io/api/v1/mave/chat",
headers=MV_H,
json={"message": f"""Compare TrustScores across these competitors:
SCORES:
{score_block}
SAMPLE COMPETITOR REVIEWS:
{review_block}
Produce:
1. Where do we win on trust? (higher score, better sentiment)
2. Where do competitors beat us? (and why)
3. Messaging opportunities based on trust differential
4. Review volume strategy (if we have fewer reviews)
5. Specific claims we can make (e.g., "Highest TrustScore in [category]")"""}).json()
print("=== Competitive TrustScore Analysis ===")
print(analysis.get("content", "")[:1500])