From f3a017cb013529dbb116a882ebb84a65da6c7a9a Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Sun, 11 Apr 2010 11:48:59 +0200 Subject: [PATCH] [aum] ICapMessages.iter_[new_]messages() implemented --- weboob/backends/aum/backend.py | 42 ++++++++++++- weboob/backends/aum/mail.py | 69 --------------------- weboob/backends/aum/pages/contact_list.py | 2 - weboob/backends/aum/pages/contact_thread.py | 10 +-- weboob/backends/aum/pages/profile.py | 1 - weboob/backends/dlfp/backend.py | 19 +++--- weboob/capabilities/messages.py | 8 ++- weboob/ouiboube.py | 2 +- 8 files changed, 63 insertions(+), 90 deletions(-) delete mode 100644 weboob/backends/aum/mail.py diff --git a/weboob/backends/aum/backend.py b/weboob/backends/aum/backend.py index 389fc0e5..fffcecc8 100644 --- a/weboob/backends/aum/backend.py +++ b/weboob/backends/aum/backend.py @@ -21,6 +21,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. from weboob.backend import Backend from weboob.capabilities.messages import ICapMessages, ICapMessagesReply +from .adopte import AdopteUnMec + class AuMBackend(Backend, ICapMessages, ICapMessagesReply): NAME = 'aum' MAINTAINER = 'Romain Bignon' @@ -28,9 +30,45 @@ class AuMBackend(Backend, ICapMessages, ICapMessagesReply): VERSION = '1.0' LICENSE = 'GPLv3' DESCRIPTION = "French dating website" + CONFIG = {'username': Backend.ConfigField(description='Username on website'), + 'password': Backend.ConfigField(description='Password of account', is_masked=True), + } + _browser = None + + def __getattr__(self, name): + if name == 'browser': + if not self._browser: + self._browser = AdopteUnMec(self.config['username'], self.config['password']) + return self._browser + raise AttributeError, name def iter_messages(self, thread=None): - return dict().iteritems() + for message in self._iter_messages(thread, False): + yield message def iter_new_messages(self, thread=None): - return dict().iteritems() + for message in self._iter_messages(thread, True): + yield message + + def _iter_messages(self, thread, only_new): + if not only_new or self.browser.nb_new_mails(): + my_name = self.browser.get_my_name() + contacts = self.browser.get_contact_list() + contacts.reverse() + + for contact in contacts: + if only_new and not contact.is_new(): + continue + + mails = self.browser.get_thread_mails(contact.get_id()) + profile = None + for i in xrange(len(mails)): + mail = mails[i] + if only_new and mail.get_from() == my_name: + break + + if not profile: + profile = self.browser.get_profile(contact.get_id()) + mail.signature += u'\n%s' % profile.get_profile_text() + print mail.signature + yield mail diff --git a/weboob/backends/aum/mail.py b/weboob/backends/aum/mail.py deleted file mode 100644 index 86e2a022..00000000 --- a/weboob/backends/aum/mail.py +++ /dev/null @@ -1,69 +0,0 @@ -# -*- coding: utf-8 -*- - -""" -Copyright(C) 2008 Romain Bignon - -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 time -import datetime - -class Mail: - - def __init__(self, id, name): - self.id = id - self.reply_date = 0 - self.name = name - self.sender = name - self.profile_link = '' - - self.new = False - self.content = '' - self.date = datetime.datetime.utcnow() - - def get_date_int(self): - return int(time.strftime('%Y%m%d%H%M%S', self.get_date().timetuple())) - - def get_msg_id(self, sender): - return '<%s.%d@%s>' % (self.get_date_int(), self.id, sender) - - def get_reply_id(self, sender): - if self.reply_date: - return '<%s.%d@%s>' % (self.reply_date, self.id, sender) - else: - return '' - - def get_id(self): - return self.id - - def get_name(self): - return self.name - - def get_date(self): - return self.date - - def get_profile_link(self): - return self.profile_link - - def get_from(self): - return self.sender - - def get_content(self): - return self.content - - def is_new(self): - return self.new - diff --git a/weboob/backends/aum/pages/contact_list.py b/weboob/backends/aum/pages/contact_list.py index 3c0ad361..4ba0e9f3 100644 --- a/weboob/backends/aum/pages/contact_list.py +++ b/weboob/backends/aum/pages/contact_list.py @@ -97,9 +97,7 @@ class ContactItem: class ContactListPage(PageBase): - def loaded(self): - self.items = [] tags = self.document.getElementsByTagName('form')[0].childNodes[3].childNodes[1].childNodes diff --git a/weboob/backends/aum/pages/contact_thread.py b/weboob/backends/aum/pages/contact_thread.py index fa7a3cb0..4e1b4e18 100644 --- a/weboob/backends/aum/pages/contact_thread.py +++ b/weboob/backends/aum/pages/contact_thread.py @@ -26,9 +26,9 @@ from mechanize import FormNotFoundError from weboob.backends.aum.pages.base import PageBase from weboob.backends.aum.exceptions import AdopteCantPostMail -from weboob.backends.aum.mail import Mail +from weboob.capabilities.messages import Message -class MailParser(Mail): +class MailParser(Message): """ @@ -96,7 +96,7 @@ class MailParser(Mail): def __init__(self, id, name, tr): # implicit - Mail.__init__(self, id, name) + Message.__init__(self, id, 0, 'Discussion with %s' % name, name) self.tr = tr.childNodes[0].childNodes[1].childNodes[0].childNodes[0] tds = self.tr.childNodes @@ -135,8 +135,6 @@ class MailParser(Mail): self.parse_from() def parse_date(self, date_str): - - # To match regexp, we have to remove any return chars in string # before the status ('nouveau', 'lu', etc) date_str = u''.join(date_str.split(u'\n')) @@ -155,6 +153,7 @@ class MailParser(Mail): d = d.astimezone(tz.tzutc()) # and get timestamp self.date = d + self.id = self.get_date_int() if m.group(7).find('nouveau') >= 0: self.new = True @@ -174,6 +173,7 @@ class MailParser(Mail): if m: self.profile_link = m.group(1) + self.signature = u'Profile link: %s' % self.profile_link return warning('Unable to find the profile URL in the message %s@%s' % (self.get_from(), self.get_id())) diff --git a/weboob/backends/aum/pages/profile.py b/weboob/backends/aum/pages/profile.py index db89c122..a390a8a7 100644 --- a/weboob/backends/aum/pages/profile.py +++ b/weboob/backends/aum/pages/profile.py @@ -50,7 +50,6 @@ class FieldOld(FieldBase): def put_value(self, d, value): m = self.regexp.match(value) - warning(value) if not m: return diff --git a/weboob/backends/dlfp/backend.py b/weboob/backends/dlfp/backend.py index fb537d38..6e5c6fee 100644 --- a/weboob/backends/dlfp/backend.py +++ b/weboob/backends/dlfp/backend.py @@ -47,28 +47,31 @@ class DLFPBackend(Backend, ICapMessages, ICapMessagesReply): return self._browser raise AttributeError, name - def iter_messages(self): - for message in self._iter_messages(False): + def iter_messages(self, thread=None): + for message in self._iter_messages(thread, False): yield message - def iter_new_messages(self): - for message in self._iter_messages(True): + def iter_new_messages(self, thread=None): + for message in self._iter_messages(thread, True): yield message - def _iter_messages(self, only_new): + def _iter_messages(self, thread, only_new): if self.config['get_news']: - for message in self._iter_messages_of('newspaper', only_new): + for message in self._iter_messages_of('newspaper', thread, only_new): yield message if self.config['get_telegrams']: - for message in self._iter_messages_of('telegram', only_new): + for message in self._iter_messages_of('telegram', thread, only_new): yield message - def _iter_messages_of(self, what, only_new): + def _iter_messages_of(self, what, thread, only_new): if not what in self.storage.get(self.name, 'seen'): self.storage.set(self.name, 'seen', what, {}) seen = {} for article in ArticlesList(what).iter_articles(): + if thread and thread != article.id: + continue + thread = self.browser.get_content(article.id) if not article.id in self.storage.get(self.name, 'seen', what): diff --git a/weboob/capabilities/messages.py b/weboob/capabilities/messages.py index 68d45c2d..0faef3b1 100644 --- a/weboob/capabilities/messages.py +++ b/weboob/capabilities/messages.py @@ -83,17 +83,21 @@ class Message: return result.encode('utf-8') class ICapMessages(ICap): - def iter_new_messages(self): + def iter_new_messages(self, thread=None): """ Iterates on new messages from last time this function has been called. + @param thread thread name (optional) @return [list] Message objects """ raise NotImplementedError() - def iter_messages(self): + def iter_messages(self, thread=None): """ Iterates on every messages + + @param thread thread name (optional) + @return [list] Message objects """ raise NotImplementedError() diff --git a/weboob/ouiboube.py b/weboob/ouiboube.py index 84f288da..9175da29 100644 --- a/weboob/ouiboube.py +++ b/weboob/ouiboube.py @@ -64,7 +64,7 @@ class Weboob: # Check conditions if (not caps is None and not module.has_caps(caps)) or \ - (not names is None and not module.name in name): + (not names is None and not name in names): continue try: