Skip to content

Instantly share code, notes, and snippets.

@aperson
Created November 16, 2011 15:07
Show Gist options
  • Save aperson/1370280 to your computer and use it in GitHub Desktop.
Save aperson/1370280 to your computer and use it in GitHub Desktop.
will approve all submissions in a subreddit
#/usr/bin/env python3
import json, signal, sys, time
import urllib.request
from urllib.parse import urlencode
import http.cookiejar
def sigint_handler(signal, frame):
'''Handles ^c'''
print('Recieved SIGINT! Exiting...')
print('Ran for {} seconds!'.format(time.time() - started))
sys.exit(0)
cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
def login(username, password):
'''Logs into reddit. Returns the modhash.'''
body = {'user' : username, 'passwd' : password, 'api_type' : 'json'}
body = urlencode(body).encode('utf=8')
with opener.open('https://www.reddit.com/api/login', body) as w:
modhash = json.loads(w.read().decode('utf-8'))['json']['data']['modhash']
return modhash
def approve_all(subreddit, approved=None):
'''Approves all the submissions on a subreddit's reported page.'''
subreddit = subreddit
if approved:
approved = approved
else:
approved = 0
with opener.open(
'http://www.reddit.com/r/{}/about/reports/.json?count=100'.format(subreddit)) as w:
cont = json.loads(w.read().decode('utf-8'))['data']
if cont['children']:
for i in cont['children']:
body = {'id' : i['data']['name'], 'r' : subreddit, 'uh' : MODHASH}
body = urlencode(body).encode('utf-8')
try:
with opener.open('http://www.reddit.com/api/approve', body) as w:
print('Successfully approved {}'.format(i['data']['name']))
approved += 1
except urllib.error.HTTPError:
print('Failed to approve {}'.format(i['data']['name']))
time.sleep(2)
approve_all(subreddit, approved)
else:
print('Approved {} posts in {}.'.format(approved, subreddit))
if __name__ == '__main__':
signal.signal(signal.SIGINT, sigint_handler)
args = sys.argv[1:]
if len(args) >= 3:
started = time.time()
MODHASH = login(args[0], args[1])
for i in args[2:]:
approve_all(i)
print('Ran for {} seconds!'.format(time.time() - started))
else:
print('Incorrect amount of arguments!\nNeeds username password subreddit [subreddit,]')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment