mirror of
https://github.com/kennethreitz-archive/python-convore.git
synced 2026-06-05 23:40:18 +00:00
initial commit of newly added files
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
'''
|
||||
Copyringt 2011 Kenichi Sato <ksato9700 AT gmail.com>
|
||||
'''
|
||||
|
||||
import urllib2
|
||||
import json
|
||||
|
||||
from group import ConvoreGroup
|
||||
from topic import ConvoreTopic
|
||||
from message import ConvoreMessage
|
||||
from live import ConvoreLiveMessage
|
||||
|
||||
URL_PREFIX = "https://convore.com/api/"
|
||||
debug=0
|
||||
|
||||
class ConvoreError(Exception):
|
||||
pass
|
||||
|
||||
class ConvoreAuthError(ConvoreError):
|
||||
pass
|
||||
|
||||
class ConvoreClient(object):
|
||||
|
||||
def __init__(self, username, password):
|
||||
auth_handler = urllib2.HTTPBasicAuthHandler()
|
||||
auth_handler.add_password(realm='Convore',
|
||||
uri=URL_PREFIX,
|
||||
user=username,
|
||||
passwd=password)
|
||||
self.openr = urllib2.build_opener(auth_handler,
|
||||
urllib2.HTTPCookieProcessor(),
|
||||
urllib2.HTTPSHandler(debug))
|
||||
|
||||
def _make_request(self, command, data=None, headers={}):
|
||||
url = URL_PREFIX + command
|
||||
try:
|
||||
return self.openr.open(urllib2.Request(url = URL_PREFIX + command,
|
||||
data=data,
|
||||
headers=headers))
|
||||
except urllib2.HTTPError as e:
|
||||
if e.code == 401:
|
||||
raise ConvoreAuthError()
|
||||
else:
|
||||
raise
|
||||
|
||||
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 = {}
|
||||
if group_id:
|
||||
data['group_id'] = group_id
|
||||
if cursor:
|
||||
data['cursor'] = cursor
|
||||
if topic_id:
|
||||
data['topic_id'] = topic_id
|
||||
response = self._make_request(command="live")
|
||||
messages = []
|
||||
for m in json.load(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]
|
||||
client = ConvoreClient(username, password)
|
||||
#client.verify()
|
||||
#for group in client.groups():
|
||||
# print group
|
||||
|
||||
group = client.group_by_id(group_id=3011)
|
||||
print group
|
||||
for topic in client.topics(group):
|
||||
print topic
|
||||
|
||||
topic = client.topic_by_id(5068)
|
||||
for message in client.messages(topic):
|
||||
print message
|
||||
|
||||
#for message in client.live():
|
||||
# print message
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
'''
|
||||
Copyringt 2011 Kenichi Sato <ksato9700 AT gmail.com>
|
||||
'''
|
||||
|
||||
from user import ConvoreUser
|
||||
from datetime import datetime
|
||||
|
||||
class ConvoreGroup(object):
|
||||
def __init__(self, g):
|
||||
self.members_count = g['members_count']
|
||||
self.name = g['name']
|
||||
self.creator = ConvoreUser(g['creator'])
|
||||
self.url = g['url']
|
||||
self.slug = g['slug']
|
||||
self.date_latest_message = datetime.utcfromtimestamp(g['date_latest_message'])
|
||||
self.date_created = datetime.utcfromtimestamp(g['date_created'])
|
||||
self.topics_count = g['topics_count'] if 'topics_count' in g else None
|
||||
self.unread = g['unread'] if 'unread' in g else None
|
||||
self.id = g['id']
|
||||
|
||||
def __str__(self):
|
||||
return ",".join(map(str,(
|
||||
self.members_count,
|
||||
self.name,
|
||||
"(%s)" % self.creator,
|
||||
self.url,
|
||||
self.slug,
|
||||
self.date_latest_message,
|
||||
self.date_created,
|
||||
self.topics_count,
|
||||
self.unread,
|
||||
self.id,
|
||||
)))
|
||||
|
||||
if __name__ == "__main__":
|
||||
g = {"date_latest_message": 1297661354.6135991,
|
||||
"topics_count": 16,
|
||||
"members_count": 643,
|
||||
"name": "Python",
|
||||
"creator": {"username": "fdrake",
|
||||
"url": "/users/fdrake/",
|
||||
"id": "886",
|
||||
"img": "https://convore2.s3.amazonaws.com/userpics/886/1297277206.jpg"},
|
||||
"url": "/python/",
|
||||
"date_created": 1297306311.4270909,
|
||||
"unread": 0,
|
||||
"id": "292",
|
||||
"slug": "python"
|
||||
}
|
||||
|
||||
group = ConvoreGroup(g)
|
||||
print group
|
||||
@@ -0,0 +1,39 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
'''
|
||||
Copyringt 2011 Kenichi Sato <ksato9700 AT gmail.com>
|
||||
'''
|
||||
|
||||
from user import ConvoreUser
|
||||
from datetime import datetime
|
||||
|
||||
class ConvoreLiveMessage(object):
|
||||
def __init__(self, m):
|
||||
self.kind = m['kind']
|
||||
self.group_ids = m['group_ids']
|
||||
self.user = ConvoreUser(m['user'])
|
||||
self._ts = datetime.utcfromtimestamp(m['_ts'])
|
||||
self._id = m['_id']
|
||||
|
||||
def __str__(self):
|
||||
return ",".join(map(str,(
|
||||
self.kind,
|
||||
self.group_ids,
|
||||
"(%s)" % self.user,
|
||||
self._ts,
|
||||
self._id,
|
||||
)))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
m = {"kind": "logout",
|
||||
"group_ids": [292],
|
||||
"user": {"username": "ksato9700",
|
||||
"url": "/users/ksato9700/",
|
||||
"id": 8849,
|
||||
"img": "https://convore2.s3.amazonaws.com/userpics/8849/1297620465.jpg"},
|
||||
"_ts": 1297638926.453522,
|
||||
"_id": "2415aa7c-37c7-11e0-a4f2-4040006c4e80"
|
||||
}
|
||||
|
||||
message = ConvoreLiveMessage(m)
|
||||
print message
|
||||
@@ -0,0 +1,22 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
'''
|
||||
Copyringt 2011 Kenichi Sato <ksato9700 AT gmail.com>
|
||||
'''
|
||||
|
||||
from user import ConvoreUser
|
||||
from datetime import datetime
|
||||
|
||||
class ConvoreMessage(object):
|
||||
def __init__(self, m):
|
||||
self.date_created = datetime.utcfromtimestamp(m['date_created'])
|
||||
self.message = m['message']
|
||||
self.user = ConvoreUser(m['user'])
|
||||
self.id = m['id']
|
||||
|
||||
def __str__(self):
|
||||
return ",".join(map(str,(
|
||||
self.date_created,
|
||||
self.message,
|
||||
"(%s)" % self.user,
|
||||
self.id,
|
||||
)))
|
||||
@@ -0,0 +1,32 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
'''
|
||||
Copyringt 2011 Kenichi Sato <ksato9700 AT gmail.com>
|
||||
'''
|
||||
|
||||
from user import ConvoreUser
|
||||
from datetime import datetime
|
||||
|
||||
class ConvoreTopic(object):
|
||||
def __init__(self, t):
|
||||
self.name = t['name']
|
||||
self.creator = ConvoreUser(t['creator'])
|
||||
self.url = t['url']
|
||||
self.slug = t['slug']
|
||||
self.date_latest_message = datetime.utcfromtimestamp(t['date_latest_message'])
|
||||
self.date_created = datetime.utcfromtimestamp(t['date_created'])
|
||||
self.message_count = t['message_count'] if 'message_count' in t else None
|
||||
self.unread = t['unread'] if 'unread' in t else None
|
||||
self.id = t['id']
|
||||
|
||||
def __str__(self):
|
||||
return ",".join(map(str,(
|
||||
self.name,
|
||||
"(%s)" % self.creator,
|
||||
self.url,
|
||||
self.slug,
|
||||
self.date_latest_message,
|
||||
self.date_created,
|
||||
self.message_count,
|
||||
self.unread,
|
||||
self.id,
|
||||
)))
|
||||
@@ -0,0 +1,20 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Copyringt 2011 Kenichi Sato <ksato9700 AT gmail.com>
|
||||
"""
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
class ConvoreUser(object):
|
||||
def __init__(self, u):
|
||||
self.username = u['username']
|
||||
self.url = u['url']
|
||||
self.id = u['id']
|
||||
self.img = u['img']
|
||||
|
||||
def __str__ (self):
|
||||
return ",".join(map(str,(
|
||||
self.username,
|
||||
self.url,
|
||||
self.id,
|
||||
self.img)))
|
||||
Reference in New Issue
Block a user