Added reddit downloader

This commit is contained in:
Nox Sluijtman 2024-03-23 11:38:22 +01:00
parent e6743ce3f8
commit 6d36e06449
2 changed files with 31 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
*jpg
*jpeg

29
reddit-image.nu Executable file
View file

@ -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
}
}