Skip to content

Instantly share code, notes, and snippets.

@WTFox
Created June 19, 2020 05:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WTFox/7ff86c698a721d57bf51bd645d7deb74 to your computer and use it in GitHub Desktop.
Save WTFox/7ff86c698a721d57bf51bd645d7deb74 to your computer and use it in GitHub Desktop.
Python script to automate reddit posts
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()
@CROHODLR
Copy link

this is a little confusing what values do i need to change

@Glort572
Copy link

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