fixes to support parsing AdopteUnMec's profile

This commit is contained in:
Romain Bignon 2011-12-07 15:05:58 +01:00
commit 3bf772ec51
4 changed files with 47 additions and 39 deletions

View file

@ -71,6 +71,9 @@ class AntiSpam(object):
return True return True
def check_contact(self, contact): def check_contact(self, contact):
if contact.id == 1:
return True
if not self.check_profile(contact.aum_profile): if not self.check_profile(contact.aum_profile):
return False return False

View file

@ -389,17 +389,18 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesPost, ICapDating, ICapCh
return None return None
s = 0 s = 0
if contact['isOnline']: if contact.get('isOnline', False):
s = Contact.STATUS_ONLINE s = Contact.STATUS_ONLINE
else: else:
s = Contact.STATUS_OFFLINE s = Contact.STATUS_OFFLINE
c = Contact(contact['id'], contact['pseudo'], s) c = Contact(contact['id'], contact['pseudo'], s)
c.url = self.browser.id2url(contact['id']) c.url = self.browser.id2url(contact['id'])
birthday = _parse_dt(contact['birthday']) if 'birthday' in contact:
age = int((datetime.datetime.now() - birthday).days / 365.25) birthday = _parse_dt(contact['birthday'])
c.status_msg = u'%s old, %s' % (age, contact['city']) age = int((datetime.datetime.now() - birthday).days / 365.25)
if int(contact['cover']) > 0: 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']) url = 'http://s%s.adopteunmec.com/%s%%(type)s%s.jpg' % (contact['shard'], contact['path'], contact['cover'])
else: else:
url = 'http://s.adopteunmec.com/www/img/thumb0.gif' 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): if not self.browser.send_charm(id):
raise QueryError('No enough charms available') raise QueryError('No enough charms available')
return Query(id, 'A charm has been sent') return Query(id, 'A charm has been sent')
def get_notes(self, id): def get_notes(self, id):
if isinstance(id, Contact): if isinstance(id, Contact):
id = id.id id = id.id
return self.storage.get('notes', id) return self.storage.get('notes', id)
def save_notes(self, id, notes): def save_notes(self, id, notes):
if isinstance(id, Contact): if isinstance(id, Contact):
id = id.id id = id.id
self.storage.set('notes', id, notes) self.storage.set('notes', id, notes)
self.storage.save() self.storage.save()
# ---- ICapChat methods --------------------- # ---- ICapChat methods ---------------------

View file

@ -326,22 +326,26 @@ class AuMBrowser(BaseBrowser):
@url2id @url2id
def get_profile(self, id, with_pics=True): def get_profile(self, id, with_pics=True):
r = self.api_request('member', 'view', data={'id': id}) r = self.api_request('member', 'view', data={'id': id})
if not 'result' in r:
print r
profile = r['result']['member'] profile = r['result']['member']
# Calculate distance in km. # 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 R = 6371
lat1 = math.radians(self.my_coords[0]) lat1 = math.radians(self.my_coords[0])
lat2 = math.radians(coords[0]) lat2 = math.radians(coords[0])
lon1 = math.radians(self.my_coords[1]) lon1 = math.radians(self.my_coords[1])
lon2 = math.radians(coords[1]) lon2 = math.radians(coords[1])
dLat = lat2 - lat1 dLat = lat2 - lat1
dLong = lon2 - lon1 dLong = lon2 - lon1
a= pow(math.sin(dLat/2), 2) + math.cos(lat1) * math.cos(lat2) * pow(math.sin(dLong/2), 2) 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)) c= 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
profile['dist'] = R * c profile['dist'] = R * c
if with_pics: if with_pics:
r = self.api_request('member', 'pictures', data={'id': id}) 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']) base_url = 'http://s%s.adopteunmec.com/%s' % (profile['shard'], profile['path'])
if len(profile['pictures']) > 0: if len(profile['pictures']) > 0:
pic_regex = re.compile('(?P<base_url>http://.+\.adopteunmec\.com/.+/)image(?P<id>.+)\.jpg') pic_regex = re.compile('(?P<base_url>http://.+\.adopteunmec\.com/.+/)image(?P<id>.+)\.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): for id in xrange(1, pic_max_id + 1):
url = u'%simage%s.jpg' % (base_url, id) url = u'%simage%s.jpg' % (base_url, id)
if not url in [pic['url'] for pic in profile['pictures']]: if not url in [pic['url'] for pic in profile['pictures']]:

View file

@ -200,20 +200,21 @@ class Contact(_Contact):
)) ))
def parse_profile(self, profile, consts): 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 = Contact.STATUS_ONLINE
self.status_msg = u'online' self.status_msg = u'online'
self.status_msg = u'since %s' % profile['last_cnx'] self.status_msg = u'since %s' % profile['last_cnx']
elif int(profile['cat']) == 2: elif cat == 2:
self.status = Contact.STATUS_AWAY self.status = Contact.STATUS_AWAY
self.status_msg = u'away' self.status_msg = u'away'
self.status_msg = u'connection at %s' % profile['last_cnx'] 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 = Contact.STATUS_OFFLINE
self.status_msg = u'last connection %s' % profile['last_cnx'] self.status_msg = u'last connection %s' % profile['last_cnx']
self.summary = html2text(profile['about1']).strip().replace('\n\n', '\n') self.summary = html2text(profile.get('about1', '')).strip().replace('\n\n', '\n')
if len(profile['about2']) > 0: if len(profile.get('about2', '')) > 0:
self.summary += u'\n\nLooking for:\n%s' % html2text(profile['about2']).strip().replace('\n\n', '\n') self.summary += u'\n\nLooking for:\n%s' % html2text(profile['about2']).strip().replace('\n\n', '\n')
for photo in profile['pictures']: for photo in profile['pictures']:
@ -223,18 +224,19 @@ class Contact(_Contact):
hidden=photo['hidden']) hidden=photo['hidden'])
self.profile = OrderedDict() self.profile = OrderedDict()
for section, d in self.TABLE.iteritems(): if 'sex' in profile:
flags = ProfileNode.SECTION for section, d in self.TABLE.iteritems():
if section.startswith('_'): flags = ProfileNode.SECTION
flags |= ProfileNode.HEAD if section.startswith('_'):
section = section.lstrip('_') flags |= ProfileNode.HEAD
s = ProfileNode(section, section.capitalize(), OrderedDict(), flags=flags) section = section.lstrip('_')
s = ProfileNode(section, section.capitalize(), OrderedDict(), flags=flags)
for key, builder in d.iteritems(): for key, builder in d.iteritems():
value = builder.get_value(profile, consts[int(profile['sex'])]) value = builder.get_value(profile, consts[int(profile['sex'])])
s.value[key] = ProfileNode(key, key.capitalize().replace('_', ' '), value) s.value[key] = ProfileNode(key, key.capitalize().replace('_', ' '), value)
self.profile[section] = s self.profile[section] = s
self.aum_profile = profile self.aum_profile = profile