Guest User

Reddit_Saved_Api

a guest
Apr 12th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. import praw
  2. import os
  3. import urllib
  4. from praw.models import Submission
  5. from StringIO import StringIO
  6. from PIL import Image
  7. Ifolder = open('C:\Users\Redacted\Pyfiles\RedditImage', 'w')
  8. _image_formats = ['bmp','dib','eps','ps','gif','im','jpg','jpe','jpeg',
  9.                   'pcd','pcx','png','pbm','pgm','ppm','psd','tif','tiff',
  10.                   'xbm','xpm','rgb','rast','svg']
  11.  
  12. r = praw.Reddit(client_id = 'CI',
  13.              client_secret = 'CS' ,
  14.              username='UN',
  15.              password='PW',
  16.              user_agent='prawtutorial')
  17. saved = r.user.me().saved(limit=None)
  18. counter = 0
  19.  
  20. for item in saved:
  21.    
  22.    
  23.     if isinstance(item, Submission):
  24.         if item.url.endswith(tuple(_image_formats)) == True:
  25.            
  26.             Ifolder.write(item.url)
  27.             Ifolder.write('\n')
  28.             try:
  29.                 jj = Image.open(StringIO(urllib.urlopen(item.url).read()))
  30.                
  31.                 if str(item.url).endswith(".jpg"):
  32.                     jj.save('C:\Users\Redacted\pyfiles\Redditimages\\'+str(counter)+".jpg")
  33.                     counter += 1
  34.                 if str(item.url).endswith(".png"):
  35.                     jj.save('C:\Users\Redacted\pyfiles\Redditimages\\'+str(counter)+".png")
  36.                     counter += 1
  37.             except:
  38.                 pass
Add Comment
Please, Sign In to add comment