diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1dd412c --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*jpg +*jpeg diff --git a/reddit-image.nu b/reddit-image.nu new file mode 100755 index 0000000..9dc462e --- /dev/null +++ b/reddit-image.nu @@ -0,0 +1,29 @@ +#!/usr/bin/env nu + +def main [--url(-u): string, --download(-d)] { + let clipboard = xclip -o + let url = $url. | default $clipboard + match ($url | str contains "reddit.com") { + true => ($url | get_url) + false => ("URL is not a valid Reddit URL"; exit 1) + } | download_url $download +} + + +# Documentation for handle_url +def download_url [download: bool] { + let input = $in + match $download { + true => (wget $input) + false => (kitten icat $input) + } +} + +def get_url [] { + let input = $in + let decoded = $input | url decode + match ($decoded | str contains "preview") { + true => ( ($decoded | parse "{reddit}={imageUrl}?{params}").imageUrl.0 | str replace "preview" "i" ) + false => ($decoded | parse "{reddit}={imageUrl}").imageUrl.0 + } +} \ No newline at end of file