예제 #1
0
    def receive(self):
        """Gets a list of each friend's most recent check-ins."""
        token = self._get_access_token()

        result = Downloader(RECENT_URL.format(access_token=token)).get_json()

        response_code = result.get('meta', {}).get('code')
        if response_code != 200:
            raise FriendsError('FourSquare: Error: {}'.format(result))

        checkins = result.get('response', {}).get('recent', [])
        for checkin in checkins:
            user = checkin.get('user', {})
            avatar = user.get('photo', {})
            checkin_id = checkin.get('id', '')
            tz_offset = checkin.get('timeZoneOffset', 0)
            epoch = checkin.get('createdAt', 0)
            venue = checkin.get('venue', {})
            location = venue.get('location', {})
            self._publish(
                message_id=checkin_id,
                stream='messages',
                sender=_full_name(user),
                from_me=(user.get('relationship') == 'self'),
                timestamp=iso8601utc(epoch, tz_offset),
                message=checkin.get('shout', ''),
                likes=checkin.get('likes', {}).get('count', 0),
                icon_uri='{prefix}100x100{suffix}'.format(**avatar),
                url=venue.get('canonicalUrl', ''),
                location=venue.get('name', ''),
                latitude=location.get('lat', 0.0),
                longitude=location.get('lng', 0.0),
                )
        return self._get_n_rows()
예제 #2
0
    def _follow_pagination(self, url, params, limit=None):
        """Follow Facebook's pagination until we hit the limit."""
        limit = limit or self._DOWNLOAD_LIMIT
        entries = []

        while True:
            response = Downloader(url, params).get_json()

            if self._is_error(response):
                break

            data = response.get('data')
            if data is None:
                break

            entries.extend(data)
            if len(entries) >= limit:
                break

            # We haven't gotten the requested number of entries.  Follow the
            # next page if there is one to try to get more.
            pages = response.get('paging')
            if pages is None:
                break

            # The 'next' key has the full link to follow; no additional
            # parameters are needed.  Specifically, this link will already
            # include the access_token, and any since/limit values.
            url = pages.get('next')
            params = None
            if url is None:
                break

        # We've gotten everything Facebook is going to give us.
        return entries
예제 #3
0
 def _whoami(self, authdata):
     """Identify the authenticating user."""
     data = Downloader(
         SELF_URL.format(access_token=self._account.access_token)).get_json()
     user = data.get('response', {}).get('user', {})
     self._account.secret_token = authdata.get('TokenSecret')
     self._account.user_name = _full_name(user)
     self._account.user_id = user.get('id')
예제 #4
0
 def _whoami(self, authdata):
     """Identify the authenticating user."""
     url = self._api_base.format(
         endpoint='users/self',
         token=self._get_access_token())
     result = Downloader(url).get_json()
     self._account.user_id = result.get('data').get('id')
     self._account.user_name = result.get('data').get('username')
예제 #5
0
 def home(self):
     """Gather and publish public timeline messages."""
     url = self._api_base.format(
         endpoint='people/~/network/updates',
         token=self._get_access_token()) + '&type=STAT'
     result = Downloader(url).get_json()
     for update in result.get('values', []):
         self._publish_entry(update)
     return self._get_n_rows()
예제 #6
0
 def home(self):
     """Gather and publish public timeline messages."""
     url = self._api_base.format(
         endpoint='users/self/feed',
         token=self._get_access_token())
     result = Downloader(url).get_json()
     values = result.get('data', {})
     for update in values:
         self._publish_entry(update)
예제 #7
0
 def _whoami(self, authdata):
     """Identify the authenticating user."""
     # http://developer.linkedin.com/documents/profile-fields
     url = self._api_base.format(
         endpoint='people/~:(id,first-name,last-name)',
         token=self._get_access_token())
     result = Downloader(url).get_json()
     self._account.user_id = result.get('id')
     self._account.user_name = make_fullname(**result)
예제 #8
0
 def make(self, url):
     """Shorten the URL by querying the shortening service."""
     if Short.already(url):
         # Don't re-shorten an already-short URL.
         return url
     return Downloader(
         self.template.format(quote(url, safe=''))).get_string().strip()
