Created
September 6, 2016 22:05
-
-
Save NoobStuff/e7964e79df3236723fa7acc12980fdbe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import warnings | |
import praw.errors | |
import praw_script_oauth as pso | |
from getpass import getpass | |
USERAGENT = 'SubredditCopy:v1.0 - /u/NoobStuff' | |
CLIENTID = 'APP_ID_HERE' | |
CLIENTSECRET = 'SECRET_HERE' | |
# Ignore unclosed socket ResourceWarnings raised by praw | |
warnings.simplefilter('ignore', ResourceWarning) | |
def get_subreddits(credentials): | |
''' | |
Return set of subscribed subreddits from the user at `credentials` | |
:param credentials: username, password tuple | |
''' | |
username, password = credentials | |
scope = {'identity', 'mysubreddits'} | |
reddit = pso.connect(CLIENTID, CLIENTSECRET, username, password, oauth_scopes=scope, useragent=USERAGENT) | |
if reddit is None: | |
auth_exit(username) | |
return {s.display_name for s in reddit.get_my_subreddits(limit=None)} | |
def merge_subreddits(credentials, subreddits): | |
''' | |
Merge `subreddits` with the subreddits of user at `credentials` | |
:param credentials: username, password tuple | |
:param subreddits: set of subreddit names for subscribing to | |
''' | |
username, password = credentials | |
scope = {'identity', 'mysubreddits', 'subscribe'} | |
reddit = pso.connect(CLIENTID, CLIENTSECRET, username, password, oauth_scopes=scope, useragent=USERAGENT) | |
if reddit is None: | |
auth_exit(username) | |
current_subs = {s.display_name for s in reddit.get_my_subreddits(limit=None)} | |
for sub in subreddits.difference(current_subs): | |
reddit.subscribe(sub) | |
def replace_subreddits(credentials, subreddits): | |
''' | |
Replace the subreddits of the user at `credentials` with `subreddits` | |
:param credentials: username, password tuple | |
:param subreddits: set of subreddit names for subscribing to | |
''' | |
username, password = credentials | |
scope = {'identity', 'mysubreddits', 'subscribe'} | |
reddit = pso.connect(CLIENTID, CLIENTSECRET, username, password, oauth_scopes=scope, useragent=USERAGENT) | |
if reddit is None: | |
auth_exit(username) | |
current_subs = {s.display_name for s in reddit.get_my_subreddits(limit=None)} | |
for sub in current_subs.difference(subreddits): | |
reddit.unsubscribe(sub) | |
for sub in subreddits.difference(current_subs): | |
try: | |
reddit.subscribe(sub) | |
except praw.errors.Forbidden: | |
print('Permission denied for subreddit: {}'.format(sub)) | |
def auth_exit(username): | |
print('{}[*]{}'.format('\x1b[91m', '\x1b[0m'), end=' ') | |
print('Error authenticating /u/{}'.format(username)) | |
print('Try again and make sure the user has been added to the app as a developer.') | |
exit() | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(prog='SubCopy') | |
parser.description = 'Gather subreddit subscriptions of `from_user` and merge them with the subscriptions of `to_user`. '\ | |
'Use the --replace argument if you want to overwrite the subreddits of `to_user`.' | |
parser.add_argument('from_user', help='Gather subreddits from this user') | |
parser.add_argument('to_user', help='Copy subreddits to this user') | |
parser.add_argument('-R', '--replace', action='store_true', help='Replace all of `to_user`\'s subreddits') | |
args = parser.parse_args() | |
from_credentials = (args.from_user, getpass('Password for {}: '.format(args.from_user))) | |
to_credentials = (args.to_user, getpass('Password for {}: '.format(args.to_user))) | |
if args.replace: | |
print('{}[*]{}'.format('\x1b[93m', '\x1b[0m'), end=' ') | |
print('This will replace all of /u/{}\'s subreddits with those of /u/{}'.format(args.to_user, args.from_user)) | |
while (1): | |
choice = input('Are you sure you want to do this? [y/n] ').lower() | |
if choice not in ('y', 'n'): | |
print('Invalid option') | |
else: | |
break | |
if choice == 'y': | |
subscribe_to = get_subreddits(from_credentials) | |
replace_subreddits(to_credentials, subscribe_to) | |
else: | |
print('Quitting...') | |
exit() | |
else: | |
print('{}[*]{}'.format('\x1b[93m', '\x1b[0m'), end=' ') | |
print('Merging subreddits of /u/{} with /u/{}\'s subreddits...'.format(args.from_user, args.to_user)) | |
subscribe_to = get_subreddits(from_credentials) | |
merge_subreddits(to_credentials, subscribe_to) | |
print('{}[*]{} Done!'.format('\x1b[92m', '\x1b[0m')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment