#!/usr/bin/python3 """ A lil script to put files on https://cdn.tahnok.ca for use in blog posts or sharing or whatever Accepts either a path or a URL. If the filename is longer than 40 characters, make a random UUID based on the filename Depends on wget and scp """ import argparse import re import subprocess from pathlib import Path from uuid import uuid5, UUID namespace = UUID("231c540e-866e-48d6-a3a4-38cf20a87893") def main(url_or_file, title): if url_or_file.startswith("http"): result = subprocess.run(["wget", url_or_file, "-P", "/tmp/cdn"], capture_output=True, check=True, text=True) url_or_file = re.search(r"Saving to: ‘(.*)’", result.stderr)[1] # should I clean up? path = Path(url_or_file) if not path.exists(): raise if title: output_name = title elif len(path.stem) > 40: output_name = f"{uuid5(namespace, url_or_file)}{path.suffix}" else: output_name = path.name subprocess.run(["scp", url_or_file, f"debian@oolong.tahnok.ca:/data/cdn/u/{output_name}"], check=True) print(f"https://cdn.tahnok.ca/u/{output_name}") if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("url_or_file") parser.add_argument("-t", "--title", default=None) args = parser.parse_args() main(args.url_or_file, args.title)