Commit 54f5f67
Changed files (2)
src/config.py
@@ -245,6 +245,8 @@ class DB:
ALIST_PASSWORD = os.getenv("ALIST_PASSWORD", "guest")
ALIST_SERVER = os.getenv("ALIST_SERVER", "")
ALIST_BASR_PATH = os.getenv("ALIST_BASR_PATH", "")
+ PASTBIN_SERVER = os.getenv("PASTBIN_SERVER", "https://shz.al")
+ PASTBIN_MAX_BYTES = int(os.getenv("PASTBIN_MAX_BYTES", "10485760")) # 10 MB
class ASR:
src/database.py
@@ -364,7 +364,7 @@ def guess_mime(path: str | Path) -> str:
async def upload_uguu(path: str | Path) -> str:
- """Upload to https://uguu.se, return the url."""
+ """Upload to https://uguu.se, return the download url."""
path = Path(path).expanduser().resolve()
if not path.is_file():
return ""
@@ -378,10 +378,57 @@ async def upload_uguu(path: str | Path) -> str:
return url
+async def upload_pastbin(path: str | Path, ttl: int | str | None = None) -> tuple[str, str]:
+ """Upload to Pastbin Workders.
+
+ Returns:
+ download url, manage url
+
+ https://github.com/SharzyL/pastebin-worker
+ """
+ path = Path(path).expanduser().resolve()
+ if not path.is_file():
+ return "", ""
+ if path.stat().st_size > DB.PASTBIN_MAX_BYTES:
+ logger.warning(f"File size exceeds {DB.PASTBIN_MAX_BYTES} bytes, skipping: {path.name}")
+ return "", ""
+ logger.debug(f"Uploading {path.name} to: {DB.PASTBIN_SERVER}")
+
+ async with await anyio.open_file(path, "rb") as f:
+ content = await f.read()
+ payload = {"c": (path.name, io.BytesIO(content)), "p": "1"}
+ if ttl is not None:
+ payload |= {"e": str(ttl)}
+ res = await hx_req(DB.PASTBIN_SERVER, method="POST", files=payload, check_keys=["url", "manageUrl"])
+ url = res["url"]
+ logger.success(f"Uploaded {path.name} to {url}")
+ return url, res["manageUrl"]
+
+
+async def delete_pastbin(url: str):
+ """Delete file in Pastbin Workders.
+
+ https://github.com/SharzyL/pastebin-worker
+ """
+ if not url:
+ return
+ async with AsyncClient(http2=True, follow_redirects=True, transport=AsyncHTTPTransport(retries=3, http2=True)) as hx:
+ try:
+ resp = await hx.delete(url, timeout=30)
+ resp.raise_for_status()
+ except Exception as e:
+ logger.warning(f"DEL Pastbin failed for url={url}: {e}")
+ return
+ else:
+ logger.success(f"DEL Pastbin for url={url}")
+
+
if __name__ == "__main__":
import asyncio
- asyncio.run(upload_uguu("测试.mp3"))
+ url, manage_url = asyncio.run(upload_pastbin("测试.mp3", ttl=10))
+ asyncio.run(delete_pastbin(manage_url))
+ # asyncio.run(upload_uguu("测试.mp3"))
# asyncio.run(list_alist())
# asyncio.run(upload_alist("测试.mp3"))
# asyncio.run(download_alist("测试.mp3"))