From ed486048272cc0d05dba464c976f23549e2fe6cc Mon Sep 17 00:00:00 2001 From: gigirassy Date: Tue, 28 Jul 2026 04:17:48 +0200 Subject: [PATCH] Add patchycli.py --- patchycli.py | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 patchycli.py diff --git a/patchycli.py b/patchycli.py new file mode 100644 index 0000000..22f904f --- /dev/null +++ b/patchycli.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python3 + +import argparse +import json +from datetime import datetime, timezone +from pathlib import Path + +import requests + +UPLOAD_URL = "https://patchy.moe/-/upload" + +LOG_DIR = Path.home() / "patchycli" +LOG_FILE = LOG_DIR / "uploads.txt" + + +def upload(path: Path): + last_error = None + + for field in ("file", "data"): + with path.open("rb") as f: + files = { + field: (path.name, f) + } + + try: + r = requests.post(UPLOAD_URL, files=files, timeout=60) + except requests.RequestException as e: + last_error = str(e) + continue + + if r.ok: + try: + data = r.json() + except json.JSONDecodeError: + last_error = f"Server returned non-JSON response:\n{r.text}" + continue + + if "link" in data: + return data + + last_error = data.get("error", "Unknown server error") + else: + last_error = f"HTTP {r.status_code}: {r.text}" + + raise RuntimeError(last_error) + + +def log_upload(filename, link, delete_link): + LOG_DIR.mkdir(parents=True, exist_ok=True) + + timestamp = datetime.now(timezone.utc).isoformat() + + with LOG_FILE.open("a", encoding="utf-8") as f: + f.write(f"Date: {timestamp}\n") + f.write(f"File: {filename}\n") + f.write(f"URL: {link}\n") + f.write(f"Delete: {delete_link}\n") + f.write("-" * 60 + "\n") + + +def main(): + parser = argparse.ArgumentParser(description="Upload files to patchy.moe") + parser.add_argument("file", help="File to upload") + args = parser.parse_args() + + path = Path(args.file) + + if not path.is_file(): + raise SystemExit(f"No such file: {path}") + + result = upload(path) + + link = result["link"] + delete_link = result.get("deleteLink", "") + + log_upload(path.name, link, delete_link) + + print(link) + + if delete_link: + print(f"Delete: {delete_link}") + + +if __name__ == "__main__": + main() \ No newline at end of file