From 44221c4c9ef30532f40d45e221fdb939bc05a4b2 Mon Sep 17 00:00:00 2001 From: Christophe Benz Date: Sun, 16 May 2010 02:18:19 +0200 Subject: [PATCH] [wip] work on chatoob frontend --- scripts/chatoob | 23 ++++++++++++ weboob/backends/aum/backend.py | 3 ++ weboob/backends/aum/browser.py | 11 +++++- weboob/frontends/chatoob/__init__.py | 18 +++++++++ weboob/frontends/chatoob/chatoob.py | 56 ++++++++++++++++++++++++++++ 5 files changed, 109 insertions(+), 2 deletions(-) create mode 100755 scripts/chatoob create mode 100644 weboob/frontends/chatoob/__init__.py create mode 100644 weboob/frontends/chatoob/chatoob.py diff --git a/scripts/chatoob b/scripts/chatoob new file mode 100755 index 00000000..ba9b94f4 --- /dev/null +++ b/scripts/chatoob @@ -0,0 +1,23 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# vim: ft=python et softtabstop=4 cinoptions=4 shiftwidth=4 ts=4 ai + +# Copyright(C) 2010 Christophe Benz +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, version 3 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +from weboob.frontends.chatoob import Chatoob + +if __name__ == '__main__': + Chatoob.run() diff --git a/weboob/backends/aum/backend.py b/weboob/backends/aum/backend.py index a4b62eb6..902baab5 100644 --- a/weboob/backends/aum/backend.py +++ b/weboob/backends/aum/backend.py @@ -139,3 +139,6 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesReply, ICapDating, ICapC def send_chat_message(self, _id, message): return self.browser.send_chat_message(_id, message) + + #def start_chat_polling(self): + #self._profile_walker = ProfilesWalker(self.weboob.scheduler, self.storage, self.browser) diff --git a/weboob/backends/aum/browser.py b/weboob/backends/aum/browser.py index 9317a895..ca76b9fc 100644 --- a/weboob/backends/aum/browser.py +++ b/weboob/backends/aum/browser.py @@ -16,10 +16,12 @@ # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +import datetime import time from logging import warning import random import simplejson +import urllib from weboob.tools.browser import BaseBrowser from weboob.tools.parsers.html5libparser import Html5libParser @@ -292,5 +294,10 @@ class AdopteUnMec(BaseBrowser): 'Referer': 'http://www.adopteunmec.com/chat.php', 'Origin': 'http://www.adopteunmec.com', } - print url, data, headers - return self.post_request(url, data, headers) + request = self.request_class(url, urllib.urlencode(data), headers) + response = self.openurl(request).read() + try: + datetime.datetime.strptime(response, '%Y-%m-%d %H:%M:%S') + return True + except ValueError: + return False diff --git a/weboob/frontends/chatoob/__init__.py b/weboob/frontends/chatoob/__init__.py new file mode 100644 index 00000000..cfc4556a --- /dev/null +++ b/weboob/frontends/chatoob/__init__.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2010 Christophe Benz +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, version 3 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +from .chatoob import Chatoob diff --git a/weboob/frontends/chatoob/chatoob.py b/weboob/frontends/chatoob/chatoob.py new file mode 100644 index 00000000..9828c3da --- /dev/null +++ b/weboob/frontends/chatoob/chatoob.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2010 Christophe Benz +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, version 3 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + +import logging + +from weboob.tools.application import ConsoleApplication +from weboob.capabilities.chat import ICapChat + + +__all__ = ['Chatoob'] + + +class Chatoob(ConsoleApplication): + APPNAME = 'chatoob' + VERSION = '1.0' + COPYRIGHT = 'Copyright(C) 2010 Christophe Benz' + + def main(self, argv): + self.load_config() + self.load_backends(ICapChat) + #for backend, result in self.weboob.do('start_chat_polling', self.on_new_chat_message): + #logging.info(u'Polling chat messages for backend %s' % backend) + return self.process_command(*argv[1:]) + + def on_new_chat_message(self, message): + print 'on_new_chat_message: %s' % message + + @ConsoleApplication.command('exit program') + def command_exit(self): + self.weboob.want_stop() + + @ConsoleApplication.command('list online contacts') + def command_list(self): + for backend, contact in self.weboob.do('iter_chat_contacts', online=True, offline=False): + self.format(contact) + + @ConsoleApplication.command('send message to contact') + def command_send(self, _id, message): + for backend, result in self.weboob.do('send_chat_message', _id, message): + if not result: + logging.error(u'Failed to send message to contact id="%s" on backend "%s"' % (_id, backend.name))