Created
May 4, 2019 20:41
-
-
Save ciccarone/7ca0eb82d3fdc19097e996a562634659 to your computer and use it in GitHub Desktop.
Been learning some python, here is a bot that posts to reddit using praw
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
import praw | |
import re | |
import time | |
reddit = praw.Reddit( | |
client_id='', | |
client_secret='Y', | |
user_agent='<console:testbot:0.0.1>', | |
username='', | |
password='' | |
) | |
subreddits = ['testingground4bots'] | |
pos = 0 | |
errors = 0 | |
title = "Testing a post via bot" | |
url = "https://tonyciccarone.com" | |
def post(): | |
global subreddits | |
global pos | |
global errors | |
try: | |
subreddit = reddit.subreddit(subreddits[pos]) | |
subreddit.submit(title, url=url) | |
print("Posted to " + subreddits[pos]) | |
post = pos + 1 | |
if (pos <= len(subreddit - 1)): | |
post() | |
else: | |
print("Done") | |
except praw.exceptions.APIException as e: | |
if(e.error_type == "RATELIMIT"): | |
delay = re.search("(\d+) minutes", e.message) | |
if delay: | |
delay_seconds = float(int(delay.group(1)) * 60) | |
time.sleep(delay_seconds) | |
post() | |
else: | |
delay = re.search("(\d+) seconds", e.message) | |
delay_seconds = float(delay.group(1)) | |
time.sleep(delay_seconds) | |
post() | |
except: | |
errors = errors + 1 | |
if (errors > 5): | |
print("CRASH BURN") | |
print(e.message) | |
post() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment