File size: 10,858 Bytes
8030948 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | """Stateful, no-external-effects Fandango WebsiteBench clone."""
from __future__ import annotations
import html
from pathlib import Path
from typing import Any
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse, JSONResponse, Response
from starlette.staticfiles import StaticFiles
from backend import store
from websitebench.local_clone_auth import AuthError
SITE_ID = "fandango"
ROOT = Path(__file__).resolve().parent
STATIC = ROOT / "static"
INDEX = ROOT / "frontend" / "index.html"
app = FastAPI(title="Fandango offline clone", docs_url=None, redoc_url=None, openapi_url=None)
app.mount("/static", StaticFiles(directory=str(STATIC)), name="static")
CSP = (
"default-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; "
"script-src 'self'; connect-src 'self'; font-src 'self' data:; frame-ancestors 'none'; "
"base-uri 'self'; form-action 'self'"
)
@app.middleware("http")
async def headers(request: Request, call_next):
response = await call_next(request)
response.headers["Content-Security-Policy"] = CSP
response.headers["Referrer-Policy"] = "no-referrer"
response.headers["X-Content-Type-Options"] = "nosniff"
response.headers["X-Frame-Options"] = "DENY"
return response
def actor(request: Request) -> tuple[str, str, dict[str, Any]]:
actor_id = request.cookies.get("wb_fandango_actor") or store.new_actor()
token, session = store.ensure_auth_session(request.cookies.get("wb_fandango_auth"))
return actor_id, token, session
def attach(response: Response, request: Request, actor_id: str, token: str) -> Response:
secure = request.url.scheme == "https"
response.set_cookie("wb_fandango_actor", actor_id, httponly=True, samesite="lax", secure=secure, max_age=2592000)
response.set_cookie("wb_fandango_auth", token, httponly=True, samesite="lax", secure=secure, max_age=2592000)
return response
def payload(data: Any, request: Request, status: int = 200) -> JSONResponse:
actor_id, token, _ = actor(request)
return attach(JSONResponse(data, status_code=status), request, actor_id, token)
async def body(request: Request) -> dict[str, Any]:
try:
value = await request.json()
except Exception:
return {}
return value if isinstance(value, dict) else {}
def error(message: str, request: Request, status: int = 422) -> JSONResponse:
return payload({"error": message}, request, status)
@app.get("/healthz", include_in_schema=False)
async def health() -> dict[str, Any]:
return {"ok": True, "site_id": SITE_ID}
@app.get("/api/bootstrap")
async def bootstrap(request: Request) -> JSONResponse:
actor_id, token, session = actor(request)
return attach(JSONResponse(store.bootstrap(actor_id, session.get("account"))), request, actor_id, token)
@app.get('/api/navigation/state')
async def navigation_state(request: Request) -> JSONResponse:
actor_id, token, _ = actor(request)
return attach(JSONResponse(store.navigation_state(actor_id)), request, actor_id, token)
@app.post('/api/navigation/state')
async def update_navigation(request: Request) -> JSONResponse:
actor_id, token, _ = actor(request)
try:
result = store.update_navigation(actor_id, await body(request))
except ValueError as exc:
return error(str(exc), request)
return attach(JSONResponse(result), request, actor_id, token)
@app.get("/api/movies")
async def movies(request: Request, q: str = "", genre: str = "", sort: str = "rating", max_price: float | None = None,
service: str = "", status: str = "", theater: str = "") -> JSONResponse:
actor_id, token, _ = actor(request)
rows = store.search(q, genre, sort, max_price, service, status, theater)
return attach(
JSONResponse({"movies": rows, "query": q, "genres": store.genres(), "theaters": store.THEATERS}),
request, actor_id, token,
)
@app.get("/api/theaters")
async def theaters(request: Request, date: str = "") -> JSONResponse:
actor_id, token, _ = actor(request)
return attach(
JSONResponse({
"theaters": store.theater_directory(date),
"dates": store.showtime_dates(),
"date": date if store.valid_date(date) else store.upcoming_friday(),
}),
request, actor_id, token,
)
@app.get("/api/movies/{movie_id}")
async def movie(movie_id: str, request: Request, date: str = "") -> JSONResponse:
row = store.movie(movie_id, date)
if not row:
return error("Movie not found", request, 404)
return payload({"movie": row, "dates": store.showtime_dates(),
"date": date if store.valid_date(date) else store.upcoming_friday()}, request)
@app.post("/api/favorites/{movie_id}")
async def favorite(movie_id: str, request: Request) -> JSONResponse:
actor_id, token, _ = actor(request)
try:
result = store.toggle_favorite(actor_id, movie_id)
except KeyError:
return attach(JSONResponse({"error": "Movie not found"}, status_code=404), request, actor_id, token)
return attach(JSONResponse(result), request, actor_id, token)
@app.post("/api/selection/showtime")
async def showtime(request: Request) -> JSONResponse:
actor_id, token, _ = actor(request)
data = await body(request)
try:
result = store.select_showtime(actor_id, str(data.get("movie_id", "")), str(data.get("theater_id", "")), str(data.get("showtime_id", "")), str(data.get("date", "")))
except ValueError as exc:
return attach(JSONResponse({"error": str(exc)}, status_code=422), request, actor_id, token)
return attach(JSONResponse(result, status_code=201), request, actor_id, token)
@app.post("/api/selection/tickets")
async def tickets(request: Request) -> JSONResponse:
actor_id, token, _ = actor(request)
data = await body(request)
try:
result = store.set_tickets(actor_id, int(data.get("adults", 0)), int(data.get("children", 0)), int(data.get("seniors", 0)))
except (ValueError, TypeError) as exc:
return attach(JSONResponse({"error": str(exc)}, status_code=422), request, actor_id, token)
return attach(JSONResponse(result), request, actor_id, token)
@app.post("/api/selection/seats")
async def seats(request: Request) -> JSONResponse:
actor_id, token, _ = actor(request)
data = await body(request)
try:
result = store.set_seats(actor_id, [str(value) for value in data.get("seats", [])])
except (ValueError, TypeError) as exc:
return attach(JSONResponse({"error": str(exc)}, status_code=422), request, actor_id, token)
return attach(JSONResponse(result), request, actor_id, token)
@app.post("/api/checkout/review")
async def checkout_review(request: Request) -> JSONResponse:
actor_id, token, _ = actor(request)
data = await body(request)
try:
result = store.review(actor_id, str(data.get("email", "")), str(data.get("postal_code", "")))
except ValueError as exc:
return attach(JSONResponse({"error": str(exc)}, status_code=422), request, actor_id, token)
return attach(JSONResponse(result), request, actor_id, token)
@app.post("/api/checkout/confirm")
async def checkout_confirm(request: Request) -> JSONResponse:
actor_id, token, _ = actor(request)
try:
result = store.confirm(actor_id)
except ValueError as exc:
return attach(JSONResponse({"error": str(exc)}, status_code=422), request, actor_id, token)
return attach(JSONResponse(result, status_code=201), request, actor_id, token)
@app.post("/api/bookings/{booking_id}/{action}")
async def booking_action(booking_id: str, action: str, request: Request) -> JSONResponse:
actor_id, token, _ = actor(request)
data = await body(request)
try:
result = store.update_booking(actor_id, booking_id, action, data.get("value"))
except KeyError:
return attach(JSONResponse({"error": "Booking not found"}, status_code=404), request, actor_id, token)
except ValueError as exc:
return attach(JSONResponse({"error": str(exc)}, status_code=422), request, actor_id, token)
return attach(JSONResponse(result), request, actor_id, token)
@app.post("/api/auth/register")
async def register(request: Request) -> JSONResponse:
actor_id, token, _ = actor(request)
data = await body(request)
try:
store.register(str(data.get("display_name", "")), str(data.get("email", "")), str(data.get("password", "")))
signed = store.login(token, str(data.get("email", "")), str(data.get("password", "")))
except (AuthError, ValueError) as exc:
return attach(JSONResponse({"error": str(exc)}, status_code=422), request, actor_id, token)
return attach(
JSONResponse({"profile": signed["account"]}, status_code=201),
request,
actor_id,
signed["session_token"],
)
@app.post("/api/auth/login")
async def login(request: Request) -> JSONResponse:
actor_id, token, _ = actor(request)
data = await body(request)
try:
signed = store.login(token, str(data.get("email", "")), str(data.get("password", "")))
except (AuthError, ValueError) as exc:
return attach(JSONResponse({"error": str(exc)}, status_code=401), request, actor_id, token)
return attach(JSONResponse({"profile": signed["account"]}), request, actor_id, signed["session_token"])
@app.post("/api/auth/logout")
async def logout(request: Request) -> JSONResponse:
actor_id, token, _ = actor(request)
store.logout(token)
new_token, _ = store.ensure_auth_session(None)
return attach(JSONResponse({"signed_out": True}), request, actor_id, new_token)
@app.post("/api/auth/recovery-preview")
async def recovery(request: Request) -> JSONResponse:
data = await body(request)
email_value = str(data.get("email", "")).strip()
if "@" not in email_value:
return error("Enter a valid email address", request)
return payload({"sent": False, "message": "No email was sent. This is a local recovery preview."}, request)
def known(path: str) -> bool:
prefixes = ("/movies", "/theaters", "/tickets", "/checkout", "/confirmation", "/account",
"/help", "/search", "/favorites", "/policies", "/offers", "/fanstore",
"/streaming", "/movie-news", "/movie-photos", "/movie-trailer")
return path == "/" or path.startswith(prefixes)
@app.get("/{path:path}", response_class=HTMLResponse)
async def page(path: str, request: Request) -> HTMLResponse:
route = "/" + path
status = 200 if known(route) else 404
source = INDEX.read_text(encoding="utf-8")
source = source.replace("__ROUTE__", html.escape(route, quote=True))
response = HTMLResponse(source, status_code=status)
actor_id, token, _ = actor(request)
return attach(response, request, actor_id, token)
|