-
Notifications
You must be signed in to change notification settings - Fork 0
/
waldl.py
79 lines (63 loc) · 2.24 KB
/
waldl.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
import os
from collpy import cprint
from progressbar import (
ProgressBar,
Percentage,
FileTransferSpeed,
ReverseBar,
DataSize,
AnimatedMarker,
)
import requests
import modules.query_action as query
import modules.search_wallpaper as sw
import modules.checkpoint as checkpoint
from modules.random_wallpaper_name import wallpaper_name
# download wallpaper
def download_wallpaper(wallpaper_url):
response = requests.get(url, stream=True)
total_size = int(response.headers.get("content-length", 0))
block_size = 1024 # 1 kb
extension = os.path.splitext(wallpaper_url)[1]
save_path = f"{checkpoint.image_save_path}{wallpaper_name()}{extension}"
widgets = [
"[ ",
AnimatedMarker(),
" ]",
FileTransferSpeed(),
" ",
ReverseBar(),
"< ",
Percentage(),
" > ",
DataSize(),
]
progress_bar = ProgressBar(widgets=widgets, maxval=total_size).start()
downloaded_size = 0
with open(save_path, "wb") as file:
for data in response.iter_content(block_size):
downloaded_size += len(data)
file.write(data)
progress_bar.update(downloaded_size)
progress_bar.finish()
try:
page_range = input("How many pages [ex: 1-4]: ")
first_number, second_number = page_range.split("-")
cprint(txt=f"Wallpaper will save on {checkpoint.image_save_path}", color="blue")
for page_number in range(int(first_number), int(second_number) + 1):
data = sw.search_wallpaper(query.query_options, page_number)
wallpaper_urls = data["wallpaper_urls"]
if len(wallpaper_urls) > 0 and page_number <= query.last_page_number:
cprint(
txt=f"[+] Downloading wallpapers of page: {page_number}", color="purple"
)
for url in wallpaper_urls:
download_wallpaper(url)
elif page_number > query.last_page_number:
cprint(txt=f"Total Page found : {query.last_page_number}", color="orange")
break
else:
cprint(txt=f"404:: No image found for : {query.query_options}", color="red")
break
except ValueError:
cprint(txt="Invalid page number. Please type like [1-10]", color="red")