[aum] ICapMessages.iter_[new_]messages() implemented

This commit is contained in:
Romain Bignon 2010-04-11 11:48:59 +02:00
commit f3a017cb01
8 changed files with 63 additions and 90 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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):
"""
<td>
@ -96,7 +96,7 @@ class MailParser(Mail):
def __init__(self, id, name, tr):
# <td> <table> implicit<tbody> <tr>
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()))

View file

@ -50,7 +50,6 @@ class FieldOld(FieldBase):
def put_value(self, d, value):
m = self.regexp.match(value)
warning(value)
if not m:
return

View file

@ -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):

View file

@ -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()

View file

@ -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: