def uplaod_rating_thread(url, ratings, headers): for rating_name, score in ratings: if (score != -1 and score != 0): rating_url = url + rating_name + '/' data = { "score": score, # todo this kind of mixing is too much. Should have 2 bkit structures, upload, use } try: r = rerequests.put(rating_url, data=data, verify=True, headers=headers) except requests.exceptions.RequestException as e: print('ratings upload failed: %s' % str(e))
def upload_rating_thread(url, ratings, headers): ''' Upload rating thread function / disconnected from blender data.''' bk_logger.debug('upload rating ' + url + str(ratings)) for rating_name, score in ratings: if (score != -1 and score != 0): rating_url = url + rating_name + '/' data = { "score": score, # todo this kind of mixing is too much. Should have 2 bkit structures, upload, use } try: r = rerequests.put(rating_url, data=data, verify=True, headers=headers) except requests.exceptions.RequestException as e: print('ratings upload failed: %s' % str(e))
def start_upload(self, context, asset_type, reupload, upload_set): '''start upload process, by processing data''' # fix the name first utils.name_update() props = utils.get_upload_props() storage_quota_ok = check_storage_quota(props) if not storage_quota_ok: self.report({'ERROR_INVALID_INPUT'}, props.report) return {'CANCELLED'} location = get_upload_location(props) props.upload_state = 'preparing upload' auto_fix(asset_type=asset_type) # do this for fixing long tags in some upload cases props.tags = props.tags[:] props.name = props.name.strip() # TODO move this to separate function # check for missing metadata if asset_type == 'MODEL': get_missing_data_model(props) if asset_type == 'SCENE': get_missing_data_scene(props) elif asset_type == 'MATERIAL': get_missing_data_material(props) elif asset_type == 'BRUSH': get_missing_data_brush(props) if props.report != '': self.report({'ERROR_INVALID_INPUT'}, props.report) return {'CANCELLED'} if not reupload: props.asset_base_id = '' props.id = '' export_data, upload_data, eval_path_computing, eval_path_state, eval_path, props = get_upload_data(self, context, asset_type) # utils.pprint(upload_data) upload_data['parameters'] = params_to_dict( upload_data['parameters']) # weird array conversion only for upload, not for tooltips. binary_path = bpy.app.binary_path script_path = os.path.dirname(os.path.realpath(__file__)) basename, ext = os.path.splitext(bpy.data.filepath) # if not basename: # basename = os.path.join(basename, "temp") if not ext: ext = ".blend" tempdir = tempfile.mkdtemp() source_filepath = os.path.join(tempdir, "export_blenderkit" + ext) clean_file_path = paths.get_clean_filepath() data = { 'clean_file_path': clean_file_path, 'source_filepath': source_filepath, 'temp_dir': tempdir, 'export_data': export_data, 'upload_data': upload_data, 'debug_value': bpy.app.debug_value, 'upload_set': upload_set, } datafile = os.path.join(tempdir, BLENDERKIT_EXPORT_DATA_FILE) # check if thumbnail exists: if 'THUMBNAIL' in upload_set: if not os.path.exists(export_data["thumbnail_path"]): props.upload_state = 'Thumbnail not found' props.uploading = False return {'CANCELLED'} # first upload metadata to server, so it can be saved inside the current file url = paths.get_api_url() + 'assets/' headers = utils.get_headers(upload_data['token']) # upload_data['license'] = 'ovejajojo' json_metadata = upload_data # json.dumps(upload_data, ensure_ascii=False).encode('utf8') global reports if props.asset_base_id == '': try: r = rerequests.post(url, json=json_metadata, headers=headers, verify=True, immediate=True) # files = files, ui.add_report('uploaded metadata') utils.p(r.text) except requests.exceptions.RequestException as e: print(e) props.upload_state = str(e) props.uploading = False return {'CANCELLED'} else: url += props.id + '/' try: if upload_set != ['METADATA']: json_metadata["verificationStatus"] = "uploading" r = rerequests.put(url, json=json_metadata, headers=headers, verify=True, immediate=True) # files = files, ui.add_report('uploaded metadata') # parse the request # print('uploaded metadata') # print(r.text) except requests.exceptions.RequestException as e: print(e) props.upload_state = str(e) props.uploading = False return {'CANCELLED'} # props.upload_state = 'step 1' if upload_set == ['METADATA']: props.uploading = False props.upload_state = 'upload finished successfully' return {'FINISHED'} try: rj = r.json() utils.pprint(rj) # if r.status_code not in (200, 201): # if r.status_code == 401: # ui.add_report(r.detail, 5, colors.RED) # return {'CANCELLED'} if props.asset_base_id == '': props.asset_base_id = rj['assetBaseId'] props.id = rj['id'] upload_data['assetBaseId'] = props.asset_base_id upload_data['id'] = props.id # bpy.ops.wm.save_mainfile() # bpy.ops.wm.save_as_mainfile(filepath=filepath, compress=False, copy=True) props.uploading = True # save a copy of actual scene but don't interfere with the users models bpy.ops.wm.save_as_mainfile(filepath=source_filepath, compress=False, copy=True) with open(datafile, 'w') as s: json.dump(data, s) proc = subprocess.Popen([ binary_path, "--background", "-noaudio", clean_file_path, "--python", os.path.join(script_path, "upload_bg.py"), "--", datafile # ,filepath, tempdir ], bufsize=5000, stdout=subprocess.PIPE, stdin=subprocess.PIPE) bg_blender.add_bg_process(eval_path_computing=eval_path_computing, eval_path_state=eval_path_state, eval_path=eval_path, process_type='UPLOAD', process=proc, location=location) except Exception as e: props.upload_state = str(e) props.uploading = False print(e) return {'CANCELLED'} return {'FINISHED'}
def upload_review_thread(url, reviews, headers): r = rerequests.put(url, data=reviews, verify=True, headers=headers)