<commit_before># -*- coding: utf-8 -*-

# Copyright(C) 2008-2011  <NAME>
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.


from weboob.backends.aum.pages.base import PageBase
from weboob.tools.ordereddict import OrderedDict

from copy import deepcopy
from logging import warning
import re

class FieldBase:

    def __init__(self, key):
        self.key = key

    def put_value(self, d, value):
        raise NotImplementedError

class FieldString(FieldBase):
    def put_value(self, d, value):
        d[self.key] = unicode(value)

class FieldList(FieldBase):
    def put_value(self, d, value):
        d[self.key] = value.split(', ')

class FieldWideList(FieldBase):

    def put_value(self, d, value):
        d[self.key] += [value]

class FieldOld(FieldBase):
    regexp = re.compile(u'([0-9]+) ans( \(Née le  ([0-9]+) ([^ ]+) ([0-9]+)\))?')
    month2i = ['', 'janvier', u'février', 'mars', 'avril', 'mai', 'juin', 'juillet', u'août', 'septembre', 'octobre', 'novembre', u'décembre']

    def put_value(self, d, value):
        m = self.regexp.match(value)
        if not m:
            return

        d[self.key] = int(m.group(1))
        if not m.group(2):
            return

        try:
            d['birthday'] = (int(m.group(3)),
                             self.month2i.index(m.group(4)),
                             int(m.group(5)))
        except ValueError, e:
            print str(e)

class FieldLocation(FieldBase):
    location = re.compile('(?P<location>.+?)( \((?P<zipcode>[0-9]{5})\))?, (?P<country>.*)')

    def __init__(self):
        FieldBase.__init__(self, '')
    def put_value(self, d, value):
        # TODO: determine distance, or something like
        m = self.location.match(value)
        if m:
            for field in ('country', 'location', 'zipcode'):
                d[field] = m.groupdict().get(field)
        else:
            warning('Unable to parse the location "%s"' % value)
            d['location'] = unicode(value)

class FieldMeasurements(FieldBase):
    height = re.compile('([0-9]{1,3}) cm')
    weight = re.compile('([0-9]{1,3}) kg')
    # TODO: parse third parameter

    def __init__(self):
        FieldBase.__init__(self, '')
    def put_value(self, d, value):
        for s in value.split(', '):
            m = self.height.match(s)
            if m:
                d['height'] = int(m.group(1))
                continue
            m = self.weight.match(s)
            if m:
                d['weight'] = int(m.group(1))
                continue
        if d['height'] and d['weight']:
            bmi = (d['weight']/float(pow(d['height']/100.0, 2)))
            if bmi < 15.5:
                d['fat'] = 'severely underweight'
            elif bmi < 18.4:
                d['fat'] = 'underweight'
            elif bmi < 24.9:
                d['fat'] = 'normal'
            elif bmi < 30:
                d['fat'] = 'overweight'
            else:
                d['fat'] = 'obese'
            d['BMI'] = bmi

class FieldParticularSignes(FieldBase):
    def __init__(self): FieldBase.__init__(self, '')
    def put_value(self, d, value):
        for s in value.split(', '):
            if s.find('tatouages') >= 0:
                d['tatoos'] = True
            elif s.find('piercing') >= 0:
                d['piercing'] = True
            elif s.find('lunettes') >= 0:
                d['glasses'] = True
            elif s.find('rousseur') >= 0:
                d['freckle'] = True

