def edit(request_type): file_url = select_type(request_type) is_menu = False if request_type == "menu": is_menu = True if file_url is None: abort(404) if request.json is None: abort(400) page_list = json.loads(file.read_file(file_url)) post_uuid = str(request.json["post_uuid"]) post_index = get_index(page_list, post_uuid) name = str(request.json["name"]).replace('/', "").strip() title = str(request.json["title"]).strip() content = str(request.json["content"]) sign = str(request.json["sign"]) send_time = int(request.json["send_time"]) status = False hash_content = post_uuid + title + name + hashlib.sha512( str(content).encode('utf-8')).hexdigest() if check_password(hash_content, sign, send_time): status = True config = {"name": name, "title": title} post_manage.edit_post(page_list, post_index, config, None, is_menu) file.write_file("./document/{0}.md".format(name), content) post_manage.update_post() build_rss.build_rss() result = {"status": status, "name": name} return jsonify(result)
def upgrade_pull(): if not os.path.exists("./.git"): console.log("Error", "Not a git repository.") return False repo, remote = git_init() console.log("Info", "On branch {}".format(repo.active_branch)) if repo.is_dirty(): console.log("Error", "The current warehouse is modified and can not be upgraded automatically.") checkout_repo = input('Do you want to restore these changes? [y/N]') if checkout_repo.lower() == 'yes' or checkout_repo.lower() == 'y': repo.index.checkout(force=True) if repo.is_dirty(): exit(1) remote.pull() if current_env_version != new_env_version: os.system("cd ./install && bash install_python_dependency.sh") file.write_file("./upgrade/current_version.json", json.dumps({"current_data_version": new_data_version, "current_env_version": new_env_version})) if current_data_version != new_data_version and os.path.exists( "./upgrade/upgrade_from_{}.py".format(current_data_version)): os.system("python3 ./upgrade/upgrade_from_{}.py".format(current_data_version)) file.write_file("./upgrade/current_version.json", json.dumps({"current_data_version": new_data_version, "current_env_version": new_env_version})) console.log("Success", "Upgrade Successful!") exit(0)
def new_post_init(config, independent=False): title = config["title"] name = get.filter_name(config["name"]) if not os.path.exists("./document/{}.md".format(name)): editor = system_info["Editor"] os.system("{0} ./document/{1}.md".format(editor, name)) post_info = {"name": name, "title": title, "time": time.time()} if not os.path.exists("./document/{}.md".format(name)): console.log("Error", "Cannot find [./document/{}.md] file".format(name)) return if not independent: excerpt = get.get_excerpt("./document/{}.md".format(name)) post_info["excerpt"] = excerpt write_json = post_info page_config = "./document/{}.json".format(name) if not independent: write_json = json.loads(file.read_file("./config/page.json")) write_json.insert(0, post_info) page_config = "./config/page.json" file.write_file( page_config, json.dumps(write_json, indent=4, sort_keys=False, ensure_ascii=False)) console.log("Success", "Create a new article successfully!")
def create_post(): if request.json is None: abort(400) title = str(request.json["title"]).strip() name = str(request.json["name"]).replace('/', "").strip() content = str(request.json["content"]) sign = str(request.json["sign"]) send_time = int(request.json["send_time"]) status = False hash_content = title + name + hashlib.sha512( str(content).encode('utf-8')).hexdigest() post_uuid = "" if check_password(hash_content, sign, send_time): if len(name) == 0: pinyin = True if 'Pinyin' in system_config: pinyin = system_config["Pinyin"] name = get.get_name(title, pinyin) while os.path.exists("./document/{0}.md".format(name)): name = "{}-repeat".format(name) post_uuid = str(uuid.uuid5(uuid.NAMESPACE_URL, name)) file.write_file("./document/{0}.md".format(name), content) config = {"title": title, "name": name, "uuid": post_uuid} post_manage.new_post(config) status = True build_rss.build_rss() result = {"status": status, "name": name, "uuid": post_uuid} return jsonify(result)
def update(): global page_list, page_list_file page_list_file = json.loads(file.read_file("./config/page.json")) if len(page_list_file) == 0: console.log("Error", "The page list can not be blank.") return False page_list = page_list_file for item in page_list_file: processing_file = "./document/{0}.md".format(item["name"]) file_exists = os.path.exists(processing_file) if not file_exists: console.log("Remove", "Removing from list: {0}".format(processing_file)) del page_list[item] loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) tasks = [build_excerpt(item) for item in page_list] loop.run_until_complete(asyncio.wait(tasks)) loop.close() file.write_file( "./config/page.json", json.dumps(page_list, ensure_ascii=False, indent=4, sort_keys=False)) console.log("Success", "Update article metadata is successful!") return True
def edit(request_type): file_url = select_type(request_type) is_menu = False if request_type == "menu": is_menu = True if file_url is None: abort(404) if request.json is None: abort(400) page_list = json.loads(file.read_file(file_url)) post_id = int(request.json["post_id"]) name = str(request.json["name"]).replace('/', "").strip() title = str(request.json["title"]).strip() content = str(request.json["content"]) sign = str(request.json["sign"]) status = False if page_list[post_id].get("lock", False): return json.dumps({"status": False}) if check_password(title, sign): status = True config = {"name": name, "title": title} post_manage.edit_post(page_list, post_id, config, None, is_menu) file.write_file("./document/{0}.md".format(name), content) post_manage.update_post() build_rss.build_rss() return json.dumps({"status": status, "name": name})
def setting_theme_config(theme_name): theme_config = json.loads( file.read_file("./templates/{}/config.json".format(theme_name))) for item in theme_config: if type(theme_config[item]) == bool: status = "no" if theme_config[item]: status = "yes" theme_config[item] = dialog.confirm( "Please choose Boolean item [{}]:".format(item), status) if type(theme_config[item]) == int: theme_config[item] = int( dialog.prompt("Please enter Number item [{}]:".format(item), str(theme_config[item]))) if type(theme_config[item]) == float: theme_config[item] = float( dialog.prompt("Please enter Number item [{}]:".format(item), str(theme_config[item]))) if type(theme_config[item]) == str: theme_config[item] = str( dialog.prompt("Please enter String item [{}]:".format(item), theme_config[item])) file.write_file("./templates/{}/config.json".format(theme_name), file.json_format_dump(theme_config)) return
def save_config(): file.write_file( "./config/system.json", json.dumps(system_config, indent=4, sort_keys=False, ensure_ascii=False))
def main(): # GUI窗口 gui = tkinter.Tk() gui.withdraw() # 原始存档文件所在路径 options = { "initialdir": os.path.join(os.path.dirname(__file__), "video"), "initialfile": "save.data", "filetypes": [("data", ".data"), ("all file", "*")], "title": "原始存档文件", } save_data_file_path = tkinter.filedialog.askopenfilename(**options) if not save_data_file_path: return options["title"] = "临时存档文件" temp_save_data_file_path = tkinter.filedialog.askopenfilename(**options) if not save_data_file_path: return if save_data_file_path == temp_save_data_file_path: output.print_msg("存档文件相同,无需合并") return # 临时存档文件所在路径 save_data = crawler.read_save_data(save_data_file_path, PRIME_KEY_INDEX) temp_save_data = crawler.read_save_data(temp_save_data_file_path, PRIME_KEY_INDEX) save_data.update(temp_save_data) temp_list = [save_data[key] for key in sorted(save_data.keys())] file.write_file(tool.list_to_string(temp_list), save_data_file_path, file.WRITE_FILE_TYPE_REPLACE)
def write_remaining_save_data(self): """ 将剩余未处理的存档数据写入临时存档文件 """ if len(self.save_data) > 0 and self.temp_save_data_path: file.write_file(tool.list_to_string(list(self.save_data.values())), self.temp_save_data_path)
def edit_post(post_list, post_index, config, editor=None, is_menu=False): safe_name = get.filter_name(config["name"]) if post_list[post_index]["name"] is not config["name"]: shutil.move("./document/{}.md".format(post_list[post_index]["name"]), "./document/{}.md".format(safe_name)) if os.path.exists("./document/{}.json".format( post_list[post_index]["name"])): shutil.move( "./document/{}.json".format(post_list[post_index]["name"]), "./document/{}.json".format(safe_name)) config_file = json.loads( file.read_file("./document/{}.json".format(safe_name))) config_file["title"] = config["title"] if "time" in config: config_file = config["time"] file.write_file("./document/{}.json".format(safe_name), file.json_format_dump(config_file)) post_list[post_index]["name"] = safe_name if editor is not None: os.system("{0} ./document/{1}.md".format(editor, safe_name)) post_list[post_index]["title"] = config["title"] file_url = "./config/page.json" if is_menu: file_url = "./config/menu.json" file.write_file(file_url, file.json_format_dump(post_list)) console.log("Success", "Edit a new article successfully!")
def new_post(config, independent=False): system_info = json.loads(file.read_file("./config/system.json")) title = config["title"] name = get.filter_name(config["name"]) post_uuid = str(uuid.uuid5(uuid.NAMESPACE_URL, name)) if "uuid" in config: post_uuid = config["uuid"] if not os.path.exists("./document/{}.md".format(name)): editor = system_info["Editor"] os.system("{0} ./document/{1}.md".format(editor, name)) post_info = { "uuid": post_uuid, "name": name, "title": title, "time": round(time.time()) } if not os.path.exists("./document/{}.md".format(name)): console.log("Error", "Cannot find [./document/{}.md]".format(name)) exit(1) if not independent: excerpt = get.get_excerpt("./document/{}.md".format(name)) post_info["excerpt"] = excerpt write_json = post_info page_config = "./document/{}.json".format(name) if not independent: write_json = json.loads(file.read_file("./config/page.json")) write_json.insert(0, post_info) page_config = "./config/page.json" file.write_file(page_config, file.json_format_dump(write_json)) console.log("Success", "Create a new article successfully!")
def notice(msg): """Debug message logger""" msg = _get_time() + " " + str(msg) if LOG_CONFIG["IS_SHOW_NOTICE"]: output.print_msg(msg, False) if LOG_CONFIG["IS_LOG_NOTICE"]: with thread_lock: file.write_file(msg, _replace_path_macro(NOTICE_LOG_PATH))
def step(msg): """Step message logger""" msg = _get_time() + " " + str(msg) if LOG_CONFIG["IS_SHOW_STEP"]: output.print_msg(msg, False) if LOG_CONFIG["IS_LOG_STEP"]: with thread_lock: file.write_file(msg, _replace_path_macro(STEP_LOG_PATH))
def error(msg): """Error message logger""" msg = _get_time() + " [Error] " + str(msg) if LOG_CONFIG["IS_SHOW_ERROR"]: output.print_msg(msg, False) if LOG_CONFIG["IS_LOG_ERROR"]: with thread_lock: file.write_file(msg, _replace_path_macro(ERROR_LOG_PATH))
def edit_menu(menu_list, menu_index, menu_info): menu_item = dict() menu_item["title"] = menu_info["title"] if menu_info["type"]: menu_item["name"] = menu_info["name"] if not menu_info["type"]: menu_item["absolute"] = menu_info["name"] menu_list[menu_index] = menu_item file.write_file("./config/menu.json", file.json_format_dump(menu_list))
def build_rss(): system_config = json.loads(file.read_file("./config/system.json")) system_config["Lazyload"] = False page_list = json.loads(file.read_file("./config/page.json")) file.write_file( "./document/rss.xml", make_rss(system_config["Project_Name"], system_config["Project_URL"], system_config["Project_Description"], page_list, system_config)) console.log("Success", "Build rss success!")
def trace(msg: str): """ trace日志 """ msg = _get_time() + " " + str(msg) if LOG_CONFIG["IS_SHOW_TRACE"]: output.print_msg(msg, False) if LOG_CONFIG["IS_LOG_TRACE"]: with thread_lock: file.write_file(msg, _replace_path_macro(TRACE_LOG_PATH))
def add_menu(menu_info): menu_item = dict() menu_item["title"] = menu_info["title"] if menu_info["type"]: menu_item["name"] = menu_info["name"] if not menu_info["type"]: menu_item["absolute"] = menu_info["name"] menu_file = json.loads(file.read_file("./config/menu.json")) menu_file.append(menu_item) file.write_file("./config/menu.json", file.json_format_dump(menu_file)) return
def rewrite_save_file(self): """ 将临时存档文件按照主键排序后写入原始存档文件 只支持一行一条记录,每条记录格式相同的存档文件 """ if self.temp_save_data_path: save_data = read_save_data(self.temp_save_data_path, 0, []) temp_list = [save_data[key] for key in sorted(save_data.keys())] file.write_file(tool.list_to_string(temp_list), self.save_data_path, file.WRITE_FILE_TYPE_REPLACE) path.delete_dir_or_file(self.temp_save_data_path)
def build_rss(): system_config = json.loads(file.read_file("./config/system.json")) page_list = json.loads(file.read_file("./config/page.json")) if "Rss_Full_Content" in system_config: full_content = system_config["Rss_Full_Content"] file.write_file( "./document/rss.xml", make_rss(system_config["Project_Name"], system_config["Project_URL"], system_config["Project_Description"], page_list, system_config)) console.log("Success", "Build Rss Success!")
def delete_post(page_list, post_index): file_name = page_list[post_index]["name"] meta_backup = page_list[post_index] del page_list[post_index] file.write_file("./config/page.json", file.json_format_dump(page_list)) if not os.path.exists("./trash"): os.mkdir("./trash") check_file("{}.md".format(file_name)) check_file("{}.json".format(file_name)) file.write_file("./trash/{}.json".format(file_name), file.json_format_dump(meta_backup)) shutil.move("./document/{}.md".format(file_name), "./trash/{}.md".format(file_name))
def delete(page_list, post_index): file_name = page_list[post_index]["name"] del page_list[post_index] file.write_file( "./config/page.json", json.dumps(page_list, indent=4, sort_keys=False, ensure_ascii=False)) if not os.path.exists("./trash"): os.mkdir("./trash") if os.path.exists("./trash/{0}.md".format(file_name)): os.remove("./trash/{0}.md".format(file_name)) shutil.move("./document/{0}.md".format(file_name), "./trash/{0}.md".format(file_name))
def add_menu(menu_info): menu_item = dict() menu_item["title"] = menu_info["title"] if menu_info["type"]: menu_item["name"] = menu_info["name"] menu_item["uuid"] = str( uuid.uuid5(uuid.NAMESPACE_URL, menu_info["name"])) if not menu_info["type"]: menu_item["absolute"] = menu_info["name"] menu_file = json.loads(file.read_file("./config/menu.json")) menu_file.append(menu_item) file.write_file("./config/menu.json", file.json_format_dump(menu_file))
def upgrade_data(): if current_data_version != new_data_version: from manage import backup backup.backup(str(current_data_version)) for index in range(current_data_version, new_data_version): if os.path.exists("./upgrade/upgrade_from_{}.py".format(index)): upgrade_item = importlib.import_module( "upgrade.upgrade_from_{}".format(index), __package__) upgrade_item.main() file.write_file("./upgrade/current_version.json", json.dumps({"current_data_version": new_data_version})) console.log("Success", "Upgrade data Successful!")
def run(self): try: self._run() except KeyboardInterrupt: self.step("提前退出") except SystemExit as e: if e.code == tool.PROCESS_EXIT_CODE_ERROR: self.error("异常退出") else: self.step("提前退出") except Exception as e: self.error("未知异常") self.error(str(e) + "\n" + traceback.format_exc(), False) # 从住线程中移除主键对应的信息 if self.index_key: self.main_thread.save_data.pop(self.index_key) # 写入存档 if self.single_save_data and self.main_thread.temp_save_data_path: with self.thread_lock: file.write_file("\t".join(self.single_save_data), self.main_thread.temp_save_data_path, file.WRITE_FILE_TYPE_APPEND) # 主线程计数累加 if self.main_thread.is_download_photo: self.main_thread.total_photo_count += self.total_photo_count if self.main_thread.is_download_video: self.main_thread.total_video_count += self.total_video_count if self.main_thread.is_download_audio: self.main_thread.total_audio_count += self.total_audio_count # 清理临时文件(未完整下载的内容) for temp_path in self.temp_path_list: path.delete_dir_or_file(temp_path) # 日志 message = "下载完毕" download_result = [] if self.main_thread.is_download_photo: download_result.append(f"图片{self.total_photo_count}张") if self.main_thread.is_download_video: download_result.append(f"视频{self.total_video_count}个") if self.main_thread.is_download_audio: download_result.append(f"音频{self.total_audio_count}个") if download_result: message += ",共计下载" + ",".join(download_result) self.step(message) # 唤醒主线程 self.notify_main_thread()
def reformat_save(): new_lines = [] for line in file.read_file(OLD_SAVE_FILE_PATH, file.READ_FILE_TYPE_LINE): temp_list = line.replace("\n", "").split("\t") new_list = list([]) # 新旧字段逻辑 new_list.append(temp_list[0]) new_list.append(temp_list[1]) new_list.append(temp_list[2]) new_lines.append("\t".join(new_list)) file.write_file("\n".join(new_lines), NEW_SAVE_FILE_PATH, file.WRITE_FILE_TYPE_REPLACE)
def edit_menu(menu_list, menu_index, menu_info): menu_item = dict() menu_item["title"] = menu_info["title"] if menu_info["type"]: menu_item["name"] = menu_info["name"] menu_item["uuid"] = str( uuid.uuid5(uuid.NAMESPACE_URL, menu_info["name"])) if not menu_info["type"]: menu_item["absolute"] = menu_info["name"] menu_list[menu_index] = menu_item if menu_info["type"] and menu_info["editor"] is not None: os.system("{0} ./document/{1}.md".format(menu_info["editor"], menu_item["name"])) file.write_file("./config/menu.json", file.json_format_dump(menu_list))
def rewrite_save_file(temp_save_data_path: str, save_data_path: str): """ 将临时存档文件按照主键排序后写入原始存档文件 只支持一行一条记录,每条记录格式相同的存档文件 """ warnings.warn( "rewrite_save_file commands are deprecated.", DeprecationWarning, stacklevel=2, ) account_list = read_save_data(temp_save_data_path, 0, []) temp_list = [account_list[key] for key in sorted(account_list.keys())] file.write_file(tool.list_to_string(temp_list), save_data_path, file.WRITE_FILE_TYPE_REPLACE) path.delete_dir_or_file(temp_save_data_path)
def main(): if not os.path.exists("./backup"): os.mkdir("./backup") shutil.copytree("./config", "./backup/config") shutil.copytree("./document", "./backup/document") if os.path.exists("./templates/static/user_file"): shutil.copytree("./templates/static/user_file", "./backup/static/user_file") write_json = json.loads(file.read_file("./config/page.json")) write_json = list(map(change_time_fomart, write_json)) file.write_file("./config/page.json", file.json_format_dump(write_json)) for filename in os.listdir("./document/"): if filename.endswith(".json"): write_json = json.loads(file.read_file("./document/" + filename)) write_json = change_time_fomart(write_json) file.write_file("./document/" + filename, json_format_dump(write_json))