- Remove config.py and integrate environment variable handling directly in docker-compose.yml. - Delete detailed_handlers.py and replace with simplified command handlers. - Update requirements.txt to use aiogram and httpx instead of python-telegram-bot and requests. - Remove test_bot.py and utils.py, consolidating functionality into new VNDBClient class. - Implement VNDBClient for API interactions, simplifying query methods for visual novels, characters, and releases. - Clean up docker-compose.yml for improved readability and maintainability.
50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
import httpx
|
|
|
|
|
|
class VNDBClient:
|
|
def __init__(self, base_url):
|
|
self.base = base_url.rstrip("/")
|
|
self.client = httpx.AsyncClient(timeout=10)
|
|
|
|
async def _post(self, endpoint, payload):
|
|
try:
|
|
r = await self.client.post(f"{self.base}/{endpoint}", json=payload)
|
|
|
|
if r.status_code != 200:
|
|
return None
|
|
|
|
data = r.json()
|
|
return data.get("results", [])
|
|
except Exception:
|
|
return None
|
|
|
|
async def search_vn(self, query):
|
|
return await self._post("vn", {
|
|
"filters": ["search", "=", query],
|
|
"fields": "id,title,original,released,rating,votecount",
|
|
"results": 5
|
|
})
|
|
|
|
async def get_vn(self, vid):
|
|
res = await self._post("vn", {
|
|
"filters": ["id", "=", vid],
|
|
"fields": "id,title,original,released,rating,votecount",
|
|
"results": 1
|
|
})
|
|
return res[0] if res else None
|
|
|
|
async def get_char(self, cid):
|
|
res = await self._post("character", {
|
|
"filters": ["id", "=", cid],
|
|
"fields": "id,name,original",
|
|
"results": 1
|
|
})
|
|
return res[0] if res else None
|
|
|
|
async def get_release(self, rid):
|
|
res = await self._post("release", {
|
|
"filters": ["id", "=", rid],
|
|
"fields": "id,title,released",
|
|
"results": 1
|
|
})
|
|
return res[0] if res else None |