347
348
self._publish_tweet(tweet, stream='search/{}'.format(query))
348
349
return self._get_n_rows()
350
# https://dev.twitter.com/docs/api/1.1/get/friends/ids
351
def _getfriendsids(self):
352
"""Get a list of the twitter id's of our twitter friends."""
353
url = self._api_base.format(endpoint="friends/ids")
354
response = self._get_url(url)
358
return response["ids"]
363
# https://dev.twitter.com/docs/api/1.1/get/users/show
364
def _showuser(self, uid):
365
"""Get all the information about a twitter user."""
366
url = self._api_base.format(
367
endpoint='users/show') + '?user_id={}'.format(uid)
368
return self._get_url(url)
370
def _create_contact(self, userdata):
371
"""Build a VCard based on a dict representation of a contact."""
373
if userdata.get('error'):
374
raise FriendsError(userdata)
376
user_fullname = userdata['name']
377
user_nickname = userdata['screen_name']
380
attrs['twitter-id'] = str(userdata['id'])
381
attrs['twitter-name'] = user_fullname
382
attrs['X-URIS'] = 'https://twitter.com/{}'.format(user_nickname)
383
attrs['X-FOLKS-WEB-SERVICES-IDS'] = {
384
'remote-full-name': user_fullname,
385
'twitter-id': str(userdata['id']),
388
return super()._create_contact(user_fullname, user_nickname, attrs)
391
352
def contacts(self):
392
contacts = self._getfriendsids()
393
log.debug('Size of the contacts returned {}'.format(len(contacts)))
394
source = self._get_eds_source()
396
for contact in contacts:
397
twitterid = str(contact)
398
if self._previously_stored_contact(source, 'twitter-id', twitterid):
400
full_contact = self._showuser(twitterid)
402
eds_contact = self._create_contact(full_contact)
405
self._push_to_eds(eds_contact)
353
# https://dev.twitter.com/docs/api/1.1/get/friends/ids
354
contacts = self._get_url(self._api_base.format(endpoint='friends/ids'))
355
# Twitter uses a dict with 'ids' key, Identica returns the ids directly.
356
with ignored(TypeError):
357
contacts = contacts['ids']
359
log.debug('Found {} contacts'.format(len(contacts)))
361
for contact_id in contacts:
362
contact_id = str(contact_id)
363
if not self._previously_stored_contact(contact_id):
364
# https://dev.twitter.com/docs/api/1.1/get/users/show
365
full_contact = self._get_url(url=self._api_base.format(
366
endpoint='users/show') + '?user_id=' + contact_id)
367
user_fullname = full_contact.get('name')
368
user_nickname = full_contact.get('screen_name')
370
'{}-id'.format(self._name): contact_id,
371
'{}-name'.format(self._name): user_fullname,
372
'{}-nick'.format(self._name): user_nickname,
373
'X-URIS': self._user_home.format(user_id=user_nickname),
374
'X-FOLKS-WEB-SERVICES-IDS': {
375
'remote-full-name': user_fullname,
376
'{}-id'.format(self._name): contact_id,
406
378
return len(contacts)
408
def delete_contacts(self):
409
source = self._get_eds_source()
410
return self._delete_service_contacts(source)
413
381
class TweetIdCache(JsonCache):
414
382
"""Persist most-recent tweet_ids as JSON."""