forked from sevensins/Wallbase-Downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallhaven.sh
171 lines (154 loc) · 6.09 KB
/
wallhaven.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
#!/bin/bash
#
##################################
### Needed for NSFW/New ###
##################################
# Enter your Username
USER=""
# Enter your password
PASS=""
#################################
### End needed for NSFW ###
#################################
#################################
### Configuration Options ###
#################################
# Where should the Wallpapers be stored?
LOCATION=/location/to/your/wallpaper/folder
# How many Wallpapers should be downloaded, should be multiples of 64 (right now they only use a fixed number of thumbs per page)
WPNUMBER=64
# Type standard (newest, oldest, random, hits, mostfav), search, favorites, useruploads
TYPE=standard
# From which Categories should Wallpapers be downloaded
CATEGORIES=111
# Which Purity Wallpapers should be downloaded
PURITY=111
# Which Resolution should be downloaded, leave empty for all
RESOLUTION=
# Which aspectratio should be downloaded, leave empty for all
RATIO=
# Which Type should be displayed (relevance, random, date_added, views, favorites)
SORTING=random
# How should the Wallpapers be ordered (desc, asc)
ORDER=desc
# Searchterm
QUERY="nature"
# User from which Wallpapers should be downloaded (only used for TYPE=useruploads)
USR=AksumkA
#################################
### End Configuration Options ###
#################################
if [ ! -d $LOCATION ]; then
mkdir -p $LOCATION
fi
cd $LOCATION
#
# logs in to the wallhaven website to give the user more functionality
# requires 2 arguments:
# arg1: username
# arg2: password
#
function login {
# checking parameters -> if not ok print error and exit script
if [ $# -lt 2 ] || [ $1 == '' ] || [ $2 == '' ]; then
printf "Please check the needed Options for NSFW Content (username and password)\n\n"
printf "For further Information see Section 13\n\n"
printf "Press any key to exit\n"
read
exit
fi
# everythings ok --> login
wget -q --keep-session-cookies --save-cookies=cookies.txt --referer=http://alpha.wallhaven.cc http://alpha.wallhaven.cc/auth/login
token="$(cat login | grep 'name="_token"' | sed 's .\{180\} ' | sed 's/.\{16\}$//')"
wget -q --load-cookies=cookies.txt --keep-session-cookies --save-cookies=cookies.txt --referer=http://alpha.wallhaven.cc/auth/login --post-data="_token=$token&username=$USER&password=$PASS" http://alpha.wallhaven.cc/auth/login
} # /login
#
# downloads Page with Thumbnails
#
function getPage {
# checking parameters -> if not ok print error and exit script
if [ $# -lt 1 ]; then
printf "getPage expects at least 1 argument\n"
printf "arg1: parameters for the wget -q command\n\n"
printf "press any key to exit\n"
read
exit
fi
# parameters ok --> get page
wget -q --keep-session-cookies --load-cookies=cookies.txt --referer=alpha.wallhaven.cc -O tmp "http://alpha.wallhaven.cc/$1"
} # /getPage
#
# downloads all the wallpapers from a wallpaperfile
# arg1: the file containing the wallpapers
#
function downloadWallpapers {
URLSFORIMAGES="$(cat tmp | grep -o '<a href="http://alpha.wallhaven.cc/wallpaper/[0-9]*"' | sed 's .\{9\} ')"
for imgURL in $URLSFORIMAGES
do
img="$(echo $imgURL | sed 's/.\{1\}$//')"
number="$(echo $img | sed 's .\{36\} ')"
if cat downloaded.txt | grep -w "$number" >/dev/null
then
printf "File already downloaded!\n"
else
echo $number >> downloaded.txt
wget -q --keep-session-cookies --load-cookies=cookies.txt --referer=alpha.wallhaven.cc $img
cat $number | egrep -o "http://alpha.wallhaven.cc/wallpapers.*(png|jpg|gif)" | wget -q --keep-session-cookies --load-cookies=cookies.txt --referer=http://alpha.wallhaven.cc/wallpaper/$number -i -
rm $number
fi
done
rm tmp
} #/downloadWallpapers
# login only when it is required ( for example to download favourites or nsfw content... )
if [ $PURITY == 001 ] || [ $PURITY == 011 ] || [ $PURITY == 111 ] ; then
login $USER $PASS
fi
if [ $TYPE == standard ]; then
for (( count= 0, page=1; count< "$WPNUMBER"; count=count+64, page=page+1 ));
do
printf "Download Page $page"
getPage "wallpaper/search?page=$page&categories=$CATEGORIES&purity=$PURITY&resolutions=$RESOLUTION&ratios=$RATIO&sorting=$SORTING&order=$ORDER"
printf " - done!\n"
printf "Download Wallpapers from Page $page"
downloadWallpapers
printf " - done!\n"
done
elif [ $TYPE == search ] ; then
# SEARCH
for (( count= 0, page=1; count< "$WPNUMBER"; count=count+"64", page=page+1 ));
do
printf "Download Page $page"
getPage "wallpaper/search?page=$page&categories=$CATEGORIES&purity=$PURITY&resolutions=$RESOLUTION&ratios=$RATIO&sorting=relevance&order=desc&q=$QUERY"
printf " - done!\n"
printf "Download Wallpapers from Page $page"
downloadWallpapers
printf " - done!\n"
done
elif [ $TYPE == favorites ] ; then
# FAVORITES
# currently using sum of all collections
favnumber="$(wget -q --keep-session-cookies --load-cookies=cookies.txt --referer=alpha.wallhaven.cc http://alpha.wallhaven.cc/favorites -O - | grep -A 1 "<span>Favorites</span>" | grep -B 1 "<small>" | sed -n '2{p;q}' | sed 's/.\{9\}$//' | sed 's .\{23\} ')"
for (( count= 0, page=1; count< "$WPNUMBER" && count< "$favnumber"; count=count+"64", page=page+1 ));
do
printf "Download Page $page"
getPage "favorites?page=$page"
printf " - done!\n"
printf "Download Wallpapers from Page $page"
downloadWallpapers
printf " - done!\n"
done
elif [ $TYPE == useruploads ] ; then
# UPLOADS FROM SPECIFIC USER
for (( count= 0, page=1; count< "$WPNUMBER"; count=count+"64", page=page+1 ));
do
printf "Download Page $page"
getPage "user/$USR/uploads?page=$page"
printf " - done!\n"
printf "Download Wallpapers from Page $page"
downloadWallpapers
printf " - done!\n"
done
else
printf "error in TYPE please check Variable\n"
fi
rm -f cookies.txt login login.1