Hana Celeste commited on
Commit
c457b4b
·
verified ·
1 Parent(s): b5d3bc8

Create gscode_logic.py

Browse files
Files changed (1) hide show
  1. app/gscode_logic.py +40 -0
app/gscode_logic.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # gscode_logic.py
2
+ import httpx
3
+ import re
4
+ from html import unescape
5
+
6
+ class GscodeApp:
7
+ def __init__(self):
8
+ self.client: httpx.AsyncClient | None = None
9
+
10
+ async def start(self):
11
+ self.client = httpx.AsyncClient(
12
+ timeout=15,
13
+ headers={
14
+ "User-Agent": "HF-Gscode-API"
15
+ }
16
+ )
17
+
18
+ async def stop(self):
19
+ if self.client:
20
+ await self.client.aclose()
21
+
22
+ async def fetch_codes(self, url: str):
23
+ if not self.client:
24
+ raise RuntimeError("GscodeApp not started")
25
+
26
+ resp = await self.client.get(url)
27
+ resp.raise_for_status()
28
+
29
+ text = resp.text
30
+
31
+ text = unescape(text)
32
+
33
+ codes = re.findall(r"<code>(.*?)</code>", text, re.I)
34
+
35
+ codes = list(dict.fromkeys(c.strip() for c in codes if c.strip()))
36
+
37
+ return {
38
+ "count": len(codes),
39
+ "codes": codes
40
+ }