aboutsummaryrefslogtreecommitdiffstats
path: root/waldl
blob: 633359070cc7304463949143fe8395d08b8a660c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/bin/sh

# https://git.xgenos.me/waldl
# script to find and download wallpapers from wallhaven
version="0.0.1"

# Usage:
# 	waldl <query>
# if query left empty then sh_menu will be used (dmenu by default)
# after the thumbnails are cached, sxiv would open up with the thumbnails
# Select the wallpapers using `m` on the image. ( marking the image in sxiv )
# press `q` to quit sxiv, the marked images would start downloading

####################
## User variables ##
####################

# the dir where wallpapers are stored
walldir="$HOME/pix/wall"
# the dir used to cache thumbnails
cachedir="$HOME/.cache/wallhaven"
# sxiv options
sxiv_otps=" -tfpo -z 200" # o is needed for selection
# number of pages to show in search results
# each page contains 24 results
max_pages=4
# sorting : date_added, relevance, random, views, favorites, toplist
sorting=relevance
# quality : large original small
quality=large
# atleast : least res
atleast=3840x2160

# allow the user to customize the defaults
[ -e "$HOME/.config/waldlrc" ] && . "$HOME/.config/waldlrc"

# the menu command used when no query is provided
sh_menu () {
	: | dmenu -p "search wallhaven:"
	# ROFI: comment the previous line and uncomment the next line for rofi
	# rofi -dmenu -l 0 -p "search wallpapers"
}

##########################
## getting search query ##
##########################

[ -n "$*" ] && query="$*" || query=$( sh_menu )
[ -z "$query" ] && exit 1
query=$(printf '%s' "$query" | tr ' ' '+' )

######################
## start up commands #
######################

rm -rf "$cachedir"
mkdir -p "$walldir" "$cachedir"

# progress display command
sh_info () {
	printf "%s\n" "$1" >&2
	notify-send "wallhaven" "$1"
	[ -n "$2" ] && exit "$2"
}

# dependency checking
dep_ck () {
	for pr; do
		command -v $pr >/dev/null 2>&1 || sh_info "command $pr not found, install: $pr" 1
	done
}
dep_ck "sxiv" "curl" "jq"


# clean up command that would be called when the program exits
clean_up () {
	printf "%s\n" "cleaning up..." >&2
	rm -rf "$datafile" "$cachedir"
}

# data file to store the api information
datafile="/tmp/wald.$$"

# clean up if killed
trap "exit" INT TERM
trap "clean_up" EXIT

##################
## getting data ##
##################

# request the search results for each page
get_results () {
	for page_no in $(seq $max_pages)
	do
		{
			json=$(curl -s -G "https://wallhaven.cc/api/v1/search" \
					-d "q=$1" \
					-d "page=$page_no" \
					-d "atleast=$atleast" \
					-d "sorting=$sorting"
				)
			printf "%s\n" "$json" >> "$datafile"
		} &
		sleep 0.001
	done
	wait
}

# search wallpapers
sh_info "getting data..."
get_results "$query"

# check if data file is empty, if so then exit
[ -s "$datafile" ] || sh_info "no images found" 1

############################
## downloading thumbnails ##
############################

# get a list of thumnails from the data
thumbnails=$( jq -r '.data[]?|.thumbs.'"$quality" < "$datafile")

[ -z "$thumbnails" ] && sh_info "no-results found" 1

# download the thumbnails
sh_info "caching thumbnails..."
for url in $thumbnails
do
		printf "url = %s\n" "$url"
		printf "output = %s\n" "$cachedir/${url##*/}"
done | curl -Z -K -
#sh_info "downloaded thumbnails..."

###########################
## user selection (sxiv) ##
###########################

# extract the id's out of the thumbnail name
image_ids="$(sxiv $sxiv_otps "$cachedir")"
[ -z "$image_ids" ] && exit

#########################
## download wallpapers ##
#########################

# download the selected wall papers
cd "$walldir"
sh_info "downloading wallpapers..."
for ids in $image_ids
do
	ids="${ids##*/}"
	ids="${ids%.*}"
	url=$( jq -r '.data[]?|select( .id == "'$ids'" )|.path' < "$datafile" )
	printf "url = %s\n" "$url"
	printf -- "-O\n"
done | curl -K -

sh_info "wallpapers downloaded in:- '$walldir'"
sxiv $(ls -c)