def execute_query2(cls, sql, sql_filepath=None): try: if sql_filepath is None: sql_filepath = os.path.join( path_data, 'tmp', f"{str(time.time()).split('.')[0]}.sql") ToolBaseFile.write(sql, sql_filepath) if platform.system() == 'Windows': tmp = sql_filepath.replace('\\', '\\\\') cmd = f'"{ModelSetting.get("base_bin_sqlite")}" "{ModelSetting.get("base_path_db")}" ".read {tmp}"' for i in range(10): ret = ToolSubprocess.execute_command_return(cmd) if ret.find('database is locked') != -1: time.sleep(5) else: break else: for i in range(10): ret = ToolSubprocess.execute_command_return([ ModelSetting.get('base_bin_sqlite'), ModelSetting.get('base_path_db'), f".read {sql_filepath}" ]) if ret.find('database is locked') != -1: time.sleep(5) else: break return ret except Exception as e: logger.error(f'Exception:{str(e)}') logger.error(traceback.format_exc()) return ''
def create_info_xml(metadata_item, metadata_type): row_ce = Task.source_con.execute( 'SELECT hash, data FROM metadata WHERE hash = ?', (metadata_item['hash'], )) row_ce.row_factory = dict_factory row = row_ce.fetchall() if len(row) == 1: metapath = os.path.join( ModelSetting.get('base_path_metadata'), 'Movies' if metadata_type == 1 else 'TV Shows', metadata_item['hash'][0], f"{metadata_item['hash'][1:]}.bundle", 'Contents', '_combined', 'Info.xml') if os.path.exists(metapath): logger.warning( f"{metadata_item['title']} Info.xml already exist..") else: folder_path = os.path.dirname(metapath) if os.path.exists(folder_path) == False: os.makedirs(folder_path) ToolBaseFile.write(row[0]['data'], metapath) logger.debug(metapath) logger.warning( f"{metadata_item['title']} Info.xml write..") else: logger.warning('info.xml data not exist')
def api(sub): ret = {} try: if sub == 'command_add': filename = request.form['filename'] file_url = request.form['file_url'] logger.debug(filename) logger.debug(file_url) r = requests.get(file_url) download_path = os.path.join(path_data, 'command', filename) update = False if os.path.exists(download_path): os.remove(download_path) update = True ToolBaseFile.write(r.text.replace('\r\n', '\n'), download_path) try: os.system('chmod 777 "%s"' % download_path) except Exception as exception: logger.error('Exception:%s', exception) logger.error(traceback.format_exc()) ret['ret'] = 'success' if update: ret['log'] = u'정상적으로 설치하였습니다.<br>파일을 업데이트 하였습니다.' else: ret['log'] = u'정상적으로 설치하였습니다.' elif sub == 'execute': command_id = request.args.get('id') mode = request.args.get('mode') if mode is None: mode = 'json' kwargs = {} for key, value in request.args.items(): if key in ['apikey', 'mode']: continue if key not in kwargs: kwargs[key] = value ret = LogicNormal.execute_thread_function_job( int(command_id), **kwargs) if mode == 'json': return jsonify(ret) elif mode == 'return': return str(ret) elif mode == 'redirect': return redirect(ret) except Exception as exception: logger.error('Exception:%s', exception) logger.error(traceback.format_exc()) ret['ret'] = 'exception' ret['log'] = str(exception) return jsonify(ret)
def make_nfo_movie(cls, info, output='text', filename='movie.nfo', savepath=None): text = cls._make_nfo_movie(info) if output == 'text': return text elif output == 'xml': return app.response_class(text, mimetype='application/xml') elif output == 'file': from io import StringIO output_stream = StringIO(u'%s' % text) response = Response( output_stream.getvalue().encode('utf-8'), mimetype='application/xml', content_type='application/octet-stream', ) response.headers[ "Content-Disposition"] = "attachment; filename=%s" % filename return response elif output == 'save': if savepath is not None: from tool_base import ToolBaseFile return ToolBaseFile.write(text, savepath)
def execute_query(cls, sql, sql_filepath=None): try: sql += f"{sql}\ncommit;" if sql_filepath is None: sql_filepath = os.path.join( path_data, 'tmp', f"{str(time.time()).split('.')[0]}.sql") ToolBaseFile.write(sql, sql_filepath) if platform.system() == 'Windows': tmp = sql_filepath.replace('\\', '\\\\') cmd = f'"{ModelSetting.get("base_bin_sqlite")}" "{ModelSetting.get("base_path_db")}" ".read {tmp}"' ToolSubprocess.execute_command_return(cmd) else: ret = ToolSubprocess.execute_command_return([ ModelSetting.get('base_bin_sqlite'), ModelSetting.get('base_path_db'), f".read {sql_filepath}" ]) #logger.warning(ret) return True except Exception as e: logger.error(f'Exception:{str(e)}') logger.error(traceback.format_exc()) return False
def ajax(sub): logger.debug('AJAX %s %s', package_name, sub) try: if sub == 'foreground_command': command = request.form['command'] ret = LogicNormal.foreground_command(command) return jsonify(ret) elif sub == 'foreground_command_close': ret = LogicNormal.foreground_command_close() return jsonify(ret) elif sub == 'job_new': ret = {} ret['ret'] = ModelCommand.job_new(request) ret['list'] = ModelCommand.job_list() return jsonify(ret) elif sub == 'job_save': ret = {} ret['ret'] = ModelCommand.job_save(request) ret['list'] = ModelCommand.job_list() return jsonify(ret) elif sub == 'scheduler_switch': ret = {} ret['ret'] = LogicNormal.scheduler_switch0(request) ret['list'] = ModelCommand.job_list() return jsonify(ret) elif sub == 'job_remove': ret = {} ret['ret'] = ModelCommand.job_remove(request) ret['list'] = ModelCommand.job_list() return jsonify(ret) elif sub == 'job_log_show': ret = {} job_id = request.form['job_id'] ret['filename'] = '%s_%s.log' % (package_name, job_id) ret['ret'] = os.path.exists( os.path.join(path_data, 'log', ret['filename'])) return jsonify(ret) elif sub == 'job_background': ret = {} job_id = request.form['job_id'] ret['ret'] = LogicNormal.job_background(job_id) return jsonify(ret) elif sub == 'job_file_edit': ret = {} job_id = request.form['job_id'] job = ModelCommand.get_job_by_id(job_id) ret['data'] = ToolBaseFile.read(job.filename) ret['ret'] = True return jsonify(ret) elif sub == 'file_save': ret = {} job_id = request.form['file_job_id'] logger.debug(job_id) data = request.form['file_textarea'] job = ModelCommand.get_job_by_id(job_id) ToolBaseFile.write(data.replace('\r\n', '\n'), job.filename) ret['ret'] = True return jsonify(ret) elif sub == 'foreground_command_by_job': ret = {} job_id = request.form['job_id'] job = ModelCommand.get_job_by_id(job_id) ret['ret'] = LogicNormal.foreground_command(job.command, job_id=job_id) return jsonify(ret) elif sub == 'process_close': ret = {'ret': 'fail'} job_id = request.form['job_id'] if LogicNormal.process_close( LogicNormal.process_list[int(job_id)]): ret['ret'] = 'success' return jsonify(ret) elif sub == 'send_process_command': ret = LogicNormal.send_process_command(request) return jsonify(ret) elif sub == 'command_list': ret = {} ret['list'] = ModelCommand.job_list() return jsonify(ret) elif sub == 'save': ret = {} ret['ret'] = LogicNormal.save(request) ret['list'] = ModelCommand.job_list() return jsonify(ret) except Exception as exception: logger.error('Exception:%s', exception) logger.error(traceback.format_exc())