implement chatoob messages command

This commit is contained in:
Christophe Benz 2010-05-16 17:42:27 +02:00
commit a4a74eda4f
4 changed files with 40 additions and 5 deletions

View file

@ -137,6 +137,9 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesReply, ICapDating, ICapC
def iter_chat_contacts(self, online=True, offline=True): def iter_chat_contacts(self, online=True, offline=True):
return self.browser.iter_chat_contacts(online=online, offline=offline) return self.browser.iter_chat_contacts(online=online, offline=offline)
def iter_chat_messages(self, _id=None):
return self.browser.iter_chat_messages(_id)
def send_chat_message(self, _id, message): def send_chat_message(self, _id, message):
return self.browser.send_chat_message(_id, message) return self.browser.send_chat_message(_id, message)

View file

@ -38,7 +38,7 @@ from weboob.backends.aum.pages.login import LoginPage, RedirectPage, BanPage, Er
from weboob.backends.aum.pages.edit import EditPhotoPage, EditPhotoCbPage, EditAnnouncePage, EditDescriptionPage, EditSexPage, EditPersonalityPage from weboob.backends.aum.pages.edit import EditPhotoPage, EditPhotoCbPage, EditAnnouncePage, EditDescriptionPage, EditSexPage, EditPersonalityPage
from weboob.backends.aum.pages.wait import WaitPage from weboob.backends.aum.pages.wait import WaitPage
from weboob.capabilities.chat import Contact from weboob.capabilities.chat import ChatContact, ChatMessage
__all__ = ['AdopteUnMec'] __all__ = ['AdopteUnMec']
@ -271,11 +271,22 @@ class AdopteUnMec(BaseBrowser):
print 'isSlutOnline(%s) = %s' % (id, r) print 'isSlutOnline(%s) = %s' % (id, r)
return r return r
def iter_chat_contacts(self, online=True, offline=True): def _get_chat_infos(self):
json = simplejson.load(self.openurl('http://www.adopteunmec.com/1.1_cht_get.php?anticache=%f' % random.random())) json = simplejson.load(self.openurl('http://www.adopteunmec.com/1.1_cht_get.php?anticache=%f' % random.random()))
if json['error']: if json['error']:
raise ChatException(u'Error while getting chat infos. json:\n%s' % json) raise ChatException(u'Error while getting chat infos. json:\n%s' % json)
for contact in json['contacts']: return json
def iter_chat_contacts(self, online=True, offline=True):
def iter_dedupe(contacts):
yielded_ids = set()
for contact in contacts:
if contact['id'] not in yielded_ids:
yield contact
yielded_ids.add(contact['id'])
json = self._get_chat_infos()
for contact in iter_dedupe(json['contacts']):
if online and contact['cat'] == 1 or offline and contact['cat'] == 3: if online and contact['cat'] == 1 or offline and contact['cat'] == 3:
if contact['cat'] == 1: if contact['cat'] == 1:
online = True online = True
@ -283,7 +294,13 @@ class AdopteUnMec(BaseBrowser):
online = False online = False
else: else:
raise ChatException(u'Unknown online status: contact=%s' % contact) raise ChatException(u'Unknown online status: contact=%s' % contact)
yield Contact(_id=contact['id'], pseudo=contact['pseudo'], online=online, avatar_url=contact['cover'], age=contact['birthday']) yield ChatContact(_id=contact['id'], pseudo=contact['pseudo'], online=online, avatar_url=contact['cover'], age=contact['birthday'])
def iter_chat_messages(self, _id=None):
json = self._get_chat_infos()
if json['messages'] is not None:
for message in json['messages']:
yield ChatMessage(id_from=message['id_from'], id_to=message['id_to'], message=message['message'], date=message['date'])
def send_chat_message(self, _id, message): def send_chat_message(self, _id, message):
url = 'http://www.adopteunmec.com/1.1_cht_send.php?anticache=%f' % random.random() url = 'http://www.adopteunmec.com/1.1_cht_send.php?anticache=%f' % random.random()

View file

@ -16,6 +16,8 @@
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import datetime
from .cap import ICap from .cap import ICap
@ -26,7 +28,7 @@ class ChatException(Exception):
pass pass
class Contact(object): class ChatContact(object):
def __init__(self, _id, pseudo, online, name=None, avatar_url=None, age=None): def __init__(self, _id, pseudo, online, name=None, avatar_url=None, age=None):
self.id = _id self.id = _id
self.pseudo = pseudo self.pseudo = pseudo
@ -36,6 +38,14 @@ class Contact(object):
self.age = age self.age = age
class ChatMessage(object):
def __init__(self, id_from, id_to, message, date=None):
self.id_from = id_from
self.id_to = id_to
self.message = message
self.date = datetime.datetime.utcnow() if date is None else date
class ICapChat(ICap): class ICapChat(ICap):
def iter_chat_contacts(self, online=True, offline=True): def iter_chat_contacts(self, online=True, offline=True):
raise NotImplementedError() raise NotImplementedError()

View file

@ -48,6 +48,11 @@ class Chatoob(ConsoleApplication):
for backend, contact in self.weboob.do('iter_chat_contacts', online=True, offline=False): for backend, contact in self.weboob.do('iter_chat_contacts', online=True, offline=False):
self.format(contact) self.format(contact)
@ConsoleApplication.command('get messages')
def command_messages(self):
for backend, message in self.weboob.do('iter_chat_messages'):
self.format(message)
@ConsoleApplication.command('send message to contact') @ConsoleApplication.command('send message to contact')
def command_send(self, _id, message): def command_send(self, _id, message):
for backend, result in self.weboob.do('send_chat_message', _id, message): for backend, result in self.weboob.do('send_chat_message', _id, message):