Files
2026-07-28 04:18:57 +02:00

94 lines
2.8 KiB
Python

#!/usr/bin/env python3
#ISC License:
#
#Copyright (c) 2004-2010 by Internet Systems Consortium, Inc. ("ISC")
#Copyright (c) 1995-2003 by Internet Software Consortium
#
#Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
#
#THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
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()