~super-friends/friends/13.10

« back to all changes in this revision

Viewing changes to friends/protocols/twitter.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:
61
61
    _favorite = _api_base.format(endpoint='favorites/create')
62
62
    _del_favorite = _api_base.format(endpoint='favorites/destroy')
63
63
 
64
 
    _tweet_permalink = 'https://twitter.com/{user_id}/status/{tweet_id}'
 
64
    _user_home = 'https://twitter.com/{user_id}'
 
65
    _tweet_permalink = _user_home + '/status/{tweet_id}'
65
66
 
66
67
    def __init__(self, account):
67
68
        super().__init__(account)
347
348
            self._publish_tweet(tweet, stream='search/{}'.format(query))
348
349
        return self._get_n_rows()
349
350
 
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)
355
 
 
356
 
        try:
357
 
            # Twitter
358
 
            return response["ids"]
359
 
        except TypeError:
360
 
            # Identica
361
 
            return response
362
 
 
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)
369
 
 
370
 
    def _create_contact(self, userdata):
371
 
        """Build a VCard based on a dict representation of a contact."""
372
 
 
373
 
        if userdata.get('error'):
374
 
            raise FriendsError(userdata)
375
 
 
376
 
        user_fullname = userdata['name']
377
 
        user_nickname = userdata['screen_name']
378
 
 
379
 
        attrs = {}
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']),
386
 
            }
387
 
 
388
 
        return super()._create_contact(user_fullname, user_nickname, attrs)
389
 
 
390
351
    @feature
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()
395
 
 
396
 
        for contact in contacts:
397
 
            twitterid = str(contact)
398
 
            if self._previously_stored_contact(source, 'twitter-id', twitterid):
399
 
                continue
400
 
            full_contact = self._showuser(twitterid)
401
 
            try:
402
 
                eds_contact = self._create_contact(full_contact)
403
 
            except FriendsError:
404
 
                continue
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']
 
358
 
 
359
        log.debug('Found {} contacts'.format(len(contacts)))
 
360
 
 
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')
 
369
                self._push_to_eds({
 
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,
 
377
                    }})
406
378
        return len(contacts)
407
379
 
408
 
    def delete_contacts(self):
409
 
        source = self._get_eds_source()
410
 
        return self._delete_service_contacts(source)
411
 
 
412
380
 
413
381
class TweetIdCache(JsonCache):
414
382
    """Persist most-recent tweet_ids as JSON."""