bep40 commited on
Commit
fef87d6
·
verified ·
1 Parent(s): 5e8f285

fix: add /api/match/{id}/detail endpoint for frontend compatibility - v6

Browse files
Files changed (1) hide show
  1. app_v2_entry.py +43 -0
app_v2_entry.py CHANGED
@@ -178,6 +178,49 @@ def proxy_bongda(event_id: int = Query(default=None), slug: str = Query(default=
178
 
179
  return JSONResponse({"event_id": event_id, "found": False})
180
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
  # === Rest of endpoints (existing) ===
182
  _STOP=set('và của các những một được trong với cho tại sau trước khi không người việt nam hôm nay mới nhất nóng tin tức cập nhật theo từ đến là có thì này đã để'.split())
183
 
 
178
 
179
  return JSONResponse({"event_id": event_id, "found": False})
180
 
181
+ @app.get('/api/match/{event_id}/detail')
182
+ def api_match_detail(event_id: int, url: str = Query(default=None)):
183
+ # Try to extract slug from url if provided
184
+ slug = None
185
+ if url:
186
+ m = re.match(r'.+/tran-dau/\d+/(?:centre|preview)/(.+)', url)
187
+ if m:
188
+ slug = m.group(1)
189
+
190
+ cache_key = f"{event_id}_{slug or ''}"
191
+ now = time.time()
192
+ cached = _match_cache.get(cache_key)
193
+ if cached and now - cached.get('_ts', 0) < 300:
194
+ return JSONResponse(cached)
195
+
196
+ try:
197
+ # If no slug, try to find it from homepage
198
+ if not slug:
199
+ try:
200
+ home_r = req.get("https://bongda.com.vn/", headers={"User-Agent": "Mozilla/5.0"}, timeout=10)
201
+ if home_r.status_code == 200:
202
+ home_soup = BeautifulSoup(home_r.text, 'html.parser')
203
+ for a in home_soup.select(f'a[href*="/tran-dau/{event_id}/"]'):
204
+ href = a.get('href', '')
205
+ m = re.match(r'/tran-dau/\d+/(?:centre|preview)/(.+)', href)
206
+ if m:
207
+ slug = m.group(1)
208
+ cache_key = f"{event_id}_{slug}"
209
+ break
210
+ except: pass
211
+
212
+ result = _get_match_detail(event_id, slug)
213
+ if result:
214
+ result['_ts'] = now
215
+ _match_cache[cache_key] = result
216
+ return JSONResponse(result)
217
+ except Exception as e:
218
+ err = {"event_id": event_id, "found": False, "error": str(e), "_ts": now}
219
+ _match_cache[cache_key] = err
220
+ return JSONResponse(err)
221
+
222
+ return JSONResponse({"event_id": event_id, "found": False})
223
+
224
  # === Rest of endpoints (existing) ===
225
  _STOP=set('và của các những một được trong với cho tại sau trước khi không người việt nam hôm nay mới nhất nóng tin tức cập nhật theo từ đến là có thì này đã để'.split())
226