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
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()
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')
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')
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)
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()
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)
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)
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)
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)
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')