Created
September 24, 2012 01:47
-
-
Save aperson/3773782 to your computer and use it in GitHub Desktop.
Quick script to remove ham from the modqueue en-mass
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
#/usr/bin/env python3 | |
import json | |
import urllib.request | |
import time | |
import re | |
import signal | |
import sys | |
from urllib.parse import urlencode | |
import http.cookiejar | |
try: | |
from settings import * | |
except: | |
USERNAME = 'this' | |
PASSWORD = 'shouldbe' | |
SUBREDDIT = 'obvious' # use 'mod' for all of them. DANGEROUS | |
def p(data, end='\n'): | |
print(time.strftime('\r\033[K\033[2K[\033[31m%y\033[39m/\033[31m%m\033[39m/\033[31m%d' | |
'\033[39m][\033[31m%H\033[39m:\033[31m%M\033[39m:\033[31m%S\033[39m] ') + data, end=end) | |
def sigint_handler(signal, frame): | |
'''Handles ^c''' | |
p('Recieved SIGINT! Exiting...') | |
sys.exit(0) | |
class Reddit(object): | |
"""Base class to perform the tasks of a redditor.""" | |
def __init__(self, username, password): | |
self.username = username | |
self.password = password | |
self.cj = http.cookiejar.CookieJar() | |
self.opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(self.cj)) | |
self.opener.addheaders = [('User-agent', 'remove-ham.py v1')] | |
self._login() | |
def _request(self, url, body=None): | |
if body is not None: | |
body = urlencode(body).encode('utf-8') | |
with self.opener.open(url, data=body) as w: | |
time.sleep(2) | |
return json.loads(w.read().decode('utf-8')) | |
def _login(self): | |
p("Logging in as {}.".format(self.username)) | |
body = {'user': self.username, 'passwd': self.password, 'api_type': 'json'} | |
resp = self._request('https://www.reddit.com/api/login', body) | |
self.modhash = resp['json']['data']['modhash'] | |
def post(self, url, body): | |
"""Sends a POST to the url and returns the json as a dict.""" | |
if 'api_type' not in body: | |
body['api_type'] = 'json' | |
body['uh'] = self.modhash | |
return self._request(url, body) | |
def get(self, url): | |
"""Sends a GET to the url and returns the json as a dict.""" | |
if '.json' not in url: | |
url += '.json' | |
return self._request(url) | |
def main(): | |
r = Reddit(USERNAME, PASSWORD) | |
removed_count = 0 | |
while True: | |
p('Retrieving modqueue...', end='') | |
things = r.get('http://reddit.com/r/{}/about/modqueue.json'.format(SUBREDDIT)) | |
things = things['data']['children'] | |
if things: | |
for thing in things: | |
thing = thing['data'] | |
p('Removing: {}'.format(thing['name'])) | |
body = {'r': thing['subreddit'], 'id': thing['name'], | |
'executed': 'remove', 'spam': 'false'} | |
r.post('http://www.reddit.com/api/remove', body) | |
removed_count += 1 | |
else: | |
break | |
p('Removed {} things from /r/{}'.format(removed_count, SUBREDDIT)) | |
if __name__ == '__main__': | |
signal.signal(signal.SIGINT, sigint_handler) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment