mirror of
https://github.com/kennethreitz-archive/python-convore.git
synced 2026-06-05 23:40:18 +00:00
introduced collection classes, moved methods to them and group and topic classes
This commit is contained in:
+55
-56
@@ -3,21 +3,22 @@
|
||||
Copyringt 2011 Kenichi Sato <ksato9700 AT gmail.com>
|
||||
'''
|
||||
|
||||
import urllib
|
||||
import urllib2
|
||||
import json
|
||||
|
||||
from group import ConvoreGroup
|
||||
from topic import ConvoreTopic
|
||||
from message import ConvoreMessage
|
||||
from group import ConvoreGroups
|
||||
from topic import ConvoreTopics
|
||||
from live import ConvoreLiveMessage
|
||||
|
||||
URL_PREFIX = "https://convore.com/api/"
|
||||
#debug=100
|
||||
debug=0
|
||||
|
||||
class ConvoreError(Exception):
|
||||
pass
|
||||
|
||||
class ConvoreAuthError(ConvoreError):
|
||||
class ConvoreAuthError(Exception):
|
||||
pass
|
||||
|
||||
class ConvoreClient(object):
|
||||
@@ -32,12 +33,25 @@ class ConvoreClient(object):
|
||||
urllib2.HTTPCookieProcessor(),
|
||||
urllib2.HTTPSHandler(debug))
|
||||
|
||||
def _make_request(self, command, data=None, headers={}):
|
||||
self.groups = ConvoreGroups(self)
|
||||
self.topics = ConvoreTopics(self)
|
||||
|
||||
def _make_request(self, command, params=None):
|
||||
url = URL_PREFIX + command
|
||||
if params:
|
||||
data = urllib.urlencode(params)
|
||||
else:
|
||||
data = None
|
||||
headers = {}
|
||||
try:
|
||||
return self.openr.open(urllib2.Request(url = URL_PREFIX + command,
|
||||
data=data,
|
||||
headers=headers))
|
||||
f = self.openr.open(urllib2.Request(url = URL_PREFIX + command,
|
||||
data=data,
|
||||
headers=headers))
|
||||
response = json.load(f)
|
||||
if 'error' in response:
|
||||
raise ConvoreError(response['error'])
|
||||
return response
|
||||
|
||||
except urllib2.HTTPError as e:
|
||||
if e.code == 401:
|
||||
raise ConvoreAuthError()
|
||||
@@ -47,27 +61,6 @@ class ConvoreClient(object):
|
||||
def verify(self):
|
||||
self._make_request(command="account/verify.json")
|
||||
|
||||
def groups(self):
|
||||
response = self._make_request(command="groups.json")
|
||||
groups = []
|
||||
for g in json.load(response)['groups']:
|
||||
groups.append(ConvoreGroup(g))
|
||||
return groups
|
||||
|
||||
def group_by_id(self, group_id):
|
||||
response = self._make_request(command="groups/%s.json" % group_id)
|
||||
return ConvoreGroup(json.load(response)['group'])
|
||||
|
||||
def topics(self, group):
|
||||
response = self._make_request(command="groups/%s/topics.json" % group.id)
|
||||
topics = []
|
||||
for t in json.load(response)['topics']:
|
||||
topics.append(ConvoreTopic(t))
|
||||
return topics
|
||||
|
||||
def topic_by_id(self, topic_id):
|
||||
response = self._make_request(command="topics/%s.json" % topic_id)
|
||||
return ConvoreTopic(json.load(response)['topic'])
|
||||
|
||||
def live(self, group_id=None, cursor=None, topic_id=None):
|
||||
data = {}
|
||||
@@ -79,44 +72,50 @@ class ConvoreClient(object):
|
||||
data['topic_id'] = topic_id
|
||||
response = self._make_request(command="live")
|
||||
messages = []
|
||||
for m in json.load(response)['messages']:
|
||||
for m in response['messages']:
|
||||
messages.append(ConvoreLiveMessage(m))
|
||||
return messages
|
||||
|
||||
|
||||
def messages(self, topic):
|
||||
response = self._make_request(command="topics/%s/messages.json" % topic.id)
|
||||
messages = []
|
||||
for m in json.load(response)['messages']:
|
||||
print m
|
||||
messages.append(ConvoreMessage(m))
|
||||
return messages
|
||||
|
||||
if __name__ == '__main__':
|
||||
import sys
|
||||
username, password = sys.argv[1:3]
|
||||
try:
|
||||
username, password = sys.argv[1:3]
|
||||
except:
|
||||
print "usage: python %s username password" % sys.argv[0]
|
||||
sys.exit(1)
|
||||
|
||||
client = ConvoreClient(username, password)
|
||||
#client.verify()
|
||||
|
||||
for group in client.groups():
|
||||
print group
|
||||
print repr(group)
|
||||
|
||||
#group_id = 3011
|
||||
group_id = group.id
|
||||
|
||||
group = client.group_by_id(group_id)
|
||||
for topic in client.topics(group):
|
||||
print topic
|
||||
print repr(topic)
|
||||
groups = client.groups()
|
||||
print groups
|
||||
|
||||
#topic_id = 5068
|
||||
topic_id = topic.id
|
||||
#group = client.groups.create(name="MyTestGroup", description="desc", slug="the group")
|
||||
#print repr(group)
|
||||
|
||||
topic = client.topic_by_id(topic_id)
|
||||
for message in client.messages(topic):
|
||||
print message
|
||||
print repr(message)
|
||||
# for group in groups:
|
||||
# if "test" in group.slug:
|
||||
# break
|
||||
|
||||
#print group
|
||||
# print repr(group)
|
||||
|
||||
# topic = group.topics.create(name="Topic One")
|
||||
# print repr(topic)
|
||||
|
||||
# topics = group.topics()
|
||||
# print topics
|
||||
|
||||
# topic = client.topics[topics[-1].id]
|
||||
# print topic
|
||||
# print repr(topic)
|
||||
|
||||
# topic.messages.create(message="Wow, I'm the first")
|
||||
# topic.messages.create(message="Hmm, I must be the second")
|
||||
|
||||
# messages = topic.messages()
|
||||
# print messages
|
||||
|
||||
#for message in client.live():
|
||||
# print message
|
||||
|
||||
+42
-4
@@ -4,10 +4,39 @@ Copyringt 2011 Kenichi Sato <ksato9700 AT gmail.com>
|
||||
'''
|
||||
|
||||
from user import ConvoreUser
|
||||
from topic import ConvoreTopics
|
||||
from datetime import datetime
|
||||
|
||||
class ConvoreGroups(object):
|
||||
def __init__(self, client):
|
||||
self.client = client
|
||||
|
||||
def __call__(self):
|
||||
response = self.client._make_request(command="groups.json")
|
||||
groups = []
|
||||
for g in response['groups']:
|
||||
group = ConvoreGroup(g, self.client)
|
||||
groups.append(group)
|
||||
return groups
|
||||
|
||||
def __getitem__(self, group_id):
|
||||
response = self.client._make_request(command="groups/%s.json" % group_id)
|
||||
return ConvoreGroup(response['group'], self.client)
|
||||
|
||||
def create(self, name, description=None, slug=None):
|
||||
params = {'name': name}
|
||||
if description:
|
||||
params['description'] = description
|
||||
if slug:
|
||||
params['slug'] = slug
|
||||
print params
|
||||
response = self.client._make_request(command="groups/create.json",
|
||||
params=params)
|
||||
return ConvoreGroup(g, self.client)
|
||||
|
||||
|
||||
class ConvoreGroup(object):
|
||||
def __init__(self, g):
|
||||
def __init__(self, g, client):
|
||||
self.members_count = g['members_count']
|
||||
self.name = g['name']
|
||||
self.creator = ConvoreUser(g['creator'])
|
||||
@@ -19,11 +48,20 @@ class ConvoreGroup(object):
|
||||
self.unread = g['unread'] if 'unread' in g else None
|
||||
self.id = g['id']
|
||||
|
||||
self.client = client
|
||||
self.topics = ConvoreTopics(self.client, self.id)
|
||||
|
||||
def leave(self):
|
||||
params = {'group_id': self.id}
|
||||
response = self.client._make_request(command="groups/%s/leave.json" % self.id,
|
||||
params=params)
|
||||
print response
|
||||
|
||||
def __str__(self):
|
||||
return "<ConvoreGroup id='%s'>" % self.id
|
||||
|
||||
def __repr__(self):
|
||||
return ",".join(map(str,(
|
||||
return "<ConvoreGroup " + ",".join(map(str,(
|
||||
self.members_count,
|
||||
self.name,
|
||||
self.creator,
|
||||
@@ -34,7 +72,7 @@ class ConvoreGroup(object):
|
||||
self.topics_count,
|
||||
self.unread,
|
||||
self.id,
|
||||
)))
|
||||
))) + ">"
|
||||
|
||||
if __name__ == "__main__":
|
||||
g = {"date_latest_message": 1297661354.6135991,
|
||||
@@ -52,6 +90,6 @@ if __name__ == "__main__":
|
||||
"slug": "python"
|
||||
}
|
||||
|
||||
group = ConvoreGroup(g)
|
||||
group = ConvoreGroup(g, None)
|
||||
print group
|
||||
print repr(group)
|
||||
|
||||
@@ -6,6 +6,28 @@ Copyringt 2011 Kenichi Sato <ksato9700 AT gmail.com>
|
||||
from user import ConvoreUser
|
||||
from datetime import datetime
|
||||
|
||||
class ConvoreMessages(object):
|
||||
def __init__(self, client, topic_id):
|
||||
self.client = client
|
||||
self.topic_id = topic_id
|
||||
|
||||
def __call__(self):
|
||||
response = self.client._make_request(command="topics/%s/messages.json" % self.topic_id)
|
||||
messages = []
|
||||
for m in response['messages']:
|
||||
message = ConvoreMessage(m)
|
||||
messages.append(message)
|
||||
return messages
|
||||
|
||||
def create(self, message):
|
||||
params = {
|
||||
'message': message,
|
||||
'topic_id': self.topic_id
|
||||
}
|
||||
response = self.client._make_request(command="topics/%s/messages/create.json" % self.topic_id,
|
||||
params=params)
|
||||
print response
|
||||
|
||||
class ConvoreMessage(object):
|
||||
def __init__(self, m):
|
||||
self.date_created = datetime.utcfromtimestamp(m['date_created'])
|
||||
|
||||
+36
-3
@@ -4,10 +4,40 @@ Copyringt 2011 Kenichi Sato <ksato9700 AT gmail.com>
|
||||
'''
|
||||
|
||||
from user import ConvoreUser
|
||||
from message import ConvoreMessages
|
||||
from datetime import datetime
|
||||
|
||||
class ConvoreTopics(object):
|
||||
def __init__(self, client, group_id=None):
|
||||
self.client = client
|
||||
self.group_id = group_id
|
||||
|
||||
def __call__(self):
|
||||
response = self.client._make_request(command="groups/%s/topics.json" % self.group_id)
|
||||
topics = []
|
||||
for t in response['topics']:
|
||||
topic = ConvoreTopic(t, self.client)
|
||||
topics.append(topic)
|
||||
return topics
|
||||
|
||||
def __getitem__(self, topic_id):
|
||||
response = self.client._make_request(command="topics/%s.json" % topic_id)
|
||||
return ConvoreTopic(response['topic'], self.client)
|
||||
|
||||
def create(self, name):
|
||||
if self.group_id == None:
|
||||
raise RuntimeError("topics should be created with group_id")
|
||||
|
||||
params = {
|
||||
'name': name,
|
||||
'group_id': self.group_id
|
||||
}
|
||||
response = self.client._make_request(command="groups/%s/topics/create.json" % self.group_id,
|
||||
params=params)
|
||||
return ConvoreTopic(response['topic'], self.client)
|
||||
|
||||
class ConvoreTopic(object):
|
||||
def __init__(self, t):
|
||||
def __init__(self, t, client):
|
||||
self.name = t['name']
|
||||
self.creator = ConvoreUser(t['creator'])
|
||||
self.url = t['url']
|
||||
@@ -18,11 +48,14 @@ class ConvoreTopic(object):
|
||||
self.unread = t['unread'] if 'unread' in t else None
|
||||
self.id = t['id']
|
||||
|
||||
self.client = client
|
||||
self.messages = ConvoreMessages(self.client, self.id)
|
||||
|
||||
def __str__(self):
|
||||
return "<ConvoreTopic id='%s'>" % self.id
|
||||
|
||||
def __repr__(self):
|
||||
return ",".join(map(str,(
|
||||
return "<ConvoreTopic " + ",".join(map(str,(
|
||||
self.name,
|
||||
self.creator,
|
||||
self.url,
|
||||
@@ -32,4 +65,4 @@ class ConvoreTopic(object):
|
||||
self.message_count,
|
||||
self.unread,
|
||||
self.id,
|
||||
)))
|
||||
))) + ">"
|
||||
|
||||
Reference in New Issue
Block a user