File size: 2,180 Bytes
ae96995
027fe8d
ae96995
7b6e408
ae96995
027fe8d
 
 
 
 
 
 
ae96995
027fe8d
 
 
 
 
 
7b6e408
81b0084
7b6e408
027fe8d
 
 
 
 
 
7b6e408
81b0084
 
 
 
027fe8d
81b0084
7b6e408
027fe8d
ae96995
 
7b6e408
81b0084
ae96995
 
 
81b0084
 
7b6e408
 
 
 
 
81b0084
 
7b6e408
e0f9d33
81b0084
e0f9d33
7b6e408
e0f9d33
 
7b6e408
 
 
 
e0f9d33
81b0084
 
 
 
e0f9d33
81b0084
 
e0f9d33
 
7b6e408
e0f9d33
ae96995
 
81b0084
 
7b6e408
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
# gscode_logic.py
import aiohttp
from bs4 import BeautifulSoup
from typing import List, Dict

GENSHIN_API = (
    "https://genshin-impact.fandom.com/api.php"
    "?action=parse"
    "&page=Promotional_Code"
    "&prop=text"
    "&format=json"
)

class GenshinCodeLogic:
    def __init__(self):
        self.session: aiohttp.ClientSession | None = None

    async def start(self):
        if not self.session:
            self.session = aiohttp.ClientSession(
                headers={"User-Agent": "Mozilla/5.0 (GenshinCodeBot)"}
            )

    async def stop(self):
        if self.session:
            await self.session.close()
            self.session = None

    async def fetch_codes(self) -> Dict:
        async with self.session.get(
            GENSHIN_API,
            timeout=aiohttp.ClientTimeout(total=20)
        ) as resp:
            if resp.status != 200:
                return {"count": 0, "cards": []}

            data = await resp.json()

        html = data.get("parse", {}).get("text", {}).get("*")
        if not html:
            return {"count": 0, "cards": []}

        soup = BeautifulSoup(html, "html.parser")

        cards: List[Dict] = []

        for row in soup.select("table.wikitable tbody tr"):
            tds = row.find_all("td")
            if len(tds) < 3:
                continue

            code_els = tds[0].find_all("code")
            if not code_els:
                continue

            codes = [c.get_text(strip=True) for c in code_els]

            server = tds[1].get_text(" ", strip=True)

            rewards = []
            for item in tds[2].select(".item-text"):
                text = item.get_text(" ", strip=True)
                if text:
                    rewards.append(text)

            validity = (
                tds[3].get_text(" ", strip=True)
                if len(tds) >= 4 else None
            )

            cards.append({
                "codes": codes,
                "server": server,
                "rewards": rewards,
                "validity": validity
            })

        return {
            "count": sum(len(card["codes"]) for card in cards),
            "cards": cards
        }