-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallgrab.sh
executable file
·178 lines (153 loc) · 4.46 KB
/
wallgrab.sh
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# Function to prompt user for input and handle skipping
prompt_input() {
read -p "$1 (Press Enter to skip): " input
if [[ -z "$input" ]]; then
echo "skip"
else
echo "$input"
fi
}
# Function to download wallpapers
download_wallpapers() {
local response="$1"
# local threshold="$2"
THRESHOLD=$(prompt_input "Enter the number of wallpapers you want to download")
if [[ "$THRESHOLD" == "skip" ]]; then
THRESHOLD=0
fi
local counter=0
echo "$response" | jq -c '.data[]' | while read -r row; do
IMAGE_URL=$(echo "$row" | jq -r '.path')
if [[ ! -z "$IMAGE_URL" ]]; then
((counter++))
mkdir -p ~/wallpapers
wget -q --show-progress -P ~/wallpapers "$IMAGE_URL"
if [[ $counter -eq $THRESHOLD ]]; then
break
fi
fi
done
}
# Prompt user for inputs
QUERY=$(prompt_input "Enter query (tagname / -tagname / +tag1 +tag2 / etc.)")
CATEGORIES=$(prompt_input "Enter categories (100/101/111/etc. - 1 for on, 0 for off)")
PURITY=$(prompt_input "Enter purity (100/110/111/etc. - 1 for on, 0 for off)")
SORTING=$(prompt_input "Enter sorting (date_added/relevance/random/views/favorites/toplist)")
ORDER=$(prompt_input "Enter order (desc/asc)")
TOPRANGE=$(prompt_input "Enter topRange (1d/3d/1w/1M/3M/6M/1y)")
ATLEAST=$(prompt_input "Enter atleast resolution (e.g., 1920x1080)")
RESOLUTIONS=$(prompt_input "Enter resolutions (comma-separated e.g., 1920x1080,1920x1200)")
RATIOS=$(prompt_input "Enter ratios (e.g., 16x9,16x10)")
COLORS=$(prompt_input "Enter colors (e.g., 660000,990000,cc0000,...)")
PAGE=$(prompt_input "Enter page (e.g., 1)")
SEED=$(prompt_input "Enter seed (optional)")
# Prompt user for threshold
# THRESHOLD=$(prompt_input "Enter the number of wallpapers you want to download")
# if [[ "$THRESHOLD" == "skip" ]]; then
# THRESHOLD=0
# fi
# Base API endpoint
BASE_URL="https://wallhaven.cc/api/v1/search"
URL="$BASE_URL"
# Constructing the API endpoint based on parameters
if [[ "$QUERY" != "skip" ]]; then
if [[ "$URL" == *'search' ]]; then
URL="$URL?q=$QUERY"
else
URL="$URL&q=$QUERY"
fi
fi
if [[ "$CATEGORIES" != "skip" ]]; then
if [[ "$URL" == *'search' ]]; then
URL="$URL?categories=$CATEGORIES"
else
URL="$URL&categories=$CATEGORIES"
fi
fi
if [[ "$PURITY" != "skip" ]]; then
if [[ "$URL" == *'search' ]]; then
URL="$URL?purity=$PURITY"
else
URL="$URL&purity=$PURITY"
fi
fi
if [[ "$SORTING" != "skip" ]]; then
if [[ "$URL" == *'search' ]]; then
URL="$URL?sorting=$SORTING"
else
URL="$URL&sorting=$SORTING"
fi
fi
if [[ "$ORDER" != "skip" ]]; then
if [[ "$URL" == *'search' ]]; then
URL="$URL&order=$ORDER"
else
URL="$URL&order=$ORDER"
fi
fi
if [[ "$TOPRANGE" != "skip" ]]; then
if [[ "$URL" == *'search' ]]; then
URL="$URL&topRange=$TOPRANGE"
else
URL="$URL&topRange=$TOPRANGE"
fi
fi
if [[ "$ATLEAST" != "skip" ]]; then
if [[ "$URL" == *'search' ]]; then
URL="$URL&atleast=$ATLEAST"
else
URL="$URL&atleast=$ATLEAST"
fi
fi
if [[ "$RESOLUTIONS" != "skip" ]]; then
if [[ "$URL" == *'search' ]]; then
URL="$URL&resolutions=$RESOLUTIONS"
else
URL="$URL&resolutions=$RESOLUTIONS"
fi
fi
if [[ "$RATIOS" != "skip" ]]; then
if [[ "$URL" == *'search' ]]; then
URL="$URL&ratios=$RATIOS"
else
URL="$URL&ratios=$RATIOS"
fi
fi
if [[ "$COLORS" != "skip" ]]; then
if [[ "$URL" == *'search' ]]; then
URL="$URL&colors=$COLORS"
else
URL="$URL&colors=$COLORS"
fi
fi
if [[ "$PAGE" != "skip" ]]; then
if [[ "$URL" == *'search' ]]; then
URL="$URL&page=$PAGE"
else
URL="$URL&page=$PAGE"
fi
fi
if [[ "$SEED" != "skip" ]]; then
if [[ "$URL" == *'search' ]]; then
URL="$URL&seed=$SEED"
else
URL="$URL&seed=$SEED"
fi
fi
# Debug: Print the constructed URL
echo "Constructed URL: $URL"
# Send request and save the response
RESPONSE=$(curl -s "$URL")
# Check if the response is valid
if [[ $? -ne 0 ]]; then
echo "Failed to fetch data from the API."
exit 1
fi
# Check if the 'data' field exists in the JSON response
if ! echo "$RESPONSE" | jq -e '.data' > /dev/null; then
echo "No 'data' field found in the API response."
exit 1
fi
# Download wallpapers
download_wallpapers "$RESPONSE" "$PAGE"
echo "Images downloaded successfully to ~/wallpapers"