Guest User

img and posst saver

a guest
Aug 3rd, 2022
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. import random
  2. import string
  3. import time
  4. import praw
  5. import shutil
  6. import requests
  7.  
  8. reddit = praw.Reddit(
  9.     client_id="clientid",
  10.     client_secret="secret",
  11.     user_agent="agent"
  12. )
  13.  
  14. print("Logged in successfully.") if reddit.read_only is True else print("Error logging in.")
  15.  
  16. temp_user = input("Please specify the user (just the username, do not include \"u/\"): ")
  17. user = reddit.redditor(temp_user)
  18.  
  19.  
  20. def scrape_user(user):
  21.     submissions = user.submissions.new(limit=10)
  22.  
  23.     for submission in submissions:
  24.         if submission.is_self:
  25.             save_txt(submission.title + " : " + submission.selftext + "\n \n")
  26.  
  27.         else:
  28.             save_img(submission.url)
  29.  
  30.  
  31. def save_img(url):
  32.     print("Saving img")
  33.  
  34.     response = requests.get(url, stream=True)
  35.  
  36.     if any(file_type in url for file_type in ['.jpeg', '.jpg', '.png']):
  37.         with open(''.join(random.choices(string.ascii_lowercase, k=5)) + '.jpeg', 'wb') as out_file:
  38.             shutil.copyfileobj(response.raw, out_file)
  39.         del response
  40.     else:
  41.         print("Not an img, skipping")
  42.  
  43.  
  44. def save_txt(text):
  45.     print("Saving text")
  46.  
  47.     with open('saved_text.txt', 'a') as f:
  48.         f.write(text)
  49.  
  50.  
  51. while True:
  52.     scrape_user(user)
  53.  
  54.     print("Done, sleeping for 15 mins")
  55.     time.sleep(900)  # 15 mins
  56.  
Advertisement
Add Comment
Please, Sign In to add comment