def post(self): username = self.request.get('username') password = self.request.get('password') logging.debug('Exporting bookmarks using Basic Auth for %s' % username) encoded_credentials = base64.encodestring( '%s:%s' % (username, password))[:-1] # The v1 API seems to be more prone to taking longer to respond (or is # it that v1 users tend to have more bookmarks?), so to avoid hitting # the 10 second HTTP urlfetch timeout limit, we chunk the data that is # requested chunk_start = 0 chunk_size = 500 bookmarks = [] while True: logging.debug(' Fetching chunk from %d' % chunk_start) result = urlfetch.fetch( url='https://api.del.icio.us/v1/posts/all?results=%d&start=%d' % (chunk_size, chunk_start), method=urlfetch.GET, deadline=60, headers={'Authorization': 'Basic %s' % encoded_credentials}) if result.status_code != 200: self._handle_error(result) return chunk_bookmarks = parse_bookmarks_xml(result.content) bookmarks.extend(chunk_bookmarks) logging.debug(' Got %d bookmarks in chunk' % len(chunk_bookmarks)) if len(chunk_bookmarks) != chunk_size: break chunk_start += chunk_size self._output_export_form(bookmarks)
def _handle_xml_export(self, oauthapp): url = 'http://api.del.icio.us/v2/posts/all' signing_request = oauthlib.oauth.OAuthRequest.from_consumer_and_token( oauthapp.consumer, token=oauthapp.token, http_method='GET', http_url=url) signing_request.sign_request( oauthapp.signature_method_hmac_sha1, oauthapp.consumer, oauthapp.token) headers = signing_request.to_header('yahooapis.com') result = urlfetch.fetch( url=url, method=urlfetch.GET, deadline=60, headers=headers) if result.status_code != 200: self._handle_error(result) return bookmarks = parse_bookmarks_xml(result.content) self._output_export_form(bookmarks)