now using all api 1.1 methods

This commit is contained in:
James Turk 2009-05-15 22:51:07 -04:00
parent 828b028469
commit e5adfb11ac

View File

@ -5,7 +5,7 @@ the Disqus comment system.
""" """
__author__ = "James Turk (james.p.turk@gmail.com)" __author__ = "James Turk (james.p.turk@gmail.com)"
__version__ = "0.0.1" __version__ = "0.0.2"
__copyright__ = "Copyright (c) 2009 James Turk" __copyright__ = "Copyright (c) 2009 James Turk"
__license__ = "BSD" __license__ = "BSD"
@ -25,6 +25,7 @@ class DisqusError(Exception):
''' base class for Disqus API errors ''' ''' base class for Disqus API errors '''
def apicall(method, params, http_method='GET'): def apicall(method, params, http_method='GET'):
params['api_version'] = 1.1
params = urllib.urlencode(params) params = urllib.urlencode(params)
if http_method == 'GET': if http_method == 'GET':
url = '?'.join((api_url % method, params)) url = '?'.join((api_url % method, params))
@ -36,15 +37,32 @@ def apicall(method, params, http_method='GET'):
raise DisqusError(obj['message']) raise DisqusError(obj['message'])
return obj['message'] return obj['message']
class Thread(object): class Post(object):
def __init__(self, d, forum_api_key): def __init__(self, d, user_api_key):
for k,v in d.iteritems(): for k,v in d.iteritems():
setattr(self, k, v) setattr(self, k, v)
self.user_api_key = user_api_key
def moderate(self, action):
params = {'user_api_key':self.user_api_key, 'post_id':self.id,
'action':action}
apicall('moderate_post', params, 'POST')
class Thread(object):
def __init__(self, d, user_api_key, forum_api_key):
for k,v in d.iteritems():
setattr(self, k, v)
self.user_api_key = user_api_key
self.forum_api_key = forum_api_key self.forum_api_key = forum_api_key
def get_posts(self): def get_posts(self, limit=25, start=0, filter=None, exclude=None):
return apicall('get_thread_posts', {'forum_api_key': self.forum_api_key, params = {'user_api_key':self.user_api_key, 'thread_id':self.id,
'thread_id': self.id}) 'limit':limit, 'start':start}
if filter:
params['filter'] = filter
if exclude:
params['exclude'] = exclude
return [Post(p, self.user_api_key) for p in apicall('get_thread_posts', params)]
def update(self, title=None, slug=None, url=None, allow_comments=None): def update(self, title=None, slug=None, url=None, allow_comments=None):
params = {'forum_api_key': self.forum_api_key, 'thread_id': self.id} params = {'forum_api_key': self.forum_api_key, 'thread_id': self.id}
@ -59,7 +77,8 @@ class Thread(object):
return apicall('update_thread', params, 'POST') return apicall('update_thread', params, 'POST')
def create_post(self, message, author_name, author_email, parent_post=None, def create_post(self, message, author_name, author_email, parent_post=None,
created_at=None, author_url=None, ip_address=None): created_at=None, author_url=None, ip_address=None,
state=None):
params = {'forum_api_key': self.forum_api_key, 'thread_id':self.id, params = {'forum_api_key': self.forum_api_key, 'thread_id':self.id,
'message':message, 'author_name':author_name, 'message':message, 'author_name':author_name,
'author_email':author_email} 'author_email':author_email}
@ -73,11 +92,13 @@ class Thread(object):
params['author_url'] = author_url params['author_url'] = author_url
if ip_address: if ip_address:
params['ip_address'] = ip_address params['ip_address'] = ip_address
if state:
params['state'] = state
return apicall('create_post', params, 'POST') return apicall('create_post', params, 'POST')
class Forum(object): class Forum(object):
def __init__(self, forum_api_key=None, id=None, name=None, shortname=None, def __init__(self, forum_api_key=None, id=None, name=None, shortname=None,
created_at=None, user_api_key=None): created_at=None, description=None, user_api_key=None):
self.__dict__['api_key'] = forum_api_key self.__dict__['api_key'] = forum_api_key
self.id = id self.id = id
self.name = name self.name = name
@ -93,22 +114,38 @@ class Forum(object):
self.__dict__['api_key'] = msg self.__dict__['api_key'] = msg
return self.__dict__['api_key'] return self.__dict__['api_key']
def get_posts(self, limit=25, start=0, filter=None, exclude=None):
params = {'user_api_key':self.user_api_key, 'forum_id':self.id,
'limit':limit, 'start':start}
if filter:
params['filter'] = filter
if exclude:
params['exclude'] = exclude
return [Post(p, self.user_api_key) for p in apicall('get_forum_posts', params)]
def get_threads(self): def get_threads(self):
msg = apicall('get_thread_list', {'forum_api_key':self.api_key}) msg = apicall('get_thread_list', {'user_api_key':self.user_api_key, 'forum_id':self.id})
return [Thread(t, self.api_key) for t in msg] return [Thread(t, self.user_api_key, self.api_key) for t in msg]
def get_num_posts(self, *thread_ids): def get_num_posts(self, *thread_ids):
params = {'forum_api_key': self.api_key, 'thread_ids': ','.join(thread_ids)} params = {'user_api_key': self.user_api_key, 'thread_ids': ','.join(thread_ids)}
return apicall('get_num_posts', params) return apicall('get_num_posts', params)
def get_updated_threads(self, since):
params = {'user_api_key': self.user_api_key, 'forum_id': self.id, 'since':since}
return apicall('get_updated_threads', params)
def get_thread_by_url(self, url): def get_thread_by_url(self, url):
msg = apicall('get_thread_by_url', {'forum_api_key': self.api_key, 'url': url}) msg = apicall('get_thread_by_url', {'forum_api_key': self.api_key, 'url': url})
if msg: if msg:
return Thread(msg, self.api_key) return Thread(msg, self.user_api_key, self.api_key)
def thread_by_identifier(self, title, identifier): def thread_by_identifier(self, title, identifier):
msg = apicall('thread_by_identifier', {'forum_api_key': self.api_key, 'title': title, 'identifier': identifier}, 'POST') msg = apicall('thread_by_identifier', {'forum_api_key': self.api_key, 'title': title, 'identifier': identifier}, 'POST')
return Thread(msg['thread'], self.api_key), msg['created'] return Thread(msg['thread'], self.user_api_key, self.api_key), msg['created']
def get_user_name(user_api_key):
return apicall('get_user_name', {'user_api_key':user_api_key}, 'POST')
def get_forums(user_api_key): def get_forums(user_api_key):
msg = apicall('get_forum_list', {'user_api_key':user_api_key}) msg = apicall('get_forum_list', {'user_api_key':user_api_key})