~super-friends/friends/13.10

« back to all changes in this revision

Viewing changes to friends/protocols/facebook.py

  • Committer: Tarmac
  • Author(s): Robert Bruce Park
  • Date: 2013-07-22 20:34:58 UTC
  • mfrom: (224.1.12 simplify-contacts)
  • Revision ID: tarmac-20130722203458-dwbs94fsik5iydt0
Vast simplification of contact logic.

Approved by Ken VanDine, PS Jenkins bot, Robert Bruce Park.

Show diffs side-by-side

added added

removed removed

Lines of Context:
326
326
        else:
327
327
            raise FriendsError(str(response))
328
328
 
329
 
    def _fetch_contacts(self):
330
 
        """Retrieve a list of up to 1,000 Facebook friends."""
331
 
        limit = 1000
332
 
        access_token = self._get_access_token()
333
 
        url = ME_URL + '/friends'
334
 
        params = dict(
335
 
            access_token=access_token,
336
 
            limit=limit)
337
 
        return self._follow_pagination(url, params, limit)
338
 
 
339
 
    def _fetch_contact(self, contact_id):
340
 
        """Fetch the full, individual contact info."""
341
 
        access_token = self._get_access_token()
342
 
        url = API_BASE.format(id=contact_id)
343
 
        params = dict(access_token=access_token)
344
 
        return Downloader(url, params).get_json()
345
 
 
346
 
    def _create_contact(self, contact_json):
347
 
        """Build a VCard based on a dict representation of a contact."""
348
 
 
349
 
        user_id = contact_json.get('id')
350
 
        user_fullname = contact_json.get('name')
351
 
        user_nickname = contact_json.get('username')
352
 
        user_link = contact_json.get('link')
353
 
        gender = contact_json.get('gender')
354
 
 
355
 
        attrs = {}
356
 
        attrs['facebook-id'] = user_id
357
 
        attrs['facebook-name'] = user_fullname
358
 
        attrs['X-URIS'] = user_link
359
 
        attrs['X-FOLKS-WEB-SERVICES-IDS'] = {
360
 
            'jabber':'-{}@chat.facebook.com'.format(user_id),
361
 
            'remote-full-name':user_fullname,
362
 
            'facebook-id': user_id}
363
 
        if gender is not None:
364
 
            attrs['X-GENDER'] = gender
365
 
 
366
 
        return super()._create_contact(user_fullname, user_nickname, attrs)
367
 
 
368
329
    @feature
369
330
    def contacts(self):
370
 
        contacts = self._fetch_contacts()
371
 
        log.debug('Size of the contacts returned {}'.format(len(contacts)))
372
 
        source = self._get_eds_source()
 
331
        contacts = self._follow_pagination(
 
332
            url=ME_URL + '/friends',
 
333
            params=dict(access_token=self._get_access_token(), limit=1000),
 
334
            limit=1000)
 
335
        log.debug('Found {} contacts'.format(len(contacts)))
373
336
 
374
337
        for contact in contacts:
375
 
            if self._previously_stored_contact(
376
 
                    source, 'facebook-id', contact['id']):
 
338
            contact_id = contact.get('id')
 
339
            if contact_id is None or self._previously_stored_contact(contact_id):
377
340
                continue
378
 
            log.debug(
379
 
                'Fetch full contact info for {} and id {}'.format(
380
 
                    contact['name'], contact['id']))
381
 
            full_contact = self._fetch_contact(contact['id'])
382
 
            eds_contact = self._create_contact(full_contact)
383
 
            self._push_to_eds(eds_contact)
 
341
            full_contact = Downloader(
 
342
                url=API_BASE.format(id=contact_id),
 
343
                params=dict(access_token=self._get_access_token())).get_json()
 
344
            self._push_to_eds({
 
345
                'facebook-id':   contact_id,
 
346
                'facebook-name': full_contact.get('name'),
 
347
                'facebook-nick': full_contact.get('username'),
 
348
                'X-URIS':        full_contact.get('link'),
 
349
                'X-GENDER':      full_contact.get('gender'),
 
350
                'X-FOLKS-WEB-SERVICES-IDS': {
 
351
                    'jabber': '-{}@chat.facebook.com'.format(contact_id),
 
352
                    'remote-full-name': full_contact.get('name'),
 
353
                    'facebook-id': contact_id,
 
354
                }})
384
355
 
385
356
        return len(contacts)
386
357
 
387
 
    def delete_contacts(self):
388
 
        source = self._get_eds_source()
389
 
        return self._delete_service_contacts(source)
390
 
 
391
358
 
392
359
class PostIdCache(JsonCache):
393
360
    """Persist most-recent timestamps as JSON."""