public
Created

My attempt at working with the Reddit API

  • Download Gist
RedditCrypto.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
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!'

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.