| 1 | #!/bin/sh |
| 2 | |
| 3 | # https://github.com/pystardust/waldl |
| 4 | # script to find and download wallpapers from wallhaven |
| 5 | version="0.0.1" |
| 6 | |
| 7 | # Usage: |
| 8 | # waldl <query> |
| 9 | # if query left empty then sh_menu will be used (dmenu by default) |
| 10 | # after the thumbnails are cached, sxiv would open up with the thumbnails |
| 11 | # Select the wallpapers using `m` on the image. ( marking the image in sxiv ) |
| 12 | # press `q` to quit sxiv, the marked images would start downloading |
| 13 | |
| 14 | #################### |
| 15 | ## User variables ## |
| 16 | #################### |
| 17 | |
| 18 | # the dir where wallpapers are stored |
| 19 | walldir="$HOME/pix/wallhaven" |
| 20 | # the dir used to cache thumbnails |
| 21 | cachedir="$HOME/.cache/wallhaven" |
| 22 | # sxiv options |
| 23 | sxiv_otps=" -tfpo -z 200" # o is needed for selection |
| 24 | # number of pages to show in search results |
| 25 | # each page contains 24 results |
| 26 | max_pages=4 |
| 27 | # sorting : date_added, relevance, random, views, favorites, toplist |
| 28 | sorting=relevance |
| 29 | # quality : large original small |
| 30 | quality=large |
| 31 | # atleast : least res |
| 32 | atleast=1920x1080 |
| 33 | |
| 34 | # the menu command used when no query is provided |
| 35 | sh_menu () { |
| 36 | : | dmenu -p "search wallhaven: " |
| 37 | # ROFI: comment the previous line and uncomment the next line for rofi |
| 38 | # rofi -dmenu -l 0 -p "search wallpapers" |
| 39 | } |
| 40 | |
| 41 | ########################## |
| 42 | ## getting search query ## |
| 43 | ########################## |
| 44 | |
| 45 | [ -n "$*" ] && query="$*" || query=$( sh_menu ) |
| 46 | [ -z "$query" ] && exit 1 |
| 47 | query=$(printf '%s' "$query" | tr ' ' '+' ) |
| 48 | |
| 49 | ###################### |
| 50 | ## start up commands # |
| 51 | ###################### |
| 52 | |
| 53 | rm -rf "$cachedir" |
| 54 | mkdir -p "$walldir" "$cachedir" |
| 55 | |
| 56 | # progress display command |
| 57 | sh_info () { |
| 58 | printf "%s\n" "$1" >&2 |
| 59 | notify-send "wallhaven" "$1" |
| 60 | [ -n "$2" ] && exit "$2" |
| 61 | } |
| 62 | |
| 63 | # dependency checking |
| 64 | dep_ck () { |
| 65 | for pr; do |
| 66 | command -v $pr >/dev/null 2>&1 || sh_info "command $pr not found, install: $pr" 1 |
| 67 | done |
| 68 | } |
| 69 | dep_ck "sxiv" "curl" "jq" |
| 70 | |
| 71 | |
| 72 | # clean up command that would be called when the program exits |
| 73 | clean_up () { |
| 74 | printf "%s\n" "cleaning up..." >&2 |
| 75 | rm -rf "$datafile" "$cachedir" |
| 76 | } |
| 77 | |
| 78 | # data file to store the api information |
| 79 | datafile="/tmp/wald.$$" |
| 80 | |
| 81 | # clean up if killed |
| 82 | trap "exit" INT TERM |
| 83 | trap "clean_up" EXIT |
| 84 | |
| 85 | ################## |
| 86 | ## getting data ## |
| 87 | ################## |
| 88 | |
| 89 | # request the search results for each page |
| 90 | get_results () { |
| 91 | for page_no in $(seq $max_pages) |
| 92 | do |
| 93 | { |
| 94 | json=$(curl -s -G "https://wallhaven.cc/api/v1/search" \ |
| 95 | -d "q=$1" \ |
| 96 | -d "page=$page_no" \ |
| 97 | -d "atleast=$atleast" \ |
| 98 | -d "sorting=$sorting" |
| 99 | ) |
| 100 | printf "%s\n" "$json" >> "$datafile" |
| 101 | } & |
| 102 | sleep 0.001 |
| 103 | done |
| 104 | wait |
| 105 | } |
| 106 | |
| 107 | # search wallpapers |
| 108 | sh_info "getting data..." |
| 109 | get_results "$query" |
| 110 | |
| 111 | # check if data file is empty, if so then exit |
| 112 | [ -s "$datafile" ] || sh_info "no images found" 1 |
| 113 | |
| 114 | ############################ |
| 115 | ## downloading thumbnails ## |
| 116 | ############################ |
| 117 | |
| 118 | # get a list of thumnails from the data |
| 119 | thumbnails=$( jq -r '.data[]?|.thumbs.'"$quality" < "$datafile") |
| 120 | |
| 121 | [ -z "$thumbnails" ] && sh_info "no-results found" 1 |
| 122 | |
| 123 | # download the thumbnails |
| 124 | sh_info "caching thumbnails..." |
| 125 | for url in $thumbnails |
| 126 | do |
| 127 | printf "url = %s\n" "$url" |
| 128 | printf "output = %s\n" "$cachedir/${url##*/}" |
| 129 | done | curl -Z -K - |
| 130 | #sh_info "downloaded thumbnails..." |
| 131 | |
| 132 | ########################### |
| 133 | ## user selection (sxiv) ## |
| 134 | ########################### |
| 135 | |
| 136 | # extract the id's out of the thumbnail name |
| 137 | image_ids="$(sxiv $sxiv_otps "$cachedir")" |
| 138 | [ -z "$image_ids" ] && exit |
| 139 | |
| 140 | ######################### |
| 141 | ## download wallpapers ## |
| 142 | ######################### |
| 143 | |
| 144 | # download the selected wall papers |
| 145 | mkdir "$walldir/$query" |
| 146 | cd "$walldir/$query" |
| 147 | sh_info "downloading wallpapers..." |
| 148 | for ids in $image_ids |
| 149 | do |
| 150 | ids="${ids##*/}" |
| 151 | ids="${ids%.*}" |
| 152 | url=$( jq -r '.data[]?|select( .id == "'$ids'" )|.path' < "$datafile" ) |
| 153 | printf "url = %s\n" "$url" |
| 154 | printf -- "-O\n" |
| 155 | done | curl -K - |
| 156 | |
| 157 | sh_info "wallpapers downloaded in:- '$walldir/$query'" |
| 158 | sxiv $(ls -c) |