make library instantiated

This commit is contained in:
yuki 2025-10-08 03:24:13 -03:00
parent d751c5ab45
commit 26559dd270

View file

@ -1,5 +1,4 @@
import aiohttp import aiohttp
import asyncio
from pathlib import Path from pathlib import Path
from typing import Union from typing import Union
@ -14,24 +13,25 @@ class ZeroXZero:
- Check service availability - Check service availability
""" """
ENDPOINT_URL = "https://0x0.st"
HEADERS = { HEADERS = {
"User-Agent": "pyzxz-uploader/1.0 (https://github.com/yourrepo)" "User-Agent": "pyzxz-uploader/1.0 (https://github.com/yourrepo)"
} }
@staticmethod def __init__(self, endpoint_url: str = "https://0x0.st"):
async def upload(file_path: Union[str, Path]) -> str: self.endpoint_url = endpoint_url
async def upload(self, file_path: Union[str, Path]) -> str:
file_path = Path(file_path) file_path = Path(file_path)
if not file_path.exists(): if not file_path.exists():
raise FileNotFoundError(f"No such file: {file_path}") raise FileNotFoundError(f"No such file: {file_path}")
async with aiohttp.ClientSession(headers=ZeroXZero.HEADERS) as session: async with aiohttp.ClientSession(headers=self.HEADERS) as session:
with file_path.open("rb") as f: with file_path.open("rb") as f:
data = aiohttp.FormData() data = aiohttp.FormData()
data.add_field("file", f, filename=file_path.name) data.add_field("file", f, filename=file_path.name)
async with session.post( async with session.post(
ZeroXZero.ENDPOINT_URL, self.endpoint_url,
data=data data=data
) as response: ) as response:
text = await response.text() text = await response.text()
@ -39,13 +39,12 @@ class ZeroXZero:
return text.strip() return text.strip()
raise ValueError(f"Upload failed: {text.strip()}") raise ValueError(f"Upload failed: {text.strip()}")
@staticmethod async def upload_from_bytes(self, data: bytes, filename: str) -> str:
async def upload_from_bytes(data: bytes, filename: str) -> str: async with aiohttp.ClientSession(headers=self.HEADERS) as session:
async with aiohttp.ClientSession(headers=ZeroXZero.HEADERS) as session:
form = aiohttp.FormData() form = aiohttp.FormData()
form.add_field("file", data, filename=filename) form.add_field("file", data, filename=filename)
async with session.post( async with session.post(
ZeroXZero.ENDPOINT_URL, self.endpoint_url,
data=form data=form
) as response: ) as response:
text = await response.text() text = await response.text()
@ -53,16 +52,14 @@ class ZeroXZero:
return text.strip() return text.strip()
raise ValueError(f"Upload failed: {text.strip()}") raise ValueError(f"Upload failed: {text.strip()}")
@staticmethod async def upload_text(self, text: str, filename: str = "text.txt") -> str:
async def upload_text(text: str, filename: str = "text.txt") -> str: return await self.upload_from_bytes(text.encode("utf-8"), filename)
return await ZeroXZero.upload_from_bytes(text.encode("utf-8"), filename)
@staticmethod async def is_available(self) -> bool:
async def is_available() -> bool:
try: try:
async with aiohttp.ClientSession(headers=ZeroXZero.HEADERS) as session: async with aiohttp.ClientSession(headers=self.HEADERS) as session:
async with session.head( async with session.head(
ZeroXZero.ENDPOINT_URL, self.endpoint_url,
timeout=aiohttp.ClientTimeout(total=3) timeout=aiohttp.ClientTimeout(total=3)
) as response: ) as response:
return response.status == 200 return response.status == 200