~super-friends/friends/13.10

« back to all changes in this revision

Viewing changes to friends/protocols/linkedin.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:
26
26
from friends.utils.base import Base, feature
27
27
from friends.utils.http import Downloader
28
28
from friends.utils.time import iso8601utc
29
 
from friends.errors import FriendsError
30
29
 
31
30
 
32
31
log = logging.getLogger(__name__)
33
32
 
34
33
 
 
34
def make_fullname(firstName=None, lastName=None, **ignored):
 
35
    """Converts dict(firstName='Bob', lastName='Loblaw') into 'Bob Loblaw'."""
 
36
    return ' '.join(name for name in (firstName, lastName) if name)
 
37
 
 
38
 
35
39
class LinkedIn(Base):
36
40
    _api_base = ('https://api.linkedin.com/v1/{endpoint}?format=json' +
37
41
                 '&secure-urls=true&oauth2_access_token={token}')
44
48
            token=self._get_access_token())
45
49
        result = Downloader(url).get_json()
46
50
        self._account.user_id = result.get('id')
47
 
        self._account.user_name = '{firstName} {lastName}'.format(**result)
 
51
        self._account.user_name = make_fullname(**result)
48
52
 
49
53
    def _publish_entry(self, entry, stream='messages'):
50
54
        """Publish a single update into the Dee.SharedModel."""
52
56
 
53
57
        content = entry.get('updateContent', {})
54
58
        person = content.get('person', {})
55
 
        name = '{firstName} {lastName}'.format(**person)
 
59
        name = make_fullname(**person)
56
60
        person_id = person.get('id', '')
57
61
        status = person.get('currentStatus')
58
62
        picture = person.get('pictureUrl', '')
98
102
        """Gather and publish all incoming messages."""
99
103
        return self.home()
100
104
 
101
 
    def _create_contact(self, connection_json):
102
 
        """Build a VCard based on a dict representation of a contact."""
103
 
        user_id = connection_json.get('id', '')
104
 
 
105
 
        user_fullname = '{firstName} {lastName}'.format(**connection_json)
106
 
        user_link = connection_json.get(
107
 
            'siteStandardProfileRequest', {}).get('url', '')
108
 
 
109
 
        attrs = { 'linkedin-id':   user_id,
110
 
                  'linkedin-name': user_fullname,
111
 
                  'X-URIS':        user_link }
112
 
 
113
 
        return super()._create_contact(user_fullname, None, attrs)
114
 
 
115
105
    @feature
116
106
    def contacts(self):
117
107
        """Retrieve a list of up to 500 LinkedIn connections."""
118
108
        # http://developer.linkedin.com/documents/connections-api
119
 
        url = self._api_base.format(
120
 
            endpoint='people/~/connections',
121
 
            token=self._get_access_token())
122
 
        result = Downloader(url).get_json()
123
 
        connections = result.get('values', [])
124
 
        source = self._get_eds_source()
 
109
        connections = Downloader(
 
110
            url=self._api_base.format(
 
111
                endpoint='people/~/connections',
 
112
                token=self._get_access_token())
 
113
        ).get_json().get('values', [])
125
114
 
126
115
        for connection in connections:
127
 
            connection_id = connection.get('id')
128
 
            if connection_id != 'private':
129
 
                if not self._previously_stored_contact(
130
 
                        source, 'linkedin-id', connection_id):
131
 
                    self._push_to_eds(self._create_contact(connection))
 
116
            connection_id = connection.get('id', 'private')
 
117
            fullname = make_fullname(**connection)
 
118
            if connection_id != 'private' and not self._previously_stored_contact(connection_id):
 
119
                self._push_to_eds({
 
120
                    'linkedin-id': connection_id,
 
121
                    'linkedin-name': fullname,
 
122
                    'X-URIS': connection.get(
 
123
                        'siteStandardProfileRequest', {}).get('url'),
 
124
                    'X-FOLKS-WEB-SERVICES-IDS': {
 
125
                        'remote-full-name': fullname,
 
126
                        'linkedin-id': connection_id,
 
127
                    }})
132
128
 
133
129
        return len(connections)
134
 
 
135
 
    def delete_contacts(self):
136
 
        source = self._get_eds_source()
137
 
        return self._delete_service_contacts(source)