Created
August 10, 2022 15:43
-
-
Save flxai/07a8f0e63ad0d73d53e1a30c600f3e71 to your computer and use it in GitHub Desktop.
Reddit list Subreddits
This file contains 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 | |
# Download a list of subscribed subreddits | |
# Since this program uses stderr incoke it like this to store subs to file: | |
# | |
# reddit-list-subs > subs.lst | |
import getpass | |
import praw | |
import sys | |
from prawcore.exceptions import OAuthException | |
def eprint(*args, **kwargs): | |
print(*args, file=sys.stderr, **kwargs) | |
def einput(prompt=None): | |
if prompt: | |
sys.stderr.write(str(prompt)) | |
return input() | |
def reddit_auth(client_id, client_secret, username, password): | |
reddit = praw.Reddit( | |
client_id=client_id, | |
client_secret=client_secret, | |
user_agent="list_subs", | |
username=username, | |
password=password, | |
) | |
return reddit | |
def reddit_subs(reddit): | |
subs = sorted([sub.display_name for sub in reddit.user.subreddits(limit=None)], key=lambda v: v.upper()) | |
return subs | |
if __name__ == '__main__': | |
# Adapt these two values, following this guide: | |
# https://praw.readthedocs.io/en/stable/getting_started/configuration/options.html#oauth-configuration-options | |
client_id = 'YOU_HAVE_TO_ADAPT_THIS_GET_IT_FROM_THE_WEBSITE_ABOVE' | |
client_secret = 'YOU_HAVE_TO_ADAPT_THIS_GET_IT_FROM_THE_WEBSITE_ABOVE' | |
# Ask for credentials on stderr | |
eprint("Enter Reddit credentials for processing...") | |
username = einput("Username: ") | |
password = getpass.getpass("Password: ", sys.stderr) | |
reddit = reddit_auth(client_id, client_secret, username, password) | |
try: | |
subs = reddit_subs(reddit) | |
except: | |
eprint("Wrong credentials!") | |
sys.exit(1) | |
for sub in subs: | |
print(sub) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment