Created
May 5, 2013 14:26
-
-
Save lospheris/5520957 to your computer and use it in GitHub Desktop.
My attempt at working with the Reddit API
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 requests | |
import time | |
import json | |
import urllib | |
import os | |
class RedditCrypto: | |
def __init__(self): | |
self.username = "" | |
self.password = "" | |
self.subReddit = "" | |
self.message = None | |
self.key = None | |
self.client = requests.session() | |
self.debugWindow = None | |
self.captcha = None | |
self.iden = None | |
self.sessionCookie = None | |
self.loggedIn = False | |
# Setters | |
def setCredentials(self, username, password): | |
self.username = str(username) | |
self.password = str(password) | |
def setDebugWindow(self, debugWindow): | |
self.debugWindow = debugWindow | |
def setCaptchaCall(self, captcha): | |
self.captcha = captcha | |
# Action functions | |
def login(self, userName='', passWord=''): | |
print 'Attempting login!' | |
if userName: | |
self.username = str(userName) | |
if passWord: | |
self.password = str(passWord) | |
if not self.username or not self.password: | |
print 'Please enter a username or password' | |
return 1 | |
if self.debugWindow: | |
self.debugWindow.append(self.password) | |
userData = {'user': self.username, 'passwd': self.password, 'api_type': 'json',} | |
headers = {'user-agent': 'Reddit Crypto V0.1',} | |
self.client.headers = headers | |
reply = self.client.post('http://www.reddit.com/api/login', data=userData) | |
jData = json.loads(reply.text) | |
if jData['json']['errors']: | |
print 'Something has gone wrong' | |
print jData['json']['errors'][0][0] | |
return 1 | |
else: | |
self.client.modhash = jData['json']['data']['modhash'] | |
self.client.user = self.username | |
self.sessionCookie = {'reddit_session': jData['json']['data']['cookie']} | |
print 'Login Successful for account ' + self.username + '.' | |
self.loggedIn = True | |
return 0 | |
# Post! | |
def post(self, message, subReddit, response): | |
self.message = str(message) | |
self.subReddit = str(subReddit) | |
captcha = str(response) | |
url = 'http://www.reddit.com/api/submit.json' | |
params = None | |
if self.iden: | |
params = { 'uh': str(self.client.modhash), | |
'title': 'OMG POST', | |
'captcha': captcha, | |
'kind': 'self', | |
'iden': str(self.iden), | |
'sr': self.subReddit, | |
'text': self.message | |
} | |
print 'Post Captcha' | |
print params | |
else: | |
params = { 'kind': 'self', | |
'sr': self.subReddit, | |
'text': self.message, | |
'title': 'OMG POST', | |
'uh': self.client.modhash, | |
} | |
reply = self.client.post(url, params=params, cookies=self.sessionCookie) | |
jData = None | |
try: | |
jData = json.loads(reply.text) | |
except ValueError: | |
print 'The reply from the server wasn\'t right so I don\'t know what to do with it.' | |
print reply | |
print reply.text | |
return 1 | |
except: | |
print 'Something I don\'t know how to handle happened.' | |
print reply | |
print reply.text | |
return 1 | |
if 'error' in jData: | |
print 'Server has responded with an error' | |
print jData['error'] | |
elif 'BAD_CAPTCHA' in jData['jquery'][12][3][0]: | |
print 'Captcha required!' | |
reply = self.client.post('http://www.reddit.com/api/new_captcha.json') | |
jData = json.loads(reply.text) | |
self.iden = jData['jquery'][11][3][0] | |
url = 'http://www.reddit.com/captcha/' + self.iden + '.png' | |
if self.captcha: | |
captchaReply = urllib.urlopen(url) | |
captchaData = captchaReply.read() | |
myCaptcha = open(self.iden + '.png', 'wb') | |
myCaptcha.write(captchaData) | |
myCaptcha.close() | |
self.captcha(os.getcwd() + '/' + self.iden + '.png') | |
os.remove(os.getcwd() + '/' + self.iden + '.png') | |
else: | |
print url | |
response = raw_input('Captcha: ') | |
return self.post(self.message, self.subReddit, response) | |
else: | |
print 'Post successful!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment