Advertisement
Guest User

Untitled

a guest
Aug 10th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.66 KB | None | 0 0
  1. import os
  2. import urllib.request as web
  3. import shutil
  4. from time import sleep
  5. import praw
  6.  
  7. def connect_api():
  8.     '''connect to reddits api'''
  9.     global REDDIT
  10.     #sign up with reddit and create an app to get this info
  11.     #NOTICE the capital "R" in 'praw.Reddit',
  12.     #I didn't for ages, and this wouldn't work
  13.     REDDIT = praw.Reddit(client_id='xxxxxxxxxxxxx',
  14.                          client_secret = 'xxxxxxxxxx',
  15.                          user_agent='Praw')
  16.  
  17. def go_getfiles():
  18.     '''download images from the subreddit'''
  19.     #change 'memes' to the subreddit you want
  20.     #and change 'limit=', to how many images you want.
  21.     #u can change '.top' to one of .hot .new .rising .gilded or .controversial
  22.     sub_reddit = REDDIT.subreddit("memes").new(limit=5)
  23.  
  24.     #change this to your desired save location
  25.     dir_path = ("D:\\Reddit images\\")
  26.  
  27.     for submissions in sub_reddit:
  28.         #ignore stickies
  29.         if not submissions.stickied:
  30.             fullfilename = os.path.join(dir_path, "{}.jpg".format(submissions))
  31.             request = web.Request(submissions.url)
  32.             #note linesplit
  33.             with web.urlopen(request) as response, \
  34.             open(fullfilename, 'wb') as out_file:
  35.                 shutil.copyfileobj(response, out_file)
  36.                 dir_count = len(os.listdir(dir_path))
  37.                 print(fullfilename)
  38.                 #note line split
  39.                 print("Downloaded. {} file(s) saved in '{}'." \
  40.                 .format(dir_count, dir_path))
  41.                 #optional,but prob good idea if u dont want to get banned
  42.                 sleep(1)
  43.  
  44. #main
  45. connect_api()
  46. go_getfiles()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement