# -*- coding: utf-8 -*- """ Copyright(C) 2008-2010 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 re from datetime import datetime from dateutil import tz from logging import error, warning 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 class MailParser(Mail): """
Romain

27 octobre 2008, 01:11:32, nouveau


Moi en g�n�ral j'aime sortir tout simplement dans des bars, pour discuter avec mes amis et/ou coll�gues, et rencontrer des gens. Sinon je fais de la guitare, et je d�veloppe des projets perso.

""" DATETIME_REGEXP = re.compile(u'([0-9]{1,2}) ([a-zéû]*) ([0-9]{4}), ([0-9]{2}):([0-9]{2}):([0-9]{2})(.*)') months = [u'', u'janvier', u'février', u'mars', u'avril', u'mai', u'juin', u'juillet', u'août', u'septembre', u'octobre', u'novembre', u'décembre'] SMILEY_REGEXP = re.compile('http://s.adopteunmec.com/img/smile/([0-9]+).gif') smileys = {0: ':)', 1: ':D', 2: ':o', 3: ':p', 4: ';)', 5: ':(', 6: ':$', 7: ':\'(', 11: ':#', 14: ':\\', 15: ':|', 10: '(L)', } def __init__(self, id, name, tr): # implicit Mail.__init__(self, id, name) self.tr = tr.childNodes[0].childNodes[1].childNodes[0].childNodes[0] tds = self.tr.childNodes counter = 0 for td in tds: if not hasattr(td, 'tagName') or td.tagName != u'td': continue counter += 1 if counter == 3: date = u'' for child in td.childNodes[1].childNodes: if hasattr(child, 'data'): date += child.data self.parse_date(date) content = '' for c in td.childNodes[3].childNodes: if hasattr(c, 'data'): content += ''.join(c.data.split('\n')) # to strip \n elif hasattr(c, 'tagName'): if c.tagName == 'br': content += '\n' elif c.tagName == 'img' and c.hasAttribute('src'): m = self.SMILEY_REGEXP.match(c.getAttribute('src')) if m and self.smileys.has_key(int(m.group(1))): try: content += self.smileys[int(m.group(1))] except KeyError: error('Mail: unable to find this smiley: %s' % c.getAttribute('src')) self.content = content break self.parse_profile_link() 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')) m = self.DATETIME_REGEXP.match(date_str) if m: day = int(m.group(1)) month = self.months.index(m.group(2)) year = int(m.group(3)) hour = int(m.group(4)) minute = int(m.group(5)) sec = int(m.group(6)) # build datetime object with local timezone d = datetime(year, month, day, hour, minute, sec, tzinfo=tz.tzlocal()) # then, convert it to UTC timezone d = d.astimezone(tz.tzutc()) # and get timestamp self.date = d if m.group(7).find('nouveau') >= 0: self.new = True else: error('Error: unable to parse the datetime string "%s"' % date_str) def parse_profile_link(self): tables = self.tr.getElementsByTagName('div') for table in tables: if table.hasAttribute('class') and table.getAttribute('class') == 'mini' and table.hasAttribute('onclick'): text = table.getAttribute('onclick') regexp = re.compile("window.location='(.*)'") m = regexp.match(text) if m: self.profile_link = m.group(1) return warning('Unable to find the profile URL in the message %s@%s' % (self.get_from(), self.get_id())) def parse_from(self): tds = self.tr.getElementsByTagName('div') for td in tds: if not td.hasAttribute('class') or td.getAttribute('class') != 'mini_pseudo': continue if td.childNodes: self.sender = td.childNodes[0].data return warning('Warning: unable to find from in the mail %s' % self.get_id()) class ContactThreadPage(PageBase): """
  Ecrire � zoe
""" def post(self, content): try: self.browser.select_form(name="sendMsg") if isinstance(content, unicode): content = content.encode('iso-8859-15', 'replace') self.browser['message'] = content self.browser.submit() # submit current form except FormNotFoundError, e: error = 'Unknown error (%s)' % e p_list = self.document.getElementsByTagName('p') for p in p_list: if p.hasAttribute('align') and p.getAttribute('align') == 'center': error = p.firstChild.data break raise AdopteCantPostMail(error) """ content ...
  voir tous les messages
""" id_regexp = re.compile("/thread.php\?id=([0-9]+)") def loaded(self): self.items = [] a_list = self.document.getElementsByTagName('a') self.id = 0 for a in a_list: if a.hasAttribute('href'): m = self.id_regexp.match(a.getAttribute('href')) if m: self.id = int(m.group(1)) break self.name = '' big_list = self.document.getElementsByTagName('big') for big in big_list: child = big.firstChild if hasattr(child, 'tagName') and child.tagName == u'b': self.name = child.firstChild.data break tables = self.document.getElementsByTagName('table') table = None for t in tables: if t.hasAttribute('style') and t.getAttribute('style') == 'width:700px;background:black': table = t break if not table: # It is probably the 'sent' page return for tag in table.childNodes[1].childNodes[1:]: if not hasattr(tag, 'tagName') or tag.tagName != u'tr': continue if not tag.hasAttribute('valign'): continue mail = MailParser(self.id, self.name, tag) if self.items: self.items[-1].reply_date = mail.get_date_int() self.items += [mail] def get_mails(self): return self.items