mirror of
https://codeberg.org/gigirassy/patchycli
synced 2026-08-30 15:27:41 +00:00
Add patchycli.py
This commit is contained in:
@@ -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()
|
||||
Reference in New Issue
Block a user