XciD's picture
XciD HF Staff
feat: include review id in response
ec60df8
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import httpx
import json
import asyncio
from datetime import datetime, timedelta
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["https://collectionsdarchitectes.fr"],
allow_methods=["GET"],
allow_headers=["*"],
)
TRUSTPILOT_URL = "https://www.trustpilot.com/review/collectionsdarchitectes.fr"
USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
cache = {"data": None, "expires": None}
async def fetch_reviews():
now = datetime.utcnow()
if cache["data"] and cache["expires"] and now < cache["expires"]:
return cache["data"]
async with httpx.AsyncClient() as client:
r = await client.get(TRUSTPILOT_URL, headers={"User-Agent": USER_AGENT}, follow_redirects=True)
html = r.text
marker = '__NEXT_DATA__'
start = html.find(marker)
if start == -1:
return cache.get("data") or {"error": "Could not parse Trustpilot page"}
start = html.find('>', start) + 1
end = html.find('</script>', start)
raw = html[start:end]
data = json.loads(raw)
props = data.get("props", {}).get("pageProps", {})
biz = props.get("businessUnit", {})
reviews = props.get("reviews", [])
result = {
"score": biz.get("trustScore"),
"total": biz.get("numberOfReviews"),
"reviews": [
{
"id": r.get("id"),
"stars": r.get("rating"),
"title": r.get("title"),
"text": r.get("text"),
"author": r.get("consumer", {}).get("displayName"),
"date": r.get("dates", {}).get("publishedDate"),
}
for r in reviews
],
}
cache["data"] = result
cache["expires"] = now + timedelta(hours=24)
return result
@app.get("/reviews")
async def get_reviews():
return await fetch_reviews()