def create(self, params): # prepare file info params['file_name'] = self.file_name params['file_size'] = self.file_size params['file_md5'] = self.file_md5 = self.checksum_md5file(self.file) self.logger.info('upload file %s, size: %d bytes' % (self.file_name, self.file_size)) self.logger.info('md5 of %s: %s' % (self.file_name, self.file_md5)) params['client_id'] = self.client_id params['access_token'] = self.access_token url = 'https://api.youku.com/uploads/create.json' r = requests.get(url, params=params) check_error(r, 201) result = r.json() self.upload_token = result['upload_token'] self.logger.info('upload token of %s: %s' % (self.file_name, self.upload_token)) self.upload_server_ip = socket.gethostbyname( result['upload_server_uri']) self.logger.info('upload_server_ip of %s: %s' % (self.file_name, self.upload_server_ip))
def cancel(self): status = self.check() params = { 'access_token': self.access_token, 'client_id': self.client_id, 'upload_token': self.upload_token, 'upload_server_ip': status['upload_server_ip'] } url = 'https://api.youku.com/uploads/cancel.json' r = requests.get(url, params=params) check_error(r, 200) self._delete_upload_state_file() return r.json()['upload_token']
def create_file(self): params = { 'upload_token': self.upload_token, 'file_size': self.file_size, # Byte 'ext': self.file_ext, 'slice_length': 2048 # KB } url = 'http://%s/gupload/create_file' % self.upload_server_ip r = requests.post(url, data=params) check_error(r, 201) # save upload state to resume upload self._save_upload_state_to_file()
def upload_slice(self): data = None with open(self.file, 'rb') as f: f.seek(self.slice_offset) data = f.read(self.slice_length) params = { 'upload_token': self.upload_token, 'slice_task_id': self.slice_task_id, 'offset': self.slice_offset, 'length': self.slice_length, # Byte 'hash': self.checksum_md5data(data) } url = 'http://%s/gupload/upload_slice' % self.upload_server_ip r = requests.post(url, params=params, data=data) check_error(r, 201) self._save_slice_state(r.json())
def commit(self): print("youku_upload Message: start commit") status = self.check() if status['status'] == 4: raise ValueError('upload has not complete, should not commit') while status['status'] != 1: # status is 2 or 3 sleep(10) status = self.check() params = { 'access_token': self.access_token, 'client_id': self.client_id, 'upload_token': self.upload_token, 'upload_server_ip': status['upload_server_ip'] } url = 'https://api.youku.com/uploads/commit.json' r = requests.post(url, data=params) check_error(r, 200) self.finished = True self._delete_upload_state_file() return r.json()['video_id']
def spec(self): url = 'https://api.youku.com/schemas/upload/spec.json' r = requests.get(url) check_error(r, 200) return r.json()
def check(self): params = {'upload_token': self.upload_token} url = 'http://%s/gupload/check' % self.upload_server_ip r = requests.get(url, params=params) check_error(r, 200) return r.json()
def new_slice(self): params = {'upload_token': self.upload_token} url = 'http://%s/gupload/new_slice' % self.upload_server_ip r = requests.get(url, params=params) check_error(r, 201) self._save_slice_state(r.json())