Krolyxon
Commit 73f71f3

Branches

Refs →

aurora.localbinwaldl

159 lines · 3.7 KB · Bourne Again Shell (bash)

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