42 lines
1.4 KiB
Python
42 lines
1.4 KiB
Python
from fastapi import APIRouter, Request, HTTPException
|
|
from fastapi.responses import RedirectResponse
|
|
import uuid
|
|
|
|
router = APIRouter()
|
|
|
|
# 模擬的 affiliate_clicks 記錄,實務上應寫入 TimescaleDB / PostgreSQL
|
|
# affiliate_clicks_db = []
|
|
|
|
@router.get("/api/v1/go/{bookmaker_id}")
|
|
async def affiliate_redirect(bookmaker_id: str, request: Request):
|
|
"""
|
|
動態聯盟行銷與防廣告攔截引擎
|
|
- Server-side redirect
|
|
- 紀錄點擊以計算 CR
|
|
"""
|
|
|
|
# 模擬博彩公司對應表與追蹤碼
|
|
bookmakers = {
|
|
"bet365": "https://www.bet365.com/?affiliate=QUANT2026",
|
|
"pinnacle": "https://www.pinnacle.com/?ref=QUANT2026",
|
|
"draftkings": "https://www.draftkings.com/?track=QUANT2026"
|
|
}
|
|
|
|
if bookmaker_id not in bookmakers:
|
|
raise HTTPException(status_code=404, detail="Bookmaker not found")
|
|
|
|
target_url = bookmakers[bookmaker_id]
|
|
|
|
# 記錄點擊資料 (User-Agent, IP, Timestamp, etc)
|
|
click_data = {
|
|
"click_id": str(uuid.uuid4()),
|
|
"bookmaker_id": bookmaker_id,
|
|
"user_agent": request.headers.get("user-agent", "unknown"),
|
|
"client_ip": request.client.host if request.client else "unknown"
|
|
}
|
|
|
|
# affiliate_clicks_db.append(click_data)
|
|
# print(f"Logged affiliate click: {click_data}")
|
|
|
|
return RedirectResponse(url=target_url, status_code=302)
|