Advertisement
inqw

Untitled

Jun 12th, 2017
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.23 KB | None | 0 0
  1. import praw
  2. import re
  3. import time
  4. import datetime
  5.  
  6.  
  7. def get_time():
  8.     return time.mktime(datetime.datetime.now().timetuple())
  9.  
  10. def login(password):
  11.     reddit = praw.Reddit(
  12.         username='MyRSSBot',
  13.         password=password,
  14.         client_id='qTnjdOyhvYjzWw',
  15.         client_secret='t7pZnB4QboZ9r1x09-QEUO5NHdI',
  16.         user_agent='NeutralContentAdder v2 - adding neutral politics to the sub.')
  17.     return reddit
  18.  
  19. def write_to_file(username, file='users.txt'):
  20.     with open(file,'a') as f:
  21.         f.write(username+'\n')
  22.         f.close()
  23.  
  24. def check_file_for_username(username,file='users.txt'):
  25.     with open(file,'r') as f:
  26.         if username in f.read().split('\n'):
  27.             return True
  28.         else:
  29.             return False
  30.  
  31. def add_users_from_target(target_subreddit,home_subreddit,password):
  32.     target_sub = re.sub('\/?[rR]\/','',target_subreddit)
  33.     home_sub = re.sub('\/?[rR]\/','',home_subreddit)
  34.     reddit = login(password)
  35.     target_subreddit = reddit.subreddit(target_sub)
  36.     home_subreddit = reddit.subreddit(home_sub)
  37.     total = 0
  38.     for comment in target_subreddit.stream.comments():
  39.         try:
  40.             if not check_file_for_username(comment.author.name):
  41.                 now = get_time()
  42.                 total += 1
  43.                 write_to_file(comment.author.name)
  44.                 print("Adding user number {} - /u/{}".format(total,comment.author.name))
  45.                 home_subreddit.contributor.add(comment.author.name)
  46.                 later = get_time()
  47.                 difference = later-now
  48.                 print('It took {} seconds to add /u/{} to /r/{}'.format(int(difference), comment.author.name, home_sub))
  49.             else:
  50.                 continue
  51.         except Exception as e:
  52.             print(e)
  53.             continue
  54.  
  55. def countdown(integer):
  56.     print("Starting in {}".format(integer))
  57.     time.sleep(1)
  58.     interval = list(reversed([i for i in range(integer) if i != 0]))
  59.     for n in interval:
  60.         print(n)
  61.         time.sleep(1)
  62.  
  63. def main():
  64.     password=input('Please input password for MyRSSBot: ')
  65.     verify_password=input('Please verify the password above: ')
  66.     if password != verify_password:
  67.         print("Passwords do not match, please try again.")
  68.         time.sleep(3)
  69.         main()
  70.     target = input('Please enter the subreddit you would like to add users from: ')
  71.     home = input("Please enter the subreddit you would like to add them to: ")
  72.     countdown(5)
  73.     add_users_from_target(target,home,password)
  74.  
  75. if __name__ == '__main__':
  76.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement