Guest User

Untitled

a guest
Jan 8th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import os
  4. import praw # reddit API Wrapper
  5. from urllib.error import URLError, HTTPError
  6. import urllib.request as web
  7. import shutil # shell utilities
  8. import colorama
  9. from colorama import Fore, Style
  10.  
  11. def main(subreddit_name = 'all', current_dir = os.getcwd()):
  12. reddit = praw.Reddit(
  13. client_id = 'censored',
  14. client_secret = 'censored',
  15. username = 'censored',
  16. password = 'censored',
  17. user_agent = 'subreddit crawler')
  18.  
  19. dir_path = os.path.join(current_dir,subreddit_name)
  20. if not os.path.exists(dir_path):
  21. os.mkdir(dir_path)
  22.  
  23. colorama.init(autoreset = True)
  24. subreddit = reddit.subreddit(subreddit_name).top(limit=1000)
  25.  
  26. print("Download files...")
  27.  
  28. for submissions in subreddit:
  29. if not submissions.stickied and submissions.score > 50:
  30. fullfilename = os.path.join(dir_path, "{}.jpg".format(submissions))
  31. request = web.Request(submissions.url, headers = {'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0'})
  32. with web.urlopen(request) as response, open(fullfilename, 'wb') as out_file:
  33. try:
  34. shutil.copyfileobj(response, out_file)
  35. except HTTPError as e:
  36. print("{}{}{}".format(Fore.RED, e.code, Style.RESET_ALL))
  37. except URLError as e:
  38. print("{}{}{}".format(Fore.RED, e.reason, Style.RESET_ALL))
  39.  
  40. # delete corrupted files smaller than 55 KB
  41. filesize = os.path.getsize(fullfilename)
  42. if filesize < 55000:
  43. os.remove(fullfilename)
  44. else:
  45. # this is for debugging purposes only
  46. temp = "{}[{:07d}KB]{}".format(Fore.GREEN, filesize, Style.RESET_ALL)
  47. print("{} ID: {} URL: {}".format(temp, submissions, submissions.url))
  48.  
  49. dir_count = len(os.listdir(dir_path))
  50. print("Download succeeded. {} files saved in '{}'.".format(dir_count, dir_path))
  51.  
  52. if __name__ == '__main__':
  53. main('PrequelMemes')
Add Comment
Please, Sign In to add comment