mirror of
https://github.com/nikdoof/test-auth.git
synced 2025-12-14 14:52:15 +00:00
Added to Inbox, allows sending messages
This commit is contained in:
@@ -3,6 +3,9 @@ import urllib2
|
||||
import urllib
|
||||
from datetime import datetime
|
||||
|
||||
class NotLoggedIn(Exception):
|
||||
pass
|
||||
|
||||
class Message(dict):
|
||||
""" Reddit Message """
|
||||
|
||||
@@ -26,13 +29,17 @@ class Inbox():
|
||||
REDDIT = "http://www.reddit.com"
|
||||
REDDIT_API_LOGIN = "%s/api/login" % REDDIT
|
||||
REDDIT_API_INBOX = "%s/message/inbox/.json?mark=false" % REDDIT
|
||||
REDDIT_API_COMPOSE = "%s/api/compose/" % REDDIT
|
||||
|
||||
def __init__(self, username, password):
|
||||
def __init__(self, username=None, password=None):
|
||||
|
||||
self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
|
||||
urllib2.install_opener(self.opener)
|
||||
|
||||
if username and password:
|
||||
self.login(username, password)
|
||||
|
||||
def login(self, username, password):
|
||||
data = { 'user': username,
|
||||
'passwd': password,
|
||||
'api_type': 'json' }
|
||||
@@ -43,6 +50,11 @@ class Inbox():
|
||||
jsondoc = json.load(self.opener.open(req))
|
||||
|
||||
self.login_cookie = jsondoc['json']['data']['cookie']
|
||||
self.modhash = jsondoc['json']['data']['modhash']
|
||||
if self.login_cookie:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
@property
|
||||
def _inbox_data(self):
|
||||
@@ -56,6 +68,10 @@ class Inbox():
|
||||
|
||||
return self.__inbox_cache
|
||||
|
||||
def _url_request(self, url, data):
|
||||
req = urllib2.Request( url, urllib.urlencode(data))
|
||||
return self.opener.open(req)
|
||||
|
||||
def __len__(self):
|
||||
return len(self._inbox_data)
|
||||
|
||||
@@ -65,3 +81,18 @@ class Inbox():
|
||||
def __iter__(self):
|
||||
return self._inbox_data.__iter__()
|
||||
|
||||
def send(self, to, subject, text):
|
||||
if not self.login_cookie:
|
||||
raise NotLoggedIn
|
||||
|
||||
print self.login_cookie
|
||||
|
||||
data = { 'to': to,
|
||||
'subject': subject,
|
||||
'text': 'text',
|
||||
'uh': self.modhash,
|
||||
'thing_id': '' }
|
||||
url = "%s" % (self.REDDIT_API_COMPOSE)
|
||||
|
||||
jsondoc = json.load(self._url_request(url, data))
|
||||
print jsondoc
|
||||
|
||||
Reference in New Issue
Block a user