-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
87 lines (71 loc) · 3.19 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
import os
import pyfiglet
import praw
import requests
print(pyfiglet.figlet_format("REDSAVE"))
print("\n")
# Reddit API credentials
client_id = input("Enter client_id: ")
client_secret = input("Enter client_secret: ")
username = input("Enter username: ")
password = input("Enter password: ")
# Subreddit name
subreddit_name = input("Enter subreddit name: ")
# Create a Reddit instance
reddit = praw.Reddit(client_id=client_id, client_secret=client_secret, username=username,password=password, user_agent="<user_agent>")
# Get the subreddit
subreddit = reddit.subreddit(subreddit_name)
# Directory to save the images/videos
save_dir = input("Enter the folder/directory name to save: ")
# Check if the directory exists, otherwise create it
if not os.path.exists(save_dir):
os.makedirs(save_dir)
#Entering the limit
Lim=input("Enter the Limit of posts requests you want to make: ")
print("SORT BY: 1.HOT / 2.NEW / 3.TOP : ")
Choice = str(input("Enter your choice 1/2/3 : "))
if Choice==1:
# Loop through the hot submissions in the subreddit
for submission in subreddit.hot(limit=Lim):
# Check if the submission is an image or video
if submission.url.endswith((".jpg", ".jpeg", ".png", ".gif", ".mp4")):
# Get the file name from the URL
file_name = submission.url.split("/")[-1]
file_path = os.path.join(save_dir, file_name)
# Download the file using the `requests` library
response = requests.get(submission.url)
with open(file_path, "wb") as file:
file.write(response.content)
print("Command successfully executed!")
print("Images/videos saved in the", save_dir, "directory.")
if Choice==2:
# Loop through the new submissions in the subreddit
for submission in subreddit.new(limit=Lim):
# Check if the submission is an image or video
if submission.url.endswith((".jpg", ".jpeg", ".png", ".gif", ".mp4")):
# Get the file name from the URL
file_name = submission.url.split("/")[-1]
file_path = os.path.join(save_dir, file_name)
# Download the file using the `requests` library
response = requests.get(submission.url)
with open(file_path, "wb") as file:
file.write(response.content)
print("Command successfully executed!")
print("Images/videos saved in the", save_dir, "directory.")
if Choice==3:
ch=input("TOP BY today/week/month/year/all: ")
# Loop through the top submissions in the subreddit
for submission in subreddit.top(ch,limit=Lim):
# Check if the submission is an image or video
if submission.url.endswith((".jpg", ".jpeg", ".png", ".gif", ".mp4")):
# Get the file name from the URL
file_name = submission.url.split("/")[-1]
file_path = os.path.join(save_dir, file_name)
# Download the file using the `requests` library
response = requests.get(submission.url)
with open(file_path, "wb") as file:
file.write(response.content)
print("Command successfully executed!")
print("Images/videos saved in the", save_dir, "directory.")
else:
pass