Advertisement
Guest User

Untitled

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