Tiger6117

[Python]-Reddit Poster Script

May 25th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.06 KB | None | 0 0
  1. #Script Process :
  2. #logs in
  3. #grabs data from text file
  4. #begin loop (restarts from here for every line in the text file)
  5. #posts data
  6. #waits for 10 mins to prevent ban/captcha/error (if you post spam you'll get banned anyway)
  7. #goes back to step 3 (begin loop) until it has completed this for every line in the text file.
  8. #When it has ran through the loop for every line in the text file, the script will finish.
  9.  
  10. #Please see flags to learn how to use it, or run with the '-h' flag to display help.
  11.  
  12.  
  13. #dependencies :
  14. #python3
  15. #argparse
  16. #praw
  17.  
  18. #useage example :
  19. #ex1: reddit.py -u "username" -p "password"
  20. #ex2: reddit.py -u "username" -p "password" -f "filepath.txt" -t 600 -sub "Pics"
  21.  
  22. #flags explained :
  23. #-u = username - default=None
  24. #-p = password - default = None
  25. #-f = filename or path to file - default = 'reddit_posts.txt'
  26. #-t = timer value in seconds - default = 600 (10 mins)
  27. #-sub = subreddit to post - default = Pics
  28. #-s = line separator (IE '|' or ':') - default = '|'
  29.  
  30. #How to use :
  31. #make a plain text file formatted like this :
  32. #post_title | url_to_post
  33.  
  34. #launch script
  35.  
  36. #if confused, launch the script with the '-h' flag
  37. #-----------------------------------------------------#
  38.  
  39. #!/usr/bin/python3
  40.  
  41. import argparse,praw,time
  42.  
  43. parser = argparse.ArgumentParser()
  44. parser.add_argument('-u','--username',help='reddit.com username',type=str)
  45. parser.add_argument('-p','--password',help='reddit.com password',type=str)
  46. parser.add_argument('-f','--filename',help='file containing data to be posted',type=str,default='reddit_posts.txt')
  47. parser.add_argument('-s','--spacer',help='character to seperate title|url inside text file',type=str,default='|')
  48. parser.add_argument('-t','--timer',help='amount of time to wait between posts (in seconds)',type=int,default=600)
  49. parser.add_argument('-sub','--subreddit',help='subreddit to post too',type=str,default='Pics')
  50. args = parser.parse_args()
  51.  
  52. def post(title,post_url):
  53.     if r.is_logged_in() == True:
  54.         print("logged in successfully")
  55.         r.submit(args.subreddit,title,text=None,url=post_url)
  56.     else:
  57.         print('login failed, aborting...')
  58.         exit()
  59.  
  60. if __name__ == '__main__':
  61.     if args.username =='' or args.password == '':
  62.         print("please try using the '-h' flag to understand this script better")
  63.         print('no username/password given, aborting...')
  64.         exit()
  65.     with open(args.filename) as f:
  66.         r = praw.Reddit(user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36')
  67.         r.login(args.username, args.password,disable_warning=True)
  68.         for line in f.read().splitlines():
  69.             combo = line.split(args.spacer)
  70.             title = combo[0]
  71.             post_url = combo[1]
  72.             try:
  73.                 print('posting : {} - {}'.format(title, post_url))
  74.                 post(title,post_url)
  75.                 print('posted : {} - {}'.format(title,post_url))
  76.                 time.sleep(args.timer)
  77.             except Exception as e:
  78.                 print(e)
  79.                 break
Add Comment
Please, Sign In to add comment