-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
130 lines (100 loc) · 2.98 KB
/
main.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
from reddit import RedditClientManager
from reddit import download_post
from PyInquirer import prompt
from reddit import Category
import concurrent.futures
from pathlib import Path
import time
import sys
import os
def ask_subreddit():
question = {
'type': 'input',
'name': 'subreddit',
'message': 'Enter the name of the subreddit:'
}
return prompt(question)['subreddit']
def select_category():
options = {
'type': 'list',
'name': 'choice',
'message': 'Select a category',
'choices': [
Category.HOT.value,
Category.TOP.value,
Category.NEW.value
]
}
return prompt(options)['choice']
def ask_limit():
question = {
'type': 'input',
'name': 'limit',
'message': 'How many images do you want to download?',
'validate': lambda val: val.isdigit() or 'Please Enter a number!'
}
return prompt(question)['limit']
def ask_download_path():
options = {
'type': 'list',
'name': 'choice',
'message': 'Where do you want to download the images?',
'choices': [
'1.Current folder',
'2.Create a new folder here and download',
'3.Enter a custom download path',
'4.Exit'
]
}
choice = prompt(options)['choice']
if '1' in choice:
return os.getcwd()
elif '2' in choice:
ques = {
'type': 'input',
'name': 'folder',
'message': 'Enter a folder name:'
}
folder = prompt(ques)['folder']
if not os.path.exists(folder):
os.mkdir(folder)
return folder
elif '3' in choice:
ques = {
'type': 'input',
'name': 'path',
'message': 'Enter path where images should be downloaded:'
}
return prompt(ques)['path']
else:
sys.exit()
def main():
subreddit = ask_subreddit()
category = select_category()
limit = int(ask_limit())
path = ask_download_path()
if not os.path.exists(path):
print('Invalid path')
return
os.chdir(Path(path))
client_manager = RedditClientManager()
reddit = client_manager.reddit_client
try:
posts_generator = getattr(reddit.subreddit(subreddit), category)
posts = list(posts_generator(limit=limit))
except:
return print("Invalid subreddit!")
imgs_downloaded = 0
start = time.perf_counter()
print("Downloading...")
processes = []
with concurrent.futures.ThreadPoolExecutor() as executor:
for i, post in enumerate(posts, 1):
processes.append(executor.submit(download_post, post.url, i))
end = time.perf_counter()
for p in concurrent.futures.as_completed(processes):
imgs_downloaded += int(p.result())
print(f"Complete! Downloaded {imgs_downloaded} of {limit} imgs.", end="")
print(f"It took {end-start} seconds.")
if __name__ == "__main__":
main()