예제 #9
0
    def get_image(url):
        if not url:
            return url
        local_path = Avatar.get_path(url)
        size = 0

        with ignored(FileNotFoundError):
            size = os.stat(local_path).st_size

        if size == 0:
            log.debug('Getting: {}'.format(url))
            image_data = Downloader(url).get_bytes()

            # Save original size at canonical URI
            with open(local_path, 'wb') as fd:
                fd.write(image_data)

            # Append '.100px' to filename and scale image there.
            input_stream = Gio.MemoryInputStream.new_from_data(
                image_data, None)
            try:
                pixbuf = GdkPixbuf.Pixbuf.new_from_stream_at_scale(
                    input_stream, 100, 100, True, None)
                pixbuf.savev(local_path + '.100px', 'png', [], [])
            except GLib.GError:
                log.error('Failed to scale image: {}'.format(url))
        return local_path
예제 #10
0
    def _send(self, obj_id, message, endpoint, stream='messages'):
        """Used for posting a message or comment."""
        token = self._get_access_token()

        url = self._api_base.format(endpoint=endpoint, token=token)

        result = Downloader(
            url,
            method='POST',
            params=dict(access_token=token, text=message)).get_json()
        new_id = result.get('id')
        if new_id is None:
            raise FriendsError(
                'Failed sending to Instagram: {!r}'.format(result))
        url = self._api_base.format(endpoint=endpoint, token=token)
        comment = Downloader(url, params=dict(access_token=token)).get_json()
        return self._publish_entry(entry=comment, stream=stream)
예제 #11
0
    def _like(self, obj_id, method):
        url = API_BASE.format(id=obj_id) + '/likes'
        token = self._get_access_token()

        if not Downloader(url, method=method,
                          params=dict(access_token=token)).get_json():
            raise FriendsError('Failed to {} like {} on Facebook'.format(
                method, obj_id))
예제 #12
0
    def _send(self, obj_id, message, endpoint, stream='messages'):
        url = API_BASE.format(id=obj_id) + endpoint
        token = self._get_access_token()

        result = Downloader(
            url,
            method='POST',
            params=dict(access_token=token, message=message)).get_json()
        new_id = result.get('id')
        if new_id is None:
            raise FriendsError('Failed sending to Facebook: {!r}'.format(result))

        url = API_BASE.format(id=new_id)
        entry = Downloader(url, params=dict(access_token=token)).get_json()
        return self._publish_entry(
            stream=stream,
            entry=entry)
예제 #13
0
 def test_headers(self):
     # Provide some additional headers.
     self.assertEqual(
         Downloader('http://localhost:9180/headers',
                    headers={
                        'X-Foo': 'baz',
                        'X-Bar': 'foo'
                    }).get_json(), dict(foo='baz', bar='foo'))
예제 #14
0
    def _like(self, obj_id, endpoint, method):
        """Used for liking or unliking an object."""
        token = self._get_access_token()
        url = self._api_base.format(endpoint=endpoint, token=token)

        if not Downloader(
                url,
                method=method,
                params=dict(access_token=token)).get_json():
            raise FriendsError(
                'Failed to {} like {} on Instagram'.format(
                    method, obj_id))
예제 #15
0
    def delete(self, obj_id):
        """Delete any Facebook object that you are the owner of."""
        url = API_BASE.format(id=obj_id)
        token = self._get_access_token()

        if not Downloader(url, method='DELETE',
                          params=dict(access_token=token)).get_json():
            raise FriendsError('Failed to delete {} on Facebook'.format(obj_id))
        else:
            self._unpublish(obj_id)

        return obj_id
예제 #16
0
    def _get_url(self, params=None):
        """Access the Flickr API with correct OAuth signed headers."""
        method = 'GET'
        headers = self._get_oauth_headers(
            method=method,
            url='{}?{}'.format(REST_SERVER, urlencode(params)),
            )

        response = Downloader(
            REST_SERVER,
            params=params,
            headers=headers,
            method=method,
            ).get_json()
        self._is_error(response)
        return response
예제 #17
0
    def contacts(self):
        """Retrieve a list of up to 500 LinkedIn connections."""
        # http://developer.linkedin.com/documents/connections-api
        connections = Downloader(url=self._api_base.format(
            endpoint='people/~/connections',
            token=self._get_access_token())).get_json().get('values', [])

        for connection in connections:
            uid = connection.get('id', 'private')
            fullname = make_fullname(**connection)
            if uid != 'private' and not self._previously_stored_contact(uid):
                self._push_to_eds(uid=uid,
                                  name=fullname,
                                  link=connection.get(
                                      'siteStandardProfileRequest',
                                      {}).get('url'))

        return len(connections)
예제 #18
0
    def _get_url(self, url, data=None):
        """Access the Twitter API with correct OAuth signed headers."""
        do_post = data is not None
        method = 'POST' if do_post else 'GET'

        headers = self._get_oauth_headers(
            method=method,
            url=url,
            data=data,
        )

        response = Downloader(url,
                              params=data,
                              headers=headers,
                              method=method,
                              rate_limiter=self._rate_limiter).get_json()
        self._is_error(response)
        return response
예제 #19
0
    def contacts(self):
        access_token=self._get_access_token()
        contacts = self._follow_pagination(
            url=ME_URL + '/friends',
            params=dict(access_token=access_token, limit=1000),
            limit=1000)
        log.debug('Found {} contacts'.format(len(contacts)))

        for contact in contacts:
            contact_id = contact.get('id')
            if not self._previously_stored_contact(contact_id):
                full_contact = Downloader(
                    url=API_BASE.format(id=contact_id),
                    params=dict(access_token=access_token)).get_json()
                self._push_to_eds(
                    uid=contact_id,
                    name=full_contact.get('name'),
                    nick=full_contact.get('username'),
                    link=full_contact.get('link'),
                    gender=full_contact.get('gender'),
                    jabber='-{}@chat.facebook.com'.format(contact_id))

        return len(contacts)
예제 #20
0
 def test_simple_text_download(self):
     # Test simple downloading of text data.
     self.assertEqual(
         Downloader('http://localhost:9180/text').get_string(),
         'hello world')
예제 #21
0
 def test_simple_json_download(self):
     # Test simple downloading of JSON data.
     self.assertEqual(
         Downloader('http://localhost:9180/json').get_json(),
         dict(answer='hello'))
예제 #22
0
 def test_json_implicit_utf_32be(self):
     # RFC 4627 $3 with implicit charset=utf-32be.
     self.assertEqual(
         Downloader('http://example.com').get_json(), dict(yes='ÑØ'))
예제 #23
0
 def test_mirror(self):
     self.assertEqual(
         Downloader('http://localhost:9180/mirror',
                    method='POST',
                    params=dict(foo='bar')).get_string(), 'foo=bar')
예제 #24
0
 def test_params_post(self):
     # Test posting data.
     self.assertEqual(
         Downloader('http://localhost:9180/post',
                    params=dict(one=1, two=2, three=3),
                    method='POST').get_json(), dict(one=1, two=2, three=3))
예제 #25
0
 def test_params_get(self):
     # Test getting with query string URL.
     self.assertEqual(
         Downloader('http://localhost:9180/post',
                    params=dict(one=1, two=2, three=3),
                    method='GET').get_json(), dict(one=1, two=2, three=3))
예제 #26
0
 def _whoami(self, authdata):
     """Identify the authenticating user."""
     me_data = Downloader(
         ME_URL, dict(access_token=self._account.access_token)).get_json()
     self._account.user_id = me_data.get('id')
     self._account.user_name = me_data.get('name')
예제 #27
0
 def test_simple_bytes_download(self):
     # Test simple downloading of bytes data.
     bytes_data = Downloader('http://localhost:9180/bytes').get_bytes()
     self.assertIsInstance(bytes_data, bytes)
     self.assertEqual(list(bytes_data), [241, 87, 240, 13])