From 3bf772ec5104e72471d5f6d5bcf239d4430c795b Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Wed, 7 Dec 2011 15:05:58 +0100 Subject: [PATCH] fixes to support parsing AdopteUnMec's profile --- weboob/backends/aum/antispam.py | 3 +++ weboob/backends/aum/backend.py | 21 ++++++++++----------- weboob/backends/aum/browser.py | 28 ++++++++++++++++------------ weboob/backends/aum/contact.py | 32 +++++++++++++++++--------------- 4 files changed, 46 insertions(+), 38 deletions(-) diff --git a/weboob/backends/aum/antispam.py b/weboob/backends/aum/antispam.py index f668e3e5..00326a88 100644 --- a/weboob/backends/aum/antispam.py +++ b/weboob/backends/aum/antispam.py @@ -71,6 +71,9 @@ class AntiSpam(object): return True def check_contact(self, contact): + if contact.id == 1: + return True + if not self.check_profile(contact.aum_profile): return False diff --git a/weboob/backends/aum/backend.py b/weboob/backends/aum/backend.py index a19a9330..5fe6bd6d 100644 --- a/weboob/backends/aum/backend.py +++ b/weboob/backends/aum/backend.py @@ -389,17 +389,18 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesPost, ICapDating, ICapCh return None s = 0 - if contact['isOnline']: + if contact.get('isOnline', False): s = Contact.STATUS_ONLINE else: s = Contact.STATUS_OFFLINE c = Contact(contact['id'], contact['pseudo'], s) c.url = self.browser.id2url(contact['id']) - birthday = _parse_dt(contact['birthday']) - age = int((datetime.datetime.now() - birthday).days / 365.25) - c.status_msg = u'%s old, %s' % (age, contact['city']) - if int(contact['cover']) > 0: + if 'birthday' in contact: + birthday = _parse_dt(contact['birthday']) + age = int((datetime.datetime.now() - birthday).days / 365.25) + c.status_msg = u'%s old, %s' % (age, contact['city']) + if contact['cover'].isdigit() and int(contact['cover']) > 0: url = 'http://s%s.adopteunmec.com/%s%%(type)s%s.jpg' % (contact['shard'], contact['path'], contact['cover']) else: url = 'http://s.adopteunmec.com/www/img/thumb0.gif' @@ -438,21 +439,19 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesPost, ICapDating, ICapCh if not self.browser.send_charm(id): raise QueryError('No enough charms available') return Query(id, 'A charm has been sent') - + def get_notes(self, id): if isinstance(id, Contact): id = id.id - + return self.storage.get('notes', id) - + def save_notes(self, id, notes): if isinstance(id, Contact): id = id.id - + self.storage.set('notes', id, notes) self.storage.save() - - # ---- ICapChat methods --------------------- diff --git a/weboob/backends/aum/browser.py b/weboob/backends/aum/browser.py index 80d73e07..321126c7 100644 --- a/weboob/backends/aum/browser.py +++ b/weboob/backends/aum/browser.py @@ -326,22 +326,26 @@ class AuMBrowser(BaseBrowser): @url2id def get_profile(self, id, with_pics=True): r = self.api_request('member', 'view', data={'id': id}) + if not 'result' in r: + print r profile = r['result']['member'] # Calculate distance in km. - coords = (float(profile['lat']), float(profile['lng'])) + profile['dist'] = 0.0 + if 'lat' in profile and 'lng' in profile: + coords = (float(profile['lat']), float(profile['lng'])) - R = 6371 - lat1 = math.radians(self.my_coords[0]) - lat2 = math.radians(coords[0]) - lon1 = math.radians(self.my_coords[1]) - lon2 = math.radians(coords[1]) - dLat = lat2 - lat1 - dLong = lon2 - lon1 - a= pow(math.sin(dLat/2), 2) + math.cos(lat1) * math.cos(lat2) * pow(math.sin(dLong/2), 2) - c= 2 * math.atan2(math.sqrt(a), math.sqrt(1-a)) - profile['dist'] = R * c + R = 6371 + lat1 = math.radians(self.my_coords[0]) + lat2 = math.radians(coords[0]) + lon1 = math.radians(self.my_coords[1]) + lon2 = math.radians(coords[1]) + dLat = lat2 - lat1 + dLong = lon2 - lon1 + a= pow(math.sin(dLat/2), 2) + math.cos(lat1) * math.cos(lat2) * pow(math.sin(dLong/2), 2) + c= 2 * math.atan2(math.sqrt(a), math.sqrt(1-a)) + profile['dist'] = R * c if with_pics: r = self.api_request('member', 'pictures', data={'id': id}) @@ -354,7 +358,7 @@ class AuMBrowser(BaseBrowser): base_url = 'http://s%s.adopteunmec.com/%s' % (profile['shard'], profile['path']) if len(profile['pictures']) > 0: pic_regex = re.compile('(?Phttp://.+\.adopteunmec\.com/.+/)image(?P.+)\.jpg') - pic_max_id = max(int(pic_regex.match(pic['url']).groupdict()['id']) for pic in profile['pictures']) + pic_max_id = max((int((lambda m: m and m.groupdict()['id'] or 0)(pic_regex.match(pic['url'])))) for pic in profile['pictures']) for id in xrange(1, pic_max_id + 1): url = u'%simage%s.jpg' % (base_url, id) if not url in [pic['url'] for pic in profile['pictures']]: diff --git a/weboob/backends/aum/contact.py b/weboob/backends/aum/contact.py index dd9fc80c..bf327a09 100644 --- a/weboob/backends/aum/contact.py +++ b/weboob/backends/aum/contact.py @@ -200,20 +200,21 @@ class Contact(_Contact): )) def parse_profile(self, profile, consts): - if int(profile['cat']) == 1: + cat = int(profile.get('cat', 3)) + if cat == 1: self.status = Contact.STATUS_ONLINE self.status_msg = u'online' self.status_msg = u'since %s' % profile['last_cnx'] - elif int(profile['cat']) == 2: + elif cat == 2: self.status = Contact.STATUS_AWAY self.status_msg = u'away' self.status_msg = u'connection at %s' % profile['last_cnx'] - elif int(profile['cat']) == 3: + elif cat == 3: self.status = Contact.STATUS_OFFLINE self.status_msg = u'last connection %s' % profile['last_cnx'] - self.summary = html2text(profile['about1']).strip().replace('\n\n', '\n') - if len(profile['about2']) > 0: + self.summary = html2text(profile.get('about1', '')).strip().replace('\n\n', '\n') + if len(profile.get('about2', '')) > 0: self.summary += u'\n\nLooking for:\n%s' % html2text(profile['about2']).strip().replace('\n\n', '\n') for photo in profile['pictures']: @@ -223,18 +224,19 @@ class Contact(_Contact): hidden=photo['hidden']) self.profile = OrderedDict() - for section, d in self.TABLE.iteritems(): - flags = ProfileNode.SECTION - if section.startswith('_'): - flags |= ProfileNode.HEAD - section = section.lstrip('_') - s = ProfileNode(section, section.capitalize(), OrderedDict(), flags=flags) + if 'sex' in profile: + for section, d in self.TABLE.iteritems(): + flags = ProfileNode.SECTION + if section.startswith('_'): + flags |= ProfileNode.HEAD + section = section.lstrip('_') + s = ProfileNode(section, section.capitalize(), OrderedDict(), flags=flags) - for key, builder in d.iteritems(): - value = builder.get_value(profile, consts[int(profile['sex'])]) - s.value[key] = ProfileNode(key, key.capitalize().replace('_', ' '), value) + for key, builder in d.iteritems(): + value = builder.get_value(profile, consts[int(profile['sex'])]) + s.value[key] = ProfileNode(key, key.capitalize().replace('_', ' '), value) - self.profile[section] = s + self.profile[section] = s self.aum_profile = profile