-
Notifications
You must be signed in to change notification settings - Fork 46
/
wallhaven-dl.py
130 lines (110 loc) · 4.5 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
########################################################
# Program to Download Wallpapers from #
# alpha.wallhaven.cc #
# #
# Author - Saurabh Bhan #
# #
# Dated- 26 June 2016 #
# Update - 11 June 2019 #
########################################################
import os
import getpass
import re
import requests
import tqdm
import time
import urllib
import json
os.makedirs('Wallhaven', exist_ok=True)
BASEURL=""
cookies=dict()
global APIKEY
APIKEY = "EnterYourAPIKeyHere"
def category():
global BASEURL
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: ').lower()
ctags = {'all':'111', 'anime':'010', 'general':'100', 'people':'001', 'ga':'110', 'gp':'101' }
ctag = ctags[ccode]
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]
BASEURL = 'https://wallhaven.cc/api/v1/search?apikey=' + APIKEY + "&categories=" +\
ctag + '&purity=' + ptag + '&page='
def latest():
global BASEURL
print('Downloading latest')
topListRange = '1M'
BASEURL = 'https://wallhaven.cc/api/v1/search?apikey=' + APIKEY + '&topRange=' +\
topListRange + '&sorting=toplist&page='
def search():
global BASEURL
query = input('Enter search query: ')
BASEURL = 'https://wallhaven.cc/api/v1/search?apikey=' + APIKEY + '&q=' + \
urllib.parse.quote_plus(query) + '&page='
def downloadPage(pageId, totalImage):
url = BASEURL + str(pageId)
urlreq = requests.get(url, cookies=cookies)
pagesImages = json.loads(urlreq.content);
pageData = pagesImages["data"]
for i in range(len(pageData)):
currentImage = (((pageId - 1) * 24) + (i + 1))
url = pageData[i]["path"]
filename = os.path.basename(url)
osPath = os.path.join('Wallhaven', filename)
if not os.path.exists(osPath):
imgreq = requests.get(url, cookies=cookies)
if imgreq.status_code == 200:
print("Downloading : %s - %s / %s" % (filename, currentImage , totalImage))
with open(osPath, 'ab') as imageFile:
for chunk in imgreq.iter_content(1024):
imageFile.write(chunk)
elif (imgreq.status_code != 403 and imgreq.status_code != 404):
print("Unable to download %s - %s / %s" % (filename, currentImage , totalImage))
else:
print("%s already exist - %s / %s" % (filename, currentImage , totalImage))
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':
category()
elif Choice == 'latest':
latest()
elif Choice == 'search':
search()
pgid = int(input('How Many pages you want to Download: '))
totalImageToDownload = str(24 * pgid)
print('Number of Wallpapers to Download: ' + totalImageToDownload)
for j in range(1, pgid + 1):
downloadPage(j, totalImageToDownload)
if __name__ == '__main__':
main()