Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import urllib.request as web
- import shutil
- from time import sleep
- import praw
- def connect_api():
- '''connect to reddits api'''
- global REDDIT
- #sign up with reddit and create an app to get this info
- #NOTICE the capital "R" in 'praw.Reddit',
- #I didn't for ages, and this wouldn't work
- REDDIT = praw.Reddit(client_id='xxxxxxxxxxxxx',
- client_secret = 'xxxxxxxxxx',
- user_agent='Praw')
- def go_getfiles():
- '''download images from the subreddit'''
- #change 'memes' to the subreddit you want
- #and change 'limit=', to how many images you want.
- #u can change '.top' to one of .hot .new .rising .gilded or .controversial
- sub_reddit = REDDIT.subreddit("memes").new(limit=5)
- #change this to your desired save location
- dir_path = ("D:\\Reddit images\\")
- for submissions in sub_reddit:
- #ignore stickies
- if not submissions.stickied:
- fullfilename = os.path.join(dir_path, "{}.jpg".format(submissions))
- request = web.Request(submissions.url)
- #note linesplit
- with web.urlopen(request) as response, \
- open(fullfilename, 'wb') as out_file:
- shutil.copyfileobj(response, out_file)
- dir_count = len(os.listdir(dir_path))
- print(fullfilename)
- #note line split
- print("Downloaded. {} file(s) saved in '{}'." \
- .format(dir_count, dir_path))
- #optional,but prob good idea if u dont want to get banned
- sleep(1)
- #main
- connect_api()
- go_getfiles()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement