From ce71de8abd30ce8fad10592c39058608a2db68cd Mon Sep 17 00:00:00 2001 From: Evan Pratten Date: Sun, 11 Sep 2022 22:58:03 -0400 Subject: [PATCH] catto! --- configs/scripts/catto | 58 +++++++++++++++++++++++++++++++++++++++++++ configs/zsh/.zshrc | 3 --- install.conf.yaml | 18 ++++++++------ 3 files changed, 69 insertions(+), 10 deletions(-) create mode 100755 configs/scripts/catto diff --git a/configs/scripts/catto b/configs/scripts/catto new file mode 100755 index 0000000..4cbff55 --- /dev/null +++ b/configs/scripts/catto @@ -0,0 +1,58 @@ +#! /usr/bin/env python3 +import sys +import requests +import json +import argparse +import os +import tempfile +from pathlib import Path +import netrc + +def main() -> int: + ap = argparse.ArgumentParser() + ap.add_argument("image", help="Image to upload") + ap.add_argument("--no-compress", help="Don't compress the image", action="store_true") + args = ap.parse_args() + + # Load the API key from the .netrc file + if "catto.pictures" not in netrc.netrc().hosts: + print("No catto.pictures entry in .netrc file") + return 1 + login, account, password = netrc.netrc().authenticators("catto.pictures") + + # Parse the image to a path + image_path = Path(args.image) + + # Ensure the file actually exists + if not image_path.exists(): + print("File not found", file=sys.stderr) + return 1 + + # Compres the image to a temporary file + with tempfile.NamedTemporaryFile(suffix=image_path.suffix) as temp: + # Compress + if not args.no_compress: + os.system(f"convert {image_path} -quality 25% {temp.name}") + image_path = Path(temp.name) + + # Strip all exif data + os.system(f"exiftool -EXIF= {image_path}") + + # Upload the image + with open(image_path, "rb") as f: + r = requests.post( + "https://upload.catto.pictures", + files={"image": (temp.name, f, f"image/{image_path.suffix[1:]}")}, + headers={ + "Authorization": password, + "Domain": "img.ewpratten.com", + } + ) + if r.status_code != 200: + print(f"Error uploading image:\n{r.text}", file=sys.stderr) + return 1 + print(json.loads(r.text)["url"]) + +if __name__ == "__main__": + sys.exit(main()) + diff --git a/configs/zsh/.zshrc b/configs/zsh/.zshrc index 65f0cdb..dc1bab6 100644 --- a/configs/zsh/.zshrc +++ b/configs/zsh/.zshrc @@ -15,6 +15,3 @@ fi # I always want my ~/bin to be in my PATH export PATH="$HOME/bin:$PATH" -#THIS MUST BE AT THE END OF THE FILE FOR SDKMAN TO WORK!!! -export SDKMAN_DIR="$HOME/.sdkman" -[[ -s "$HOME/.sdkman/bin/sdkman-init.sh" ]] && source "$HOME/.sdkman/bin/sdkman-init.sh" diff --git a/install.conf.yaml b/install.conf.yaml index e394b04..fc190ac 100644 --- a/install.conf.yaml +++ b/install.conf.yaml @@ -4,6 +4,12 @@ - clean: ['~'] +- create: + - ~/Downloads + - ~/bin + - ~/projects + - ~/src + - link: ~/.gitconfig: configs/git/.gitconfig ~/.zshrc: @@ -12,12 +18,10 @@ ~/.config/tabset: configs/tabset ~/.local/share/remmina: configs/remmina/hosts ~/.config/nvim: configs/nvim - -- create: - - ~/Downloads - - ~/bin - - ~/projects - - ~/src + ~/bin/catto: + path: configs/scripts/catto + mode: 755 - shell: - - [git submodule update --init --recursive, Installing submodules] \ No newline at end of file + - [git submodule update --init --recursive, Installing submodules] + - [chmod +x configs/scripts/catto, Making catto executable] \ No newline at end of file