Skip to content

@lospheris /

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
My attempt at working with the Reddit API
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:
userData = {'user': self.username, 'passwd': self.password, 'api_type': 'json',}
headers = {'user-agent': 'Reddit Crypto V0.1',}
self.client.headers = headers
reply ='', data=userData)
jData = json.loads(reply.text)
if jData['json']['errors']:
print 'Something has gone wrong'
print jData['json']['errors'][0][0]
return 1
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 = ''
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
params = { 'kind': 'self',
'sr': self.subReddit,
'text': self.message,
'title': 'OMG POST',
'uh': self.client.modhash,
reply =, params=params, cookies=self.sessionCookie)
jData = None
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
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 ='')
jData = json.loads(reply.text)
self.iden = jData['jquery'][11][3][0]
url = '' + self.iden + '.png'
if self.captcha:
captchaReply = urllib.urlopen(url)
captchaData =
myCaptcha = open(self.iden + '.png', 'wb')
self.captcha(os.getcwd() + '/' + self.iden + '.png')
os.remove(os.getcwd() + '/' + self.iden + '.png')
print url
response = raw_input('Captcha: ')
return, self.subReddit, response)
print 'Post successful!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.