class ProfilePage(PageBase):
    empty_table = OrderedDict((
                    ('details',    OrderedDict((
                                    ('old',           0),
                                    ('birthday',      (0,0,0)),
                                    ('zipcode',       0),
                                    ('location',      ''),
                                    ('country',       ''),
                                    ('eyes',          ''),
                                    ('hairs',         []),
                                    ('height',        0),
                                    ('weight',        0),
                                    ('BMI',           0),
                                    ('fat',           ''),
                                    ('from',          ''),
                                    ('tatoos',        False),
                                    ('piercing',      False),
                                    ('freckle',       False),
                                    ('glasses',       False),
                                    ('job',           ''),
                                    ('style',         ''),
                                    ('alimentation',  ''),
                                    ('alcool',        ''),
                                    ('tabac',         ''),
                                   ))),
                    ('liking',      OrderedDict((
                                    ('activities',    ''),
                                    ('music',         []),
                                    ('cinema',        []),
                                    ('books',         []),
                                    ('tv',            []),
                                   ))),
                    ('sex',         OrderedDict((
                                    ('underwear',     []),
                                    ('top',           []),
                                    ('bottom',        []),
                                    ('interval',      ''),
                                    ('favorite',      []),
                                    ('practices',     []),
                                    ('toys',          []),
                                   ))),
                   ('personality', OrderedDict((
                                    ('snap',          ''),
                                    ('exciting',      ''),
                                    ('hate',          ''),
                                    ('vices',         ''),
                                    ('assets',        ''),
                                    ('fantasies',     ''),
                                    ('is',            []),
                                   ))),
                ))

    tables = {'tab_0': 'details',
              'tab_1': 'liking',
              'tab_2': 'sex',
              'tab_3': 'personality'
             }

    fields =      {'details':      {'Age':                  FieldOld('old'),
                                    u'Réside à':            FieldLocation(),
                                    'Yeux':                 FieldString('eyes'),
                                    'Cheveux':              FieldList('hairs'),
                                    'Mensurations ':        FieldMeasurements(),
                                    'Origines ':            FieldString('from'),
                                    'Signes particuliers ': FieldParticularSignes(),
                                    'Style ':               FieldString('style'),
                                    'Profession ':          FieldString('job'),
                                    'Alimentation':         FieldString('alimentation'),
                                    'Alcool':               FieldString('alcool'),
                                    'Tabac':                FieldString('tabac'),
                                   },
                   'liking':       {'Hobbies ':             FieldString('activities'),
                                    'Musique ':             FieldWideList('music'),
                                   u'Cinéma':               FieldWideList('cinema'),
                                    'Livres ':              FieldWideList('books'),
                                   u'Télé':                 FieldWideList('tv'),
                                   },
                   'sex':          {u'Sous-v\xeatements ':  FieldList('underwear'),
                                    '... en haut ':         FieldList('top'),
                                    '... en bas ':          FieldList('bottom'),
                                    u'Fréquence idéale des rapports sexuels ':
                                                            FieldString('interval'),
                                   u'Position favorite ':   FieldList('favorite'),
                                    'Pratiques sexuelles ': FieldList('practices'),
                                   u'Accessoires préférés ':FieldList('toys'),
                                   u'Ce qui se cache en dessous ':
                                                            FieldList('underwear'),
                                   u"Ce qui m'\xe9moustille ":
                                                            FieldList('favorite'),
                                   u"Au lit j'aime ":       FieldList('practices'),
                                   u'Mes accessoires ':     FieldList('toys'),
                                   },
                   'personality':  {u'Ça la fait craquer ': FieldString('snap'),
                                    u'Ça l\'excite ':       FieldString('exciting'),
                                    u'Elle déteste ':       FieldString('hate'),
                                    'Ses vices ':           FieldString('vices'),
                                    'Ses atouts ':          FieldString('assets'),
                                    'Ses fantasmes ':       FieldString('fantasies'),
                                    'Elle est  ':           FieldList('is'),
                                   },
                }

    ID_REGEXP = re.compile('(charm|addBasket|openAlbum)\(([0-9]+)(,[\s\'\d]+)?\)')
    PHOTO_REGEXP = re.compile('http://(s|p)([0-9]+)\.adopteunmec\.com/(.*)')

    STATS2ID = {'visites':   'visits',
                'charmes':   'charms',
                'paniers':   'baskets',
                'mails':     'mails',
                'POPULARIT': 'score',
               }
    STATS_VALUE_REGEXP = re.compile('([0-9\s]+).*')

    def __repr__(self):
        if isinstance(self.name, unicode):
            name = self.name.encode('utf-8', 'backslashreplace')
        else:
            name = self.name
        return '<Profile name="%s">' % name

    def on_loaded(self):
        self.name = u''
        self.description = u''
        self.table = deepcopy(self.empty_table)
        self.id = 0
        self.photos = []
        self.status = ''
        self.stats = OrderedDict((
                      ('score',   0),
                      ('visits',  0),
                      ('charms',  0),
                      ('baskets', 0),
                      ('mails',   0),
                     ))

        divs = self.document.getElementsByTagName('td')
        for div in divs:
            if (div.hasAttribute('style') and
                    div.getAttribute('style') == "color:#ffffff;font-size:32px;font-weight:bold;letter-spacing:-2px" and
                    hasattr(div.firstChild, 'data')):
                self.name = div.firstChild.data
            if (div.hasAttribute('style') and
                    div.getAttribute('style') == "font-size:12px;font-weight:bold" and
                    hasattr(div.firstChild, 'data')):
                self.status = div.firstChild.data
            if div.hasAttribute('background'):
                m = self.PHOTO_REGEXP.match(div.getAttribute('background'))
                if m:
                    self.photos.append(dict(url=re.sub(u'thumb[0-2]_', u'image', div.getAttribute('background')),<commit_msg>should fix #645 (and should help to display profile name in qhavesex too)<commit_after># -*- coding: utf-8 -*-

# Copyright(C) 2008-2011  <NAME>
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.


from weboob.backends.aum.pages.base import PageBase
from weboob.tools.ordereddict import OrderedDict

from copy import deepcopy
from logging import warning
import re

class FieldBase:

    def __init__(self, key):
        self.key = key

    def put_value(self, d, value):
        raise NotImplementedError

class FieldString(FieldBase):
    def put_value(self, d, value):
        d[self.key] = unicode(value)

class FieldList(FieldBase):
    def put_value(self, d, value):
        d[self.key] = value.split(', ')

class FieldWideList(FieldBase):

    def put_value(self, d, value):
        d[self.key] += [value]

class FieldOld(FieldBase):
    regexp = re.compile(u'([0-9]+) ans( \(Née le  ([0-9]+) ([^ ]+) ([0-9]+)\))?')
    month2i = ['', 'janvier', u'février', 'mars', 'avril', 'mai', 'juin', 'juillet', u'août', 'septembre', 'octobre', 'novembre', u'décembre']

    def put_value(self, d, value):
        m = self.regexp.match(value)
        if not m:
            return

        d[self.key] = int(m.group(1))
        if not m.group(2):
            return

        try:
            d['birthday'] = (int(m.group(3)),
                             self.month2i.index(m.group(4)),
                             int(m.group(5)))
        except ValueError, e:
            print str(e)

class FieldLocation(FieldBase):
    location = re.compile('(?P<location>.+?)( \((?P<zipcode>[0-9]{5})\))?, (?P<country>.*)')

    def __init__(self):
        FieldBase.__init__(self, '')
    def put_value(self, d, value):
        # TODO: determine distance, or something like
        m = self.location.match(value)
        if m:
            for field in ('country', 'location', 'zipcode'):
                d[field] = m.groupdict().get(field)
        else:
            warning('Unable to parse the location "%s"' % value)
            d['location'] = unicode(value)

class FieldMeasurements(FieldBase):
    height = re.compile('([0-9]{1,3}) cm')
    weight = re.compile('([0-9]{1,3}) kg')
    # TODO: parse third parameter

    def __init__(self):
        FieldBase.__init__(self, '')
    def put_value(self, d, value):
        for s in value.split(', '):
            m = self.height.match(s)
            if m:
                d['height'] = int(m.group(1))
                continue
            m = self.weight.match(s)
            if m:
                d['weight'] = int(m.group(1))
                continue
        if d['height'] and d['weight']:
            bmi = (d['weight']/float(pow(d['height']/100.0, 2)))
            if bmi < 15.5:
                d['fat'] = 'severely underweight'
            elif bmi < 18.4:
                d['fat'] = 'underweight'
            elif bmi < 24.9:
                d['fat'] = 'normal'
            elif bmi < 30:
                d['fat'] = 'overweight'
            else:
                d['fat'] = 'obese'
            d['BMI'] = bmi

class FieldParticularSignes(FieldBase):
    def __init__(self): FieldBase.__init__(self, '')
    def put_value(self, d, value):
        for s in value.split(', '):
            if s.find('tatouages') >= 0:
                d['tatoos'] = True
            elif s.find('piercing') >= 0:
                d['piercing'] = True
            elif s.find('lunettes') >= 0:
                d['glasses'] = True
            elif s.find('rousseur') >= 0:
                d['freckle'] = True

class ProfilePage(PageBase):
    empty_table = OrderedDict((
                    ('details',    OrderedDict((
                                    ('old',           0),
                                    ('birthday',      (0,0,0)),
                                    ('zipcode',       0),
                                    ('location',      ''),
                                    ('country',       ''),
                                    ('eyes',          ''),
                                    ('hairs',         []),
                                    ('height',        0),
                                    ('weight',        0),
                                    ('BMI',           0),
                                    ('fat',           ''),
                                    ('from',          ''),
                                    ('tatoos',        False),
                                    ('piercing',      False),
                                    ('freckle',       False),
                                    ('glasses',       False),
                                    ('job',           ''),
                                    ('style',         ''),
                                    ('alimentation',  ''),
                                    ('alcool',        ''),
                                    ('tabac',         ''),
                                   ))),
                    ('liking',      OrderedDict((
                                    ('activities',    ''),
                                    ('music',         []),
                                    ('cinema',        []),
                                    ('books',         []),
                                    ('tv',            []),
                                   ))),
                    ('sex',         OrderedDict((
                                    ('underwear',     []),
                                    ('top',           []),
                                    ('bottom',        []),
                                    ('interval',      ''),
                                    ('favorite',      []),
                                    ('practices',     []),
                                    ('toys',          []),
                                   ))),
                   ('personality', OrderedDict((
                                    ('snap',          ''),
                                    ('exciting',      ''),
                                    ('hate',          ''),
                                    ('vices',         ''),
                                    ('assets',        ''),
                                    ('fantasies',     ''),
                                    ('is',            []),
                                   ))),
                ))

    tables = {'tab_0': 'details',
              'tab_1': 'liking',
              'tab_2': 'sex',
              'tab_3': 'personality'
             }

    fields =      {'details':      {'Age':                  FieldOld('old'),
                                    u'Réside à':            FieldLocation(),
                                    'Yeux':                 FieldString('eyes'),
                                    'Cheveux':              FieldList('hairs'),
                                    'Mensurations ':        FieldMeasurements(),
                                    'Origines ':            FieldString('from'),
                                    'Signes particuliers ': FieldParticularSignes(),
                                    'Style ':               FieldString('style'),
                                    'Profession ':          FieldString('job'),
                                    'Alimentation':         FieldString('alimentation'),
                                    'Alcool':               FieldString('alcool'),
                                    'Tabac':                FieldString('tabac'),
                                   },
                   'liking':       {'Hobbies ':             FieldString('activities'),
                                    'Musique ':             FieldWideList('music'),
                                   u'Cinéma':               FieldWideList('cinema'),
                                    'Livres ':              FieldWideList('books'),
                                   u'Télé':                 FieldWideList('tv'),
                                   },
                   'sex':          {u'Sous-v\xeatements ':  FieldList('underwear'),
                                    '... en haut ':         FieldList('top'),
                                    '... en bas ':          FieldList('bottom'),
                                    u'Fréquence idéale des rapports sexuels ':
                                                            FieldString('interval'),
                                   u'Position favorite ':   FieldList('favorite'),
                                    'Pratiques sexuelles ': FieldList('practices'),
                                   u'Accessoires préférés ':FieldList('toys'),
                                   u'Ce qui se cache en dessous ':
                                                            FieldList('underwear'),
                                   u"Ce qui m'\xe9moustille ":
                                                            FieldList('favorite'),
                                   u"Au lit j'aime ":       FieldList('practices'),
                                   u'Mes accessoires ':     FieldList('toys'),
                                   },
                   'personality':  {u'Ça la fait craquer ': FieldString('snap'),
                                    u'Ça l\'excite ':       FieldString('exciting'),
                                    u'Elle déteste ':       FieldString('hate'),
                                    'Ses vices ':           FieldString('vices'),
                                    'Ses atouts ':          FieldString('assets'),
                                    'Ses fantasmes ':       FieldString('fantasies'),
                                    'Elle est  ':           FieldList('is'),
                                   },
                }

    ID_REGEXP = re.compile('(charm|addBasket|openAlbum)\(([0-9]+)(,[\s\'\d]+)?\)')
    PHOTO_REGEXP = re.compile('http://(s|p)([0-9]+)\.adopteunmec\.com/(.*)')

    STATS2ID = {'visites':   'visits',
                'charmes':   'charms',
                'paniers':   'baskets',
                'mails':     'mails',
                'POPULARIT': 'score',
               }
    STATS_VALUE_REGEXP = re.compile('([0-9\s]+).*')

    def __repr__(self):
        if isinstance(self.name, unicode):
            name = self.name.encode('utf-8', 'backslashreplace')
        else:
            name = self.name
        return '<Profile name="%s">' % name

    def on_loaded(self):
        self.name = u''
        self.description = u''
        self.table = deepcopy(self.empty_table)
        self.id = 0
        self.photos = []
        self.status = ''
        self.stats = OrderedDict((
                      ('score',   0),
                      ('visits',  0),
                      ('charms',  0),
                      ('baskets', 0),
                      ('mails',   0),
                     ))

        divs = self.document.getElementsByTagName('td')
        for div in divs:
            if (div.hasAttribute('style') and
                    div.getAttribute('style') == "color:#ffffff;font-size:32px;font-weight:bold;letter-spacing:-2px" and
                    hasattr(div.firstChild, 'data')):
                if len(div.childNodes) > 1: 
                    self.name = div.childNodes[1].data
            if (div.hasAttribute('style') and
                    div.getAttribute('style') == "font-size:12px;font-weight:bold" and
                    hasattr(div.firstChild, 'data')):
                self.status = div.firstChild.data
            if div.hasAttribute('background'):
                m = self.PHOTO_REGEXP.match(div.getAttribute('background'))
                if m:
                    self.photos.append(dict(url=re.sub(u'thumb[0-2]_', u'image', div.getAttribute('background')),
{'fraction_non_alphanumeric': 0.13136075434888636, 'fraction_numerical': 0.007519102584945537, 'mean_word_length': 0.9882828282828283, 'pattern_counts': {'":': 4, '<': 23, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 182, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4652766', 'n_tokens_mistral': 6477, 'n_tokens_neox': 6018, 'n_words': 1264}