from argparse import ArgumentParser, Namespace
import json
import subprocess
from pathlib import Path
from sys import stderr
def get_args() -> Namespace:
argParser = ArgumentParser()
argParser.add_argument("-n", "--name", action="store_true",
help="Show comma delimited filenames with the url in the output.")
argParser.add_argument(
"-s", "--single_line", action="store_true", help="Print output on a single line.")
argParser.add_argument("files", help="File to upload.",
nargs="+", action="extend")
pargs = argParser.parse_args()
pargs.files = list(map(lambda f: Path(f).resolve(), pargs.files))
return pargs
def format_response_links(name: str, path: str, with_name: bool) -> str:
if with_name:
return f"{name}, {path}"
else:
return f"{path}"
def uguu_upload(ps: list[Path], pargs: Namespace) -> None:
print("Uploading...", file=stderr)
errors = []
for p in ps:
try:
curl_complete_process = subprocess.run(
["curl", "-F",
f"files[]=@{str(p)}", "uguu.se/upload.php"],
capture_output=True)
curl_complete_process.check_returncode()
file_responses: list[dict[str, str]] = json.loads(
curl_complete_process.stdout)["files"]
except Exception as e:
errors.append(str(e))
continue
[print(format_response_links(f["name"], f["url"], pargs.name),
sep=" " if pargs.single_line else "\n",
end=" " if pargs.single_line else "\n")
for f in file_responses]
print("Done.", file=stderr)
print()
if len(errors) > 0:
[print(e, file=stderr) for e in errors]
pargs = get_args()
uguu_upload(pargs.files, pargs)
it uploads to uguu, had to take some stuff out to fit the size requirements but this should be functional.