327
327
raise FriendsError(str(response))
329
def _fetch_contacts(self):
330
"""Retrieve a list of up to 1,000 Facebook friends."""
332
access_token = self._get_access_token()
333
url = ME_URL + '/friends'
335
access_token=access_token,
337
return self._follow_pagination(url, params, limit)
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()
346
def _create_contact(self, contact_json):
347
"""Build a VCard based on a dict representation of a contact."""
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')
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
366
return super()._create_contact(user_fullname, user_nickname, attrs)
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),
335
log.debug('Found {} contacts'.format(len(contacts)))
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):
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()
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,
385
356
return len(contacts)
387
def delete_contacts(self):
388
source = self._get_eds_source()
389
return self._delete_service_contacts(source)
392
359
class PostIdCache(JsonCache):
393
360
"""Persist most-recent timestamps as JSON."""