def _upload(self): last_wake = time() while True: if not self.queue_pending_files: while time() < last_wake + 600 and not self.destroyed: self.new_job.wait() last_wake = time() self.new_job.clear() while self.queue_pending_files: with self.mutex: name = self.queue_pending_files.pop(False) self.dict_files_buffer[name].mutex.acquire() logger.debug(self._post( 'https://c.pcs.baidu.com/rest/2.0/pcs/file', { 'method': 'upload', 'path': self._path(name), 'ondup': 'overwrite'}, {'file': self.dict_files_buffer[name].read( 0, self.size(name))})) self.dict_files_buffer[name].dirty = False self.set_new_files.discard(name) self.dict_files_buffer[name].mutex.release() if self.destroyed: break self.all_jobs_done.set()
def _get(self, base_url, parameters, headers=None): logger.debug( 'bpan: get(base_url={}, parameters={}, headers={})'.format( base_url, parameters, headers)) parameters['access_token'] = self.access_token req = Request('?'.join((base_url, urlencode(parameters)))) if headers: req.headers.update(headers) try: return urlopen(req).read() except HTTPError as e: return e.read()
def _post(self, base_url, parameters, data=b'', headers=None): logger.debug( 'bpan: post(base_url={}, parameters={}, headers={})'.format( base_url, parameters, headers)) if self.dry_run: logger.debug('bpan: dry_run') return b'' parameters['access_token'] = self.access_token if data: data, boundary = encode_multipart(data) req = Request( '?'.join((base_url, urlencode(parameters))), data.encode('ISO-8859-1')) req.add_header( 'Content-Type', 'multipart/form-data; boundary=%s' % boundary) else: req = Request( '?'.join((base_url, urlencode(parameters))), data) if headers: req.headers.update(headers) try: return urlopen(req).read() except HTTPError as e: return e.read()