💾 Archived View for hackersphere.space › ~willowf › memes › meme.py captured on 2023-04-19 at 22:35:19.
⬅️ Previous capture (2023-03-20)
-=-=-=-=-=-=-
#!/usr/bin/env python3 import os import sys import typing from datetime import date # authored by willowf def template_var(identifier: str) -> str: if any(c.islower() or not c.isalpha() for c in identifier): sys.stderr.write(f"{sys.argv[0]}: `{identifier}`: not a valid template variable\n") sys.exit(1) return f"@@{identifier}@@" today = date.today().strftime("%Y-%m-%d") pwd = template_var("PWD") def convert_to_png(source_file: str, dest_file: str) -> int: return os.system(f"convert {source_file} {dest_file}") def append_file_to_index(filename_to_add: str) -> None: with open("index.gmi", "a") as index_file: index_file.write(f"=> {pwd}/{filename_to_add} {today} {filename_to_add}\n") def download_file(uri: str, dest_file: str) -> int: return os.system(f"wget {uri} -O {dest_file}") def try_to(callback: typing.Callable[[], int], pname: str): # `px` is an abbreviation for "process exit code" here px = callback() if px != 0: print(f"Subprocess of function {pname} exited with erroneous code {px}. Aborting.") sys.exit(1) if __name__ == "__main__": if "-h" in sys.argv or "--help" in sys.argv: print(f"{sys.argv[0]} <URL> <filename to save as> [-convert]") sys.exit(0) elif "-convert" in sys.argv: if sys.argv[2][-4:] != ".png": try_to(lambda: download_file(sys.argv[1], sys.argv[2]), "download_file") new_file = f"{sys.argv[2][:-4]}.png" try_to(lambda: convert_to_png(sys.argv[2], new_file), "convert_to_png") append_file_to_index(new_file) print(f"Converted {sys.argv[2]} to {new_file}") sys.exit(0) else: print(f"Expected filename to end in .png, but got {sys.argv[2]}") sys.exit(1) else: download_file(sys.argv[1], sys.argv[2]) append_file_to_index(sys.argv[2])