forked from saurabhan/Wallhaven-dl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallhaven-dl.py
231 lines (200 loc) · 7.87 KB
/
wallhaven-dl.py
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
########################################################
# Program to Download Wallpapers from #
# alpha.wallhaven.cc #
# #
# Author - Saurabh Bhan #
# Updater - Bui The Hien #
# #
# dated- 26 June 2016 #
# Update - 20 May 2018 #
########################################################
import os
import getpass
import bs4
import re
import requests
import time
import urllib
import math
os.makedirs('Wallhaven', exist_ok=True)
def build_params():
params = ''
print('''****************************************************************
Category Codes
all - Every wallpaper.
general - For 'general' wallpapers only.
anime - For 'Anime' Wallpapers only.
people - For 'people' wallapapers only.
ga - For 'General' and 'Anime' wallapapers only.
gp - For 'General' and 'People' wallpapers only.
****************************************************************
''')
ccode = input('Enter Category: ')
ALL = '111'
ANIME = '010'
GENERAL = '100'
PEOPLE = '001'
GENERAL_ANIME = '110'
GENERAL_PEOPLE = '101'
if ccode.lower() == "all":
ctag = ALL
elif ccode.lower() == "anime":
ctag = ANIME
elif ccode.lower() == "general":
ctag = GENERAL
elif ccode.lower() == "people":
ctag = PEOPLE
elif ccode.lower() == "ga":
ctag = GENERAL_ANIME
elif ccode.lower() == "gp":
ctag = GENERAL_PEOPLE
params += "&categories=" + ctag
print('''
****************************************************************
Purity Codes
sfw - For 'Safe For Work'
sketchy - For 'Sketchy'
nsfw - For 'Not Safe For Work'
ws - For 'SFW' and 'Sketchy'
wn - For 'SFW' and 'NSFW'
sn - For 'Sketchy' and 'NSFW'
all - For 'SFW', 'Sketchy' and 'NSFW'
****************************************************************
''')
pcode = input('Enter Purity: ')
ptags = {'sfw':'100', 'sketchy':'010', 'nsfw':'001', 'ws':'110', 'wn':'101', 'sn':'011', 'all':'111'}
ptag = ptags[pcode]
params += "&purity=" + ptag
rtag = input('Enter at least resolution (ex: 1280x720): ')
if rtag:
params += "&atleast=" + rtag
ratio = input("Enter ratio (16x9): ")
if ratio:
params += "&ratios=" + ratio
stag = input('Enter sort type: relevance, random, date_added, views, favorites, toplist: ')
if stag:
params += "&sorting=" + stag
otag = input('Enter order of sort: desc, asc: ')
if otag:
params += "&order=" + otag
return params
def login():
print('NSFW images require login')
username = input('Enter username: ')
password = getpass.getpass('Enter password: ')
req = requests.post('https://alpha.wallhaven.cc/auth/login', data={'username':username, 'password':password})
return req.cookies
def category():
print('''****************************************************************
Category Codes
all - Every wallpaper.
general - For 'general' wallpapers only.
anime - For 'Anime' Wallpapers only.
people - For 'people' wallapapers only.
ga - For 'General' and 'Anime' wallapapers only.
gp - For 'General' and 'People' wallpapers only.
****************************************************************
''')
ccode = input('Enter Category: ')
ALL = '111'
ANIME = '010'
GENERAL = '100'
PEOPLE = '001'
GENERAL_ANIME = '110'
GENERAL_PEOPLE = '101'
if ccode.lower() == "all":
ctag = ALL
elif ccode.lower() == "anime":
ctag = ANIME
elif ccode.lower() == "general":
ctag = GENERAL
elif ccode.lower() == "people":
ctag = PEOPLE
elif ccode.lower() == "ga":
ctag = GENERAL_ANIME
elif ccode.lower() == "gp":
ctag = GENERAL_PEOPLE
print('''
****************************************************************
Purity Codes
sfw - For 'Safe For Work'
sketchy - For 'Sketchy'
nsfw - For 'Not Safe For Work'
ws - For 'SFW' and 'Sketchy'
wn - For 'SFW' and 'NSFW'
sn - For 'Sketchy' and 'NSFW'
all - For 'SFW', 'Sketchy' and 'NSFW'
****************************************************************
''')
pcode = input('Enter Purity: ')
ptags = {'sfw':'100', 'sketchy':'010', 'nsfw':'001', 'ws':'110', 'wn':'101', 'sn':'011', 'all':'111'}
ptag = ptags[pcode]
if pcode in ['nsfw', 'wn', 'sn', 'all']:
cookies = login()
else:
cookies = dict()
CATURL = 'https://alpha.wallhaven.cc/search?categories=' + \
ctag + '&purity=' + ptag + '&page='
return (CATURL, cookies)
def latest():
print('Downloading latest')
latesturl = 'https://alpha.wallhaven.cc/latest?page='
return (latesturl, dict())
def search():
query = input('Enter search query: ')
searchurl = 'https://alpha.wallhaven.cc/search?q=' + \
urllib.parse.quote_plus(query) + build_params() + '&page='
return (searchurl, dict())
from tqdm import tqdm
from math import *
def main():
Choice = input('''Choose how you want to download the image:
Enter "category" for downloading wallpapers from specified categories
Enter "latest" for downloading latest wallpapers
Enter "search" for downloading wallpapers from search
Enter choice: ''').lower()
while Choice not in ['category', 'latest', 'search']:
if Choice != None:
print('You entered an incorrect value.')
choice = input('Enter choice: ')
if Choice == 'category':
BASEURL, cookies = category()
elif Choice == 'latest':
BASEURL, cookies = latest()
elif Choice == 'search':
BASEURL, cookies = search()
pgid = int(input('How Many pages you want to Download: '))
startpg = int(input('Start page to begin download: '))
if startpg > pgid:
print("Can download when number of pages less than start page ")
return
print('Number of Wallpapers to Download: ' + str(24 * (pgid-startpg+1)))
for j in range(startpg, pgid + 1):
totalImage = str(24 * pgid)
url = BASEURL + str(j)
print('Starting download page: ' + str(j) + ', url: ' + url);
urlreq = requests.get(url, cookies=cookies)
soup = bs4.BeautifulSoup(urlreq.text, 'lxml')
soupid = soup.findAll('a', {'class': 'preview'})
res = re.compile(r'\d+')
imgid = res.findall(str(soupid))
imgext = ['jpg', 'png', 'bmp']
for i in range(len(imgid)):
currentImage = (((j - 1) * 24) + (i + 1))
url = 'http://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-%s.' % imgid[i]
for ext in imgext:
iurl = url + ext
osPath = os.path.join('Wallhaven', os.path.basename(iurl))
if not os.path.exists(osPath):
imgreq = requests.get(iurl, cookies=cookies, stream=True)
if imgreq.status_code == 200:
print("Downloading : %s - %s / %s" % ((os.path.basename(iurl)), currentImage , totalImage))
total_size = int(imgreq.headers.get('content-length', 0))
with open(osPath, 'ab') as imageFile:
for chunk in tqdm(imgreq.iter_content(1024), total=math.ceil(total_size/1024), unit='KB', unit_scale=True):
imageFile.write(chunk)
break
else:
print("%s already exist - %s / %s" % (os.path.basename(iurl), currentImage , totalImage))
if __name__ == '__main__':
main()