Hana Celeste commited on
Update app/gscode_logic.py
Browse files- app/gscode_logic.py +36 -18
app/gscode_logic.py
CHANGED
|
@@ -24,31 +24,49 @@ class GenshinCodeLogic:
|
|
| 24 |
self.session = None
|
| 25 |
|
| 26 |
async def fetch_codes(self):
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
"User-Agent": "Mozilla/5.0"
|
| 31 |
-
},
|
| 32 |
-
timeout=aiohttp.ClientTimeout(total=20)
|
| 33 |
-
) as resp:
|
| 34 |
if resp.status != 200:
|
| 35 |
return {"count": 0, "codes": []}
|
| 36 |
-
|
| 37 |
data = await resp.json()
|
| 38 |
|
| 39 |
html = data.get("parse", {}).get("text", {}).get("*")
|
| 40 |
-
if not html:
|
| 41 |
-
return {"count": 0, "codes": []}
|
| 42 |
|
| 43 |
soup = BeautifulSoup(html, "html.parser")
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
return {
|
| 52 |
-
"count": len(
|
| 53 |
-
"codes":
|
| 54 |
-
}
|
|
|
|
| 24 |
self.session = None
|
| 25 |
|
| 26 |
async def fetch_codes(self):
|
| 27 |
+
if not self.session: await self.start()
|
| 28 |
+
|
| 29 |
+
async with self.session.get(GENSHIN_API, timeout=aiohttp.ClientTimeout(total=20)) as resp:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
if resp.status != 200:
|
| 31 |
return {"count": 0, "codes": []}
|
|
|
|
| 32 |
data = await resp.json()
|
| 33 |
|
| 34 |
html = data.get("parse", {}).get("text", {}).get("*")
|
| 35 |
+
if not html: return {"count": 0, "codes": []}
|
|
|
|
| 36 |
|
| 37 |
soup = BeautifulSoup(html, "html.parser")
|
| 38 |
+
results = []
|
| 39 |
|
| 40 |
+
rows = soup.select("table.article-table tr")
|
| 41 |
+
|
| 42 |
+
for row in rows:
|
| 43 |
+
cols = row.find_all("td")
|
| 44 |
+
if len(cols) < 3: continue # Bỏ qua hàng tiêu đề hoặc hàng thiếu dữ liệu
|
| 45 |
+
|
| 46 |
+
code_tag = cols[0].find("code")
|
| 47 |
+
if not code_tag: continue
|
| 48 |
+
code_text = code_tag.get_text(strip=True)
|
| 49 |
+
|
| 50 |
+
server = cols[1].get_text(strip=True)
|
| 51 |
+
|
| 52 |
+
rewards = []
|
| 53 |
+
reward_items = cols[2].select(".item-text")
|
| 54 |
+
for item in reward_items:
|
| 55 |
+
rewards.append(item.get_text(strip=True))
|
| 56 |
+
|
| 57 |
+
if not rewards:
|
| 58 |
+
rewards = [cols[2].get_text(strip=True).replace("\n", ", ")]
|
| 59 |
+
|
| 60 |
+
status_text = cols[3].get_text(" ", strip=True) if len(cols) > 3 else "Unknown"
|
| 61 |
+
|
| 62 |
+
results.append({
|
| 63 |
+
"code": code_text,
|
| 64 |
+
"server": server,
|
| 65 |
+
"rewards": rewards,
|
| 66 |
+
"status": status_text
|
| 67 |
+
})
|
| 68 |
|
| 69 |
return {
|
| 70 |
+
"count": len(results),
|
| 71 |
+
"codes": results
|
| 72 |
+
}
|