Skip to content

Instantly share code, notes, and snippets.

@ciccarone
Created May 4, 2019 20:41
Show Gist options
  • Save ciccarone/7ca0eb82d3fdc19097e996a562634659 to your computer and use it in GitHub Desktop.
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
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