adapt to new ICapMessages API

This commit is contained in:
Romain Bignon 2010-08-30 16:38:34 +02:00
commit e7c24c13c8
12 changed files with 381 additions and 249 deletions

View file

@ -19,16 +19,16 @@ from __future__ import with_statement
from datetime import datetime
from dateutil import tz
from logging import warning
from time import sleep
from logging import warning, debug
from weboob.capabilities.chat import ICapChat
from weboob.capabilities.messages import ICapMessages, ICapMessagesReply, Message
from weboob.capabilities.messages import ICapMessages, ICapMessagesPost, Message, Thread
from weboob.capabilities.dating import ICapDating, StatusField
from weboob.capabilities.contact import ICapContact, Contact, ProfileNode
from weboob.tools.backend import BaseBackend
from weboob.tools.browser import BrowserUnavailable
from .captcha import CaptchaError
from .browser import AuMBrowser
from .exceptions import AdopteWait
from .optim.profiles_walker import ProfilesWalker
@ -38,7 +38,7 @@ from .optim.visibility import Visibility
__all__ = ['AuMBackend']
class AuMBackend(BaseBackend, ICapMessages, ICapMessagesReply, ICapDating, ICapChat, ICapContact):
class AuMBackend(BaseBackend, ICapMessages, ICapMessagesPost, ICapDating, ICapChat, ICapContact):
NAME = 'aum'
MAINTAINER = 'Romain Bignon'
EMAIL = 'romain@peerfuse.org'
@ -54,17 +54,27 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesReply, ICapDating, ICapC
}
BROWSER = AuMBrowser
MAGIC_ID_BASKET = 1
def create_default_browser(self):
if self.config['register']:
browser = self.create_browser(self.config['username'])
browser.register(password= self.config['password'],
sex= 0,
birthday_d= 1,
birthday_m= 1,
birthday_y= 1970,
zipcode= 75001,
country= 'fr',
godfather= '')
browser = None
while not browser:
try:
browser = self.create_browser(self.config['username'])
browser.register(password= self.config['password'],
sex= 0,
birthday_d= 1,
birthday_m= 1,
birthday_y= 1970,
zipcode= 75001,
country= 'fr',
godfather= '')
except CaptchaError:
debug('Unable to resolve captcha. Retrying...')
browser = None
browser.password = self.config['password']
return browser
else:
return self.create_browser(self.config['username'], self.config['password'])
@ -79,16 +89,117 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesReply, ICapDating, ICapC
except AdopteWait:
return (StatusField('notice', '', u'<h3>You are currently waiting 1am to be able to connect with this account</h3>', StatusField.FIELD_HTML|StatusField.FIELD_TEXT))
def iter_messages(self, thread=None):
for message in self._iter_messages(thread, False):
yield message
def iter_threads(self):
with self.browser:
contacts = self.browser.get_threads_list()
def iter_new_messages(self, thread=None):
for message in self._iter_messages(thread, True):
yield message
for contact in contacts:
thread = Thread(contact.get_id())
yield thread
def get_thread(self, id):
thread = None
if isinstance(id, Thread):
thread = id
id = thread.id
if not thread:
thread = Thread(id)
full = False
else:
full = True
with self.browser:
mails = self.browser.get_thread_mails(id, full)
my_name = self.browser.get_my_name()
child = None
msg = None
slut = self._get_slut(id)
for mail in mails:
flags = 0
if mail.date > slut['lastmsg']:
flags |= Message.IS_UNREAD
if mail.sender != my_name:
if mail.new:
flags |= Message.IS_NOT_ACCUSED
else:
flags |= Message.IS_ACCUSED
msg = Message(thread=thread,
id=mail.message_id,
title=mail.title,
sender=mail.sender,
receiver=mail.name if mail.sender == my_name else my_name, # TODO: me
date=mail.date,
content=mail.content,
signature=mail.signature,
children=[],
flags=flags)
if child:
msg.children.append(child)
child.parent = msg
child = msg
if full:
# If we have get all the messages, replace NotLoaded with None as
# parent.
msg.parent = None
thread.root = msg
return thread
def iter_unread_messages(self, thread=None):
with self.browser:
contacts = self.browser.get_threads_list()
for contact in contacts:
slut = self._get_slut(contact.get_id())
if contact.get_lastmsg_date() > slut['lastmsg']:
thread = self.get_thread(contact.get_id())
for m in thread.iter_all_messages():
if m.flags & m.IS_UNREAD:
yield m
# Send mail when someone added me in her basket.
# XXX possibly race condition if a slut adds me in her basket
# between the aum.nb_new_baskets() and aum.get_baskets().
with self.browser:
new_baskets = self.browser.nb_new_baskets()
if new_baskets:
ids = self.browser.get_baskets()
while new_baskets > 0 and len(ids) > new_baskets:
new_baskets -= 1
profile = self.browser.get_profile(ids[new_baskets])
thread = Thread(profile.get_id())
thread.root = Message(thread=thread,
id=self.MAGIC_ID_BASKET,
title='Basket of %s' % profile.get_name(),
sender=profile.get_name(),
receiver=self.browser.get_my_name(),
date=None, # now
content='You are taken in her basket!',
signature=profile.get_profile_text(),
children=[],
flags=Message.IS_UNREAD)
yield thread.root
def set_message_read(self, message):
if message.id == self.MAGIC_ID_BASKET:
# We don't save baskets.
return
slut = self._get_slut(message.thread.id)
if slut['lastmsg'] < message.date:
slut['lastmsg'] = message.date
#slut['msgstatus'] = contact.get_status()
self.storage.set('sluts', message.thread.id, slut)
self.storage.save()
def _get_slut(self, id):
if not id in self.storage.get('sluts'):
sluts = self.storage.get('sluts')
if not sluts or not id in sluts:
slut = {'lastmsg': datetime(1970,1,1),
'msgstatus': ''}
else:
@ -97,81 +208,9 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesReply, ICapDating, ICapC
slut['lastmsg'] = slut['lastmsg'].replace(tzinfo=tz.tzutc())
return slut
def _iter_messages(self, thread, only_new):
def post_message(self, message):
with self.browser:
try:
profiles = {}
if thread:
slut = self._get_slut(int(thread))
for mail in self._iter_thread_messages(thread, only_new, slut['lastmsg'], {}):
if slut['lastmsg'] < mail.date:
slut['lastmsg'] = mail.date
yield mail
self.storage.set('sluts', int(thread), slut)
self.storage.save()
else:
contacts = self.browser.get_threads_list()
for contact in contacts:
slut = self._get_slut(contact.get_id())
last_msg = slut['lastmsg']
if only_new and contact.get_lastmsg_date() < last_msg and contact.get_status() == slut['msgstatus'] or \
not thread is None and int(thread) != contact.get_id():
continue
for mail in self._iter_thread_messages(contact.get_id(), only_new, last_msg, profiles):
if last_msg < mail.date:
last_msg = mail.date
yield mail
slut['lastmsg'] = last_msg
slut['msgstatus'] = contact.get_status()
self.storage.set('sluts', contact.get_id(), slut)
self.storage.save()
# Send mail when someone added me in her basket.
# XXX possibly race condition if a slut adds me in her basket
# between the aum.nbNewBaskets() and aum.getBaskets().
new_baskets = self.browser.nb_new_baskets()
if new_baskets:
ids = self.browser.get_baskets()
while new_baskets > 0 and len(ids) > new_baskets:
new_baskets -= 1
profile = self.browser.get_profile(ids[new_baskets])
yield Message(profile.get_id(), 1,
title='Basket of %s' % profile.get_name(),
sender=profile.get_name(),
content='You are taken in her basket!',
signature=profile.get_profile_text())
except BrowserUnavailable:
pass
def _iter_thread_messages(self, id, only_new, last_msg, profiles):
mails = self.browser.get_thread_mails(id)
for mail in mails:
if only_new and mail.date <= last_msg:
continue
if not mail.profile_link in profiles:
profiles[mail.profile_link] = self.browser.get_profile(mail.profile_link)
mail.signature += u'\n%s' % profiles[mail.profile_link].get_profile_text()
yield mail
def post_reply(self, thread_id, reply_id, title, message):
while 1:
try:
with self.browser:
self.browser.post_mail(thread_id, message)
except AdopteWait:
# If we are on a waiting state, retry every 30 minutes until it is posted.
sleep(60*30)
else:
return
self.browser.post_mail(message.thread.id, message.content)
def get_contact(self, contact):
try:
@ -268,4 +307,8 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesReply, ICapDating, ICapC
data = self.browser.openurl(photo.thumbnail_url).read()
contact.set_photo(name, thumbnail_data=data)
OBJECTS = {Contact: fill_contact}
def fill_thread(self, thread, fields):
return self.get_thread(thread)
OBJECTS = {Thread: fill_thread,
Contact: fill_contact}