Downloading images from Reddit using Python 3

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
4
down vote
favorite
I have been working on this small project in order to fetch top-notch images from Reddit. Overall I am pretty confident about the code below but I am not entirely sure about whether my error handling conforms to best practices or not. (Obviously account-related data has been 'censored'.) Thoughts?
#!/usr/bin/env python3
import os
import praw # reddit API Wrapper
from urllib.error import URLError, HTTPError
import urllib.request as web
import shutil # shell utilities
import colorama
from colorama import Fore, Style
def main(subreddit_name = 'all', current_dir = os.getcwd()):
reddit = praw.Reddit(
client_id = 'censored',
client_secret = 'censored',
username = 'censored',
password = 'censored',
user_agent = 'subreddit crawler')
dir_path = os.path.join(current_dir,subreddit_name)
if not os.path.exists(dir_path):
os.mkdir(dir_path)
colorama.init(autoreset = True)
subreddit = reddit.subreddit(subreddit_name).top(limit=1000)
print("Download files...")
for submissions in subreddit:
if not submissions.stickied and submissions.score > 50:
fullfilename = os.path.join(dir_path, ".jpg".format(submissions))
request = web.Request(submissions.url, headers = 'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0')
with web.urlopen(request) as response, open(fullfilename, 'wb') as out_file:
try:
shutil.copyfileobj(response, out_file)
except HTTPError as e:
print("".format(Fore.RED, e.code, Style.RESET_ALL))
except URLError as e:
print("".format(Fore.RED, e.reason, Style.RESET_ALL))
# delete corrupted files smaller than 55 KB
filesize = os.path.getsize(fullfilename)
if filesize < 55000:
os.remove(fullfilename)
else:
# this is for debugging purposes only
temp = "[:07dKB]".format(Fore.GREEN, filesize, Style.RESET_ALL)
print(" ID: URL: ".format(temp, submissions, submissions.url))
dir_count = len(os.listdir(dir_path))
print("Download succeeded. files saved in ''.".format(dir_count, dir_path))
if __name__ == '__main__':
main('PrequelMemes')
python python-3.x error-handling http reddit
add a comment |Â
up vote
4
down vote
favorite
I have been working on this small project in order to fetch top-notch images from Reddit. Overall I am pretty confident about the code below but I am not entirely sure about whether my error handling conforms to best practices or not. (Obviously account-related data has been 'censored'.) Thoughts?
#!/usr/bin/env python3
import os
import praw # reddit API Wrapper
from urllib.error import URLError, HTTPError
import urllib.request as web
import shutil # shell utilities
import colorama
from colorama import Fore, Style
def main(subreddit_name = 'all', current_dir = os.getcwd()):
reddit = praw.Reddit(
client_id = 'censored',
client_secret = 'censored',
username = 'censored',
password = 'censored',
user_agent = 'subreddit crawler')
dir_path = os.path.join(current_dir,subreddit_name)
if not os.path.exists(dir_path):
os.mkdir(dir_path)
colorama.init(autoreset = True)
subreddit = reddit.subreddit(subreddit_name).top(limit=1000)
print("Download files...")
for submissions in subreddit:
if not submissions.stickied and submissions.score > 50:
fullfilename = os.path.join(dir_path, ".jpg".format(submissions))
request = web.Request(submissions.url, headers = 'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0')
with web.urlopen(request) as response, open(fullfilename, 'wb') as out_file:
try:
shutil.copyfileobj(response, out_file)
except HTTPError as e:
print("".format(Fore.RED, e.code, Style.RESET_ALL))
except URLError as e:
print("".format(Fore.RED, e.reason, Style.RESET_ALL))
# delete corrupted files smaller than 55 KB
filesize = os.path.getsize(fullfilename)
if filesize < 55000:
os.remove(fullfilename)
else:
# this is for debugging purposes only
temp = "[:07dKB]".format(Fore.GREEN, filesize, Style.RESET_ALL)
print(" ID: URL: ".format(temp, submissions, submissions.url))
dir_count = len(os.listdir(dir_path))
print("Download succeeded. files saved in ''.".format(dir_count, dir_path))
if __name__ == '__main__':
main('PrequelMemes')
python python-3.x error-handling http reddit
1
What's the point of providing both an ID/secret and a username/password? Are you sure all the fields are required when buildingpraw.Reddit?
â Mathias Ettinger
Jan 8 at 20:11
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I have been working on this small project in order to fetch top-notch images from Reddit. Overall I am pretty confident about the code below but I am not entirely sure about whether my error handling conforms to best practices or not. (Obviously account-related data has been 'censored'.) Thoughts?
#!/usr/bin/env python3
import os
import praw # reddit API Wrapper
from urllib.error import URLError, HTTPError
import urllib.request as web
import shutil # shell utilities
import colorama
from colorama import Fore, Style
def main(subreddit_name = 'all', current_dir = os.getcwd()):
reddit = praw.Reddit(
client_id = 'censored',
client_secret = 'censored',
username = 'censored',
password = 'censored',
user_agent = 'subreddit crawler')
dir_path = os.path.join(current_dir,subreddit_name)
if not os.path.exists(dir_path):
os.mkdir(dir_path)
colorama.init(autoreset = True)
subreddit = reddit.subreddit(subreddit_name).top(limit=1000)
print("Download files...")
for submissions in subreddit:
if not submissions.stickied and submissions.score > 50:
fullfilename = os.path.join(dir_path, ".jpg".format(submissions))
request = web.Request(submissions.url, headers = 'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0')
with web.urlopen(request) as response, open(fullfilename, 'wb') as out_file:
try:
shutil.copyfileobj(response, out_file)
except HTTPError as e:
print("".format(Fore.RED, e.code, Style.RESET_ALL))
except URLError as e:
print("".format(Fore.RED, e.reason, Style.RESET_ALL))
# delete corrupted files smaller than 55 KB
filesize = os.path.getsize(fullfilename)
if filesize < 55000:
os.remove(fullfilename)
else:
# this is for debugging purposes only
temp = "[:07dKB]".format(Fore.GREEN, filesize, Style.RESET_ALL)
print(" ID: URL: ".format(temp, submissions, submissions.url))
dir_count = len(os.listdir(dir_path))
print("Download succeeded. files saved in ''.".format(dir_count, dir_path))
if __name__ == '__main__':
main('PrequelMemes')
python python-3.x error-handling http reddit
I have been working on this small project in order to fetch top-notch images from Reddit. Overall I am pretty confident about the code below but I am not entirely sure about whether my error handling conforms to best practices or not. (Obviously account-related data has been 'censored'.) Thoughts?
#!/usr/bin/env python3
import os
import praw # reddit API Wrapper
from urllib.error import URLError, HTTPError
import urllib.request as web
import shutil # shell utilities
import colorama
from colorama import Fore, Style
def main(subreddit_name = 'all', current_dir = os.getcwd()):
reddit = praw.Reddit(
client_id = 'censored',
client_secret = 'censored',
username = 'censored',
password = 'censored',
user_agent = 'subreddit crawler')
dir_path = os.path.join(current_dir,subreddit_name)
if not os.path.exists(dir_path):
os.mkdir(dir_path)
colorama.init(autoreset = True)
subreddit = reddit.subreddit(subreddit_name).top(limit=1000)
print("Download files...")
for submissions in subreddit:
if not submissions.stickied and submissions.score > 50:
fullfilename = os.path.join(dir_path, ".jpg".format(submissions))
request = web.Request(submissions.url, headers = 'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0')
with web.urlopen(request) as response, open(fullfilename, 'wb') as out_file:
try:
shutil.copyfileobj(response, out_file)
except HTTPError as e:
print("".format(Fore.RED, e.code, Style.RESET_ALL))
except URLError as e:
print("".format(Fore.RED, e.reason, Style.RESET_ALL))
# delete corrupted files smaller than 55 KB
filesize = os.path.getsize(fullfilename)
if filesize < 55000:
os.remove(fullfilename)
else:
# this is for debugging purposes only
temp = "[:07dKB]".format(Fore.GREEN, filesize, Style.RESET_ALL)
print(" ID: URL: ".format(temp, submissions, submissions.url))
dir_count = len(os.listdir(dir_path))
print("Download succeeded. files saved in ''.".format(dir_count, dir_path))
if __name__ == '__main__':
main('PrequelMemes')
python python-3.x error-handling http reddit
edited Jan 8 at 18:43
200_success
123k14143401
123k14143401
asked Jan 8 at 16:28
å¬海æÂÂè¡£
5816
5816
1
What's the point of providing both an ID/secret and a username/password? Are you sure all the fields are required when buildingpraw.Reddit?
â Mathias Ettinger
Jan 8 at 20:11
add a comment |Â
1
What's the point of providing both an ID/secret and a username/password? Are you sure all the fields are required when buildingpraw.Reddit?
â Mathias Ettinger
Jan 8 at 20:11
1
1
What's the point of providing both an ID/secret and a username/password? Are you sure all the fields are required when building
praw.Reddit?â Mathias Ettinger
Jan 8 at 20:11
What's the point of providing both an ID/secret and a username/password? Are you sure all the fields are required when building
praw.Reddit?â Mathias Ettinger
Jan 8 at 20:11
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184594%2fdownloading-images-from-reddit-using-python-3%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
What's the point of providing both an ID/secret and a username/password? Are you sure all the fields are required when building
praw.Reddit?â Mathias Ettinger
Jan 8 at 20:11