make library asynchronous

This commit is contained in:
yuki 2025-10-08 03:23:33 -03:00
parent 5c0cec74d0
commit d751c5ab45

View file

@ -1,11 +1,11 @@
import requests import aiohttp
import asyncio
from pathlib import Path from pathlib import Path
from typing import Union from typing import Union
class ZeroXZero: class ZeroXZero:
""" """
A static utility class for interacting with the 0x0.st file hosting service. An async utility class for interacting with the 0x0.st file hosting service.
Features: Features:
- Upload files from disk - Upload files from disk
@ -20,48 +20,51 @@ class ZeroXZero:
} }
@staticmethod @staticmethod
def upload(file_path: Union[str, Path]) -> str: async def upload(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}")
with file_path.open("rb") as f: async with aiohttp.ClientSession(headers=ZeroXZero.HEADERS) as session:
response = requests.post( with file_path.open("rb") as f:
data = aiohttp.FormData()
data.add_field("file", f, filename=file_path.name)
async with session.post(
ZeroXZero.ENDPOINT_URL,
data=data
) as response:
text = await response.text()
if response.status == 200 and text.startswith("https://"):
return text.strip()
raise ValueError(f"Upload failed: {text.strip()}")
@staticmethod
async def upload_from_bytes(data: bytes, filename: str) -> str:
async with aiohttp.ClientSession(headers=ZeroXZero.HEADERS) as session:
form = aiohttp.FormData()
form.add_field("file", data, filename=filename)
async with session.post(
ZeroXZero.ENDPOINT_URL, ZeroXZero.ENDPOINT_URL,
files={"file": f}, data=form
headers=ZeroXZero.HEADERS ) as response:
) text = await response.text()
if response.status == 200 and text.startswith("https://"):
if response.ok and response.text.startswith("https://"): return text.strip()
return response.text.strip() raise ValueError(f"Upload failed: {text.strip()}")
raise ValueError(f"Upload failed: {response.text.strip()}")
@staticmethod @staticmethod
def upload_from_bytes(data: bytes, filename: str) -> str: async def upload_text(text: str, filename: str = "text.txt") -> str:
files = {"file": (filename, data)} return await ZeroXZero.upload_from_bytes(text.encode("utf-8"), filename)
response = requests.post(
ZeroXZero.ENDPOINT_URL,
files=files,
headers=ZeroXZero.HEADERS
)
if response.ok and response.text.startswith("https://"):
return response.text.strip()
raise ValueError(f"Upload failed: {response.text.strip()}")
@staticmethod @staticmethod
def upload_text(text: str, filename: str = "text.txt") -> str: async def is_available() -> bool:
return ZeroXZero.upload_from_bytes(text.encode("utf-8"), filename)
@staticmethod
def is_available() -> bool:
try: try:
response = requests.head( async with aiohttp.ClientSession(headers=ZeroXZero.HEADERS) as session:
ZeroXZero.ENDPOINT_URL, async with session.head(
timeout=3, ZeroXZero.ENDPOINT_URL,
headers=ZeroXZero.HEADERS timeout=aiohttp.ClientTimeout(total=3)
) ) as response:
return response.status_code == 200 return response.status == 200
except requests.RequestException: except Exception:
return False return False