support new API (closes #871,#900,#902)

This commit is contained in:
Romain Bignon 2012-08-04 15:51:47 +02:00
commit c383960729
5 changed files with 205 additions and 333 deletions

View file

@ -65,7 +65,8 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesPost, ICapDating, ICapCh
CONFIG = BackendConfig(Value('username', label='Username'),
ValueBackendPassword('password', label='Password'),
ValueBool('antispam', label='Enable anti-spam', default=False),
ValueBool('baskets', label='Get baskets with new messages', default=True))
ValueBool('baskets', label='Get baskets with new messages', default=True),
Value('search_query', label='Search query', default=''))
STORAGE = {'profiles_walker': {'viewed': []},
'queries_queue': {'queue': []},
'sluts': {},
@ -83,11 +84,14 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesPost, ICapDating, ICapCh
self.antispam = None
def create_default_browser(self):
return self.create_browser(self.config['username'].get(), self.config['password'].get())
return self.create_browser(self.config['username'].get(),
self.config['password'].get(),
self.config['search_query'].get())
def report_spam(self, id):
with self.browser:
self.browser.delete_thread(id)
pass
#self.browser.delete_thread(id)
# Do not report fakes to website, to let them to other guys :)
#self.browser.report_fake(id)
@ -106,15 +110,12 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesPost, ICapDating, ICapCh
all_events[u'visits'] = (self.browser.get_visits, 'Visited by %s')
for type, (events, message) in all_events.iteritems():
for event in events():
try:
e = Event(event['%sid' % type[0]])
except KeyError:
e = Event(event['id'])
e = Event(event['who']['id'])
e.date = parse_dt(event['date'])
e.type = type
if 'member' in event:
e.contact = self._get_partial_contact(event['member'])
if 'who' in event:
e.contact = self._get_partial_contact(event['who'])
else:
e.contact = self._get_partial_contact(event)
@ -134,17 +135,17 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesPost, ICapDating, ICapCh
threads = self.browser.get_threads_list()
for thread in threads:
if thread['member'].get('isBan', thread['member'].get('dead', False)):
with self.browser:
self.browser.delete_thread(thread['member']['id'])
continue
#if thread['member'].get('isBan', thread['member'].get('dead', False)):
# with self.browser:
# self.browser.delete_thread(thread['member']['id'])
# continue
if self.antispam and not self.antispam.check_thread(thread):
self.logger.info('Skipped a spam-thread from %s' % thread['pseudo'])
self.report_spam(thread['member']['id'])
self.report_spam(thread['who']['id'])
continue
t = Thread(int(thread['member']['id']))
t = Thread(int(thread['who']['id']))
t.flags = Thread.IS_DISCUSSION
t.title = u'Discussion with %s' % to_unicode(thread['member']['pseudo'])
t.title = u'Discussion with %s' % to_unicode(thread['who']['pseudo'])
yield t
def get_thread(self, id, contacts=None, get_profiles=False):
@ -176,15 +177,15 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesPost, ICapDating, ICapCh
contacts = {}
if not thread.title:
thread.title = u'Discussion with %s' % mails['member']['pseudo']
thread.title = u'Discussion with %s' % mails['who']['pseudo']
self.storage.set('sluts', int(thread.id), 'status', mails['status'])
self.storage.save()
for mail in mails['messages']:
for mail in mails['results']:
flags = 0
if self.antispam and not self.antispam.check_mail(mail):
self.logger.info('Skipped a spam-mail from %s' % mails['member']['pseudo'])
self.logger.info('Skipped a spam-mail from %s' % mails['who']['pseudo'])
self.report_spam(thread.id)
break
@ -192,31 +193,31 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesPost, ICapDating, ICapCh
flags |= Message.IS_UNREAD
if get_profiles:
if not mail['id_from'] in contacts:
if not mail['from'] in contacts:
with self.browser:
contacts[mail['id_from']] = self.get_contact(mail['id_from'])
if self.antispam and not self.antispam.check_contact(contacts[mail['id_from']]):
self.logger.info('Skipped a spam-mail-profile from %s' % mails['member']['pseudo'])
contacts[mail['from']] = self.get_contact(mail['from'])
if self.antispam and not self.antispam.check_contact(contacts[mail['from']]):
self.logger.info('Skipped a spam-mail-profile from %s' % mails['who']['pseudo'])
self.report_spam(thread.id)
break
if int(mail['id_from']) == self.browser.my_id:
if int(mails['remoteStatus']) == 0 and msg is None:
if int(mail['from']) == self.browser.my_id:
if mails['remote_status'] == 'new' and msg is None:
flags |= Message.IS_NOT_RECEIVED
else:
flags |= Message.IS_RECEIVED
signature = u''
if mail.get('src', None):
signature += u'Sent from my %s\n\n' % mail['src']
if mail['id_from'] in contacts:
signature += contacts[mail['id_from']].get_text()
#if mail.get('src', None):
# signature += u'Sent from my %s\n\n' % mail['src']
if mail['from'] in contacts:
signature += contacts[mail['from']].get_text()
msg = Message(thread=thread,
id=int(time.strftime('%Y%m%d%H%M%S', parse_dt(mail['date']).timetuple())),
title=thread.title,
sender=to_unicode(my_name if int(mail['id_from']) == self.browser.my_id else mails['member']['pseudo']),
receivers=[to_unicode(my_name if int(mail['id_from']) != self.browser.my_id else mails['member']['pseudo'])],
sender=to_unicode(my_name if int(mail['from']) == self.browser.my_id else mails['who']['pseudo']),
receivers=[to_unicode(my_name if int(mail['from']) != self.browser.my_id else mails['who']['pseudo'])],
date=parse_dt(mail['date']),
content=to_unicode(unescape(mail['message'] or '').strip()),
signature=signature,
@ -246,17 +247,17 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesPost, ICapDating, ICapCh
with self.browser:
threads = self.browser.get_threads_list()
for thread in threads:
if thread['member'].get('isBan', thread['member'].get('dead', False)):
with self.browser:
self.browser.delete_thread(int(thread['member']['id']))
continue
#if thread['member'].get('isBan', thread['member'].get('dead', False)):
# with self.browser:
# self.browser.delete_thread(int(thread['member']['id']))
# continue
if self.antispam and not self.antispam.check_thread(thread):
self.logger.info('Skipped a spam-unread-thread from %s' % thread['member']['pseudo'])
self.logger.info('Skipped a spam-unread-thread from %s' % thread['who']['pseudo'])
self.report_spam(thread['member']['id'])
continue
slut = self._get_slut(thread['member']['id'])
if parse_dt(thread['date']) > slut['lastmsg'] or int(thread['status']) != int(slut['status']):
t = self.get_thread(thread['member']['id'], contacts, get_profiles=True)
slut = self._get_slut(thread['who']['id'])
if parse_dt(thread['date']) > slut['lastmsg'] or thread['status'] != slut['status']:
t = self.get_thread(thread['who']['id'], contacts, get_profiles=True)
for m in t.iter_all_messages():
if m.flags & m.IS_UNREAD:
yield m
@ -275,7 +276,7 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesPost, ICapDating, ICapCh
baskets = self.browser.get_baskets()
my_name = self.browser.get_my_name()
for basket in baskets:
if basket['isBan'] or parse_dt(basket['date']) <= slut['lastmsg']:
if parse_dt(basket['date']) <= slut['lastmsg']:
continue
contact = self.get_contact(basket['id'])
if self.antispam and not self.antispam.check_contact(contact):
@ -321,12 +322,12 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesPost, ICapDating, ICapCh
sluts = self.storage.get('sluts')
if not sluts or not id in sluts:
slut = {'lastmsg': datetime.datetime(1970,1,1),
'status': 0}
'status': None}
else:
slut = self.storage.get('sluts', id)
slut['lastmsg'] = slut.get('lastmsg', datetime.datetime(1970,1,1)).replace(tzinfo=tz.tzutc())
slut['status'] = int(slut.get('status', 0))
slut['status'] = slut.get('status', None)
return slut
# ---- ICapMessagesPost methods ---------------------
@ -383,31 +384,24 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesPost, ICapDating, ICapCh
return contact
def _get_partial_contact(self, contact):
if contact.get('isBan', contact.get('dead', False)):
with self.browser:
self.browser.delete_thread(int(contact['id']))
return None
s = 0
if contact.get('isOnline', False):
if contact.get('online', False):
s = Contact.STATUS_ONLINE
else:
s = Contact.STATUS_OFFLINE
c = Contact(contact['id'], to_unicode(contact['pseudo']), s)
c.url = self.browser.id2url(contact['id'])
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 = u'http://s%s.adopteunmec.com/%s%%(type)s%s.jpg' % (contact['shard'], contact['path'], contact['cover'])
if 'age' in contact:
c.status_msg = u'%s old, %s' % (contact['age'], contact['city'])
if contact['cover'] is not None:
url = contact['cover'] + '/%(type)s'
else:
url = u'http://s.adopteunmec.com/www/img/thumb0.gif'
url = u'http://s.adopteunmec.com/www/img/thumb0.jpg'
c.set_photo(u'image%s' % contact['cover'],
url=url % {'type': 'image'},
thumbnail_url=url % {'type': 'thumb0_'})
url=url % {'type': 'full'},
thumbnail_url=url % {'type': 'small'})
return c
def iter_contacts(self, status=Contact.STATUS_ALL, ids=None):
@ -415,7 +409,7 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesPost, ICapDating, ICapCh
threads = self.browser.get_threads_list(count=100)
for thread in threads:
c = self._get_partial_contact(thread['member'])
c = self._get_partial_contact(thread['who'])
if c and (c.status & status) and (not ids or c.id in ids):
yield c
@ -547,7 +541,6 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesPost, ICapDating, ICapCh
StatusField('score', 'Score', self.browser.score()),
StatusField('avcharms', 'Available charms', self.browser.nb_available_charms()),
StatusField('newvisits', 'New visits', self.browser.nb_new_visites()),
StatusField('godchilds', 'Number of godchilds', self.browser.nb_godchilds()),
)
OBJECTS = {Thread: fill_thread,