Hana Celeste commited on
Rename app/gscode_logic.py to app/Gscode_logic.py
Browse filesFiles changed (2)
- app/Gscode_logic.pyAdded+40
- app/gscode_logic.pyDeleted-40
12345678910111213141516171819202122232425262728293031323334353637383940# gscode_logic.pyimport jsonfrom bs4 import BeautifulSoupfrom fastapi import HTTPException
class GenshinCodeLogic: async def fetch_codes(self, fetcher, url: str): # 1. fetch raw res = await fetcher.fetch(url)
if not res or "text" not in res: raise HTTPException(500, "Fetch failed")
try: data = json.loads(res["text"]) except Exception: raise HTTPException(400, "Invalid JSON response")
# 2. lấy HTML string html = data.get("parse", {}).get("text", {}).get("*") if not html: return {"count": 0, "codes": []}
# 3. parse HTML soup = BeautifulSoup(html, "html.parser")
# 4. lấy <code> codes = [] for c in soup.find_all("code"): val = c.get_text(strip=True) if val and len(val) >= 6: codes.append(val)
# 5. unique + sort codes = sorted(set(codes))
return { "count": len(codes), "codes": codes }12345678910111213141516171819202122232425262728293031323334353637383940# gscode_logic.pyimport httpximport refrom html import unescape
class GscodeApp: def __init__(self): self.client: httpx.AsyncClient | None = None
async def start(self): self.client = httpx.AsyncClient( timeout=15, headers={ "User-Agent": "HF-Gscode-API" } )
async def stop(self): if self.client: await self.client.aclose()
async def fetch_codes(self, url: str): if not self.client: raise RuntimeError("GscodeApp not started")
resp = await self.client.get(url) resp.raise_for_status()
text = resp.text
text = unescape(text)
codes = re.findall(r"<code>(.*?)</code>", text, re.I)
codes = list(dict.fromkeys(c.strip() for c in codes if c.strip()))
return { "count": len(codes), "codes": codes }app/Gscode_logic.pyAdded+40
app/gscode_logic.pyDeleted-40