Created
June 19, 2020 05:06
-
-
Save WTFox/7ff86c698a721d57bf51bd645d7deb74 to your computer and use it in GitHub Desktop.
Python script to automate reddit posts
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
import time | |
import praw | |
USERNAME = "" | |
PASSWORD = "" | |
CLIENT_ID = "" | |
CLIENT_SECRET = "" | |
TITLE = None | |
URL = None | |
SUBREDDITS = [ | |
# "python", | |
# "django", | |
# "webdev", | |
# "programming", | |
# "golang", | |
# "datascience", | |
] | |
class Reddit: | |
def __init__(self, config): | |
self.reddit = praw.Reddit( | |
client_id=config["CLIENT_ID"], | |
client_secret=config["CLIENT_SECRET"], | |
username=config["USERNAME"], | |
password=config["PASSWORD"], | |
user_agent="Script by u/wtfox", | |
) | |
def post(self, subreddit, title, url=None, text=""): | |
if url: | |
submission = self.reddit.subreddit(subreddit).submit(title=title, url=url) | |
else: | |
submission = self.reddit.subreddit(subreddit).submit( | |
title=title, selftext=text | |
) | |
return submission | |
def main(): | |
assert TITLE is not None | |
assert URL is not None | |
config = { | |
"CLIENT_ID": CLIENT_ID, | |
"CLIENT_SECRET": CLIENT_SECRET, | |
"USERNAME": USERNAME, | |
"PASSWORD": PASSWORD, | |
} | |
reddit = Reddit(config) | |
for subreddit in SUBREDDITS: | |
print(f"Posting to {subreddit}:\n{TITLE}\n{URL}\n\n") | |
sub = reddit.post(subreddit, title=TITLE, url=URL) | |
time.sleep(1) | |
if __name__ == "__main__": | |
main() |
It would be very helpful if you wrote some comments about what the blocks of code, the classes, the functions/methods and the loops do.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is a little confusing what values do i need to change