Downloading images from Reddit using Python 3

The name of the pictureThe name of the pictureThe name of the pictureClash 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')






share|improve this question

















  • 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
















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')






share|improve this question

















  • 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












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')






share|improve this question













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')








share|improve this question












share|improve this question




share|improve this question








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 building praw.Reddit?
    – Mathias Ettinger
    Jan 8 at 20:11












  • 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







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















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

Python Lists

Aion

JavaScript Array Iteration Methods