Krolyxon
Commit a5ec147

Branches

Refs →

dotfiles-x11.localbinwaldl

158 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, 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
19walldir="$HOME/pix/wallhaven"
20# the dir used to cache thumbnails
21cachedir="$HOME/.cache/wallhaven"
22# sxiv options
23sxiv_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 # 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
47query=$(printf '%s' "$query" | tr ' ' '+' )
48
49######################
50## start up commands #
51######################
52
53rm -rf "$cachedir"
54mkdir -p "$walldir" "$cachedir"
55
56# progress display command
57sh_info () {
58 printf "%s\n" "$1" >&2
59 notify-send "wallhaven" "$1"
60 [ -n "$2" ] && exit "$2"
61}
62
63# dependency checking
64dep_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}
69dep_ck "sxiv" "curl" "jq"
70
71
72# clean up command that would be called when the program exits
73clean_up () {
74 printf "%s\n" "cleaning up..." >&2
75 rm -rf "$datafile" "$cachedir"
76}
77
78# data file to store the api information
79datafile="/tmp/wald.$$"
80
81# clean up if killed
82trap "exit" INT TERM
83trap "clean_up" EXIT
84
85##################
86## getting data ##
87##################
88
89# request the search results for each page
90get_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
108sh_info "getting data..."
109get_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
119thumbnails=$( jq -r '.data[]?|.thumbs.'"$quality" < "$datafile")
120
121[ -z "$thumbnails" ] && sh_info "no-results found" 1
122
123# download the thumbnails
124sh_info "caching thumbnails..."
125for url in $thumbnails
126do
127 printf "url = %s\n" "$url"
128 printf "output = %s\n" "$cachedir/${url##*/}"
129done | 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
137image_ids="$(sxiv $sxiv_otps "$cachedir")"
138[ -z "$image_ids" ] && exit
139
140#########################
141## download wallpapers ##
142#########################
143
144# download the selected wall papers
145mkdir "$walldir/$query"
146cd "$walldir/$query"
147sh_info "downloading wallpapers..."
148for ids in $image_ids
149do
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"
155done | curl -K -
156
157sh_info "wallpapers downloaded in:- '$walldir/$query'"
158sxiv $(ls -c)