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 build_page(name, system_config, page_info, menu_list, template_config, i18n=None): content = file.read_file("./document/{0}.md".format(name)) if page_info is None: page_info = {"title": "undefined"} if os.path.exists("./document/{0}.json".format(name)): page_info = json.loads(file.read_file( "document/{0}.json".format(name))) if "time" in page_info: page_info["time"] = str( post_map.build_time(page_info["time"], system_config)) document = markdown.markdown(content) template = env.get_template("./{0}/post.html".format( system_config["Theme"])) result = template.render(page_info=page_info, menu_list=menu_list, content=document, system_config=system_config, template_config=template_config, now_time=time.localtime(), i18n=i18n) return result
def use_text_mode(args): if args.command == "qrcode": system_config = json.loads(file.read_file("./config/system.json")) from common import install_module install_module.install_and_import("qrcode_terminal") import qrcode_terminal if len(system_config["API_Password"]) == 0 or len(system_config["Project_URL"]) == 0: console.log("Error", "Check the API_Password and Project_URL configuration items") exit(1) try: password_md5 = json.loads(system_config["API_Password"])["hash_password"] except (ValueError, KeyError, TypeError): exit(1) console.log("Info", "Please use the client to scan the following QR Code") config_json = json.dumps({"url": system_config["Project_URL"], "password": password_md5}) qrcode_terminal.draw(config_json) exit(0) if args.command == "upgrade": from manage import upgrade if upgrade.upgrade_check(): if not args.yes: start_to_pull = input('Find new version, do you want to upgrade? [y/N]') if start_to_pull.lower() == 'yes' or start_to_pull.lower() == 'y' or args.yes: upgrade.upgrade_pull() exit(0) console.log("Info", "No upgrade found") exit(0) if args.command == "build-page": from manage import build_static_page build_static_page.publish() exit(0) from manage import build_rss, post_manage if args.command == "new": config = None if args.config is not None: config = json.loads(file.read_file(args.config)) if config is None: print("Please enter the title of the article:") title = input().strip() if len(title) == 0: console.log("Error", "The title can not be blank.") exit(1) name = get.get_name(title) print("Please enter the slug [{}]:".format(name)) name2 = input().strip() if len(name2) != 0: name = get.filter_name(name2) if os.path.exists("./document/{}.md".format(name)): console.log("Error", "File [./document/{}.md] already exists".format(name)) exit(1) if len(name) != 0 and len(title) != 0: config = {"title": title, "name": name} post_manage.new_post(config, args.independent) build_rss.build_rss() exit(0) if args.command == "update": if post_manage.update_post(): build_rss.build_rss() exit(0)
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 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 get_content(request_type): file_url = select_type(request_type) 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"]) result = dict() result["title"] = page_list[post_id]["title"] result["name"] = page_list[post_id]["name"] result["content"] = file.read_file("./document/{0}.md".format(page_list[post_id]["name"])) result["status"] = True return json.dumps(result)
def get_content(request_type): file_url = select_type(request_type) 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) result = dict() result["title"] = page_list[post_index]["title"] result["name"] = page_list[post_index]["name"] result["content"] = file.read_file("./document/{0}.md".format( page_list[post_index]["name"])) return jsonify(result)
def read_save_data(save_data_path, key_index=0, default_value_list=[], check_duplicate_index=True): result_list = {} if not os.path.exists(save_data_path): return result_list for single_save_data in file.read_file(save_data_path, file.READ_FILE_TYPE_LINE): single_save_data = single_save_data.replace("\n", "").replace("\r", "") if len(single_save_data) == 0: continue single_save_list = single_save_data.split("\t") if check_duplicate_index and single_save_list[key_index] in result_list: output.print_msg("存档中存在重复行 %s" % single_save_list[key_index]) tool.process_exit() # 去除前后空格 single_save_list = [value.strip() for value in single_save_list] # 根据default_value_list给没给字段默认值 index = 0 for default_value in default_value_list: # _开头表示和该数组下标的值一直,如["", "_0"] 表示第1位为空时数值和第0位一致 if default_value != "" and default_value[0] == "_": default_value = single_save_list[int( default_value.replace("_", ""))] if len(single_save_list) <= index: single_save_list.append(default_value) if single_save_list[index] == "": single_save_list[index] = default_value index += 1 result_list[single_save_list[key_index]] = single_save_list return result_list
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 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 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))
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 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 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 get_post_list(request_type): file_url = select_type(request_type) if file_url is None: abort(404) result_list = json.loads(file.read_file(file_url)) if request_type == "post": result_list = list(map(convert_timestamp, result_list)) return jsonify(result_list)
def get_save_data_file_count(): if not os.path.exists(SAVE_DATA_FILE_PATH): output.print_msg(f"save data {SAVE_DATA_FILE_PATH} not exist") return {} account_list = {} for line in file.read_file(SAVE_DATA_FILE_PATH, file.READ_FILE_TYPE_LINE): temp_list = line.replace("\n", "").split("\t") account_list[temp_list[PRIME_KEY_INDEX]] = int(temp_list[COUNT_INDEX]) return account_list
def check_is_repeat(): history = [] for line in file.read_file(SAVE_FILE_PATH, file.READ_FILE_TYPE_LINE): temp_list = line.replace("\n", "").split("\t") if temp_list[NAME_COLUMN] in history: output.print_msg(temp_list[NAME_COLUMN]) else: history.append(temp_list[NAME_COLUMN]) return history
def post_list(request_type): file_url = select_type(request_type) if file_url is None: abort(404) page_list = json.loads(file.read_file(file_url)) if request_type == "post": for item in page_list: page_list[page_list.index(item)]["time"] = str(post_map.build_time(item["time"], system_config)) return json.dumps(page_list)
def edit_post(): from manage import edit_post dialog.title = "Edit post" page_list, post_index = select_post() if not page_list: return config = get_post_info(page_list[post_index]["title"], page_list[post_index]["name"]) system_info = json.loads(file.read_file("./config/system.json")) edit_post.edit(page_list, post_index, config, system_info["Editor"])
def load_review_list(): review_data = { "can_review_lists": [], "dlc_in_game": {}, "review_list": [], } if not os.path.exists(REVIEW_DATA_PATH): return review_data review_data = tool.json_decode(file.read_file(REVIEW_DATA_PATH), review_data) return review_data
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 main(): if not os.path.exists("./backup"): os.mkdir("./backup") tar = tarfile.open( "./backup/backup-version_2-{}.tar.gz".format( time.strftime("%Y%m%d%H%M%S", time.localtime())), "w:gz") add_tar_file(tar, "./config") add_tar_file(tar, "./document") add_tar_file(tar, "./templates") add_tar_file(tar, "./static_file") tar.close() page_list = json.loads(file.read_file("./config/page.json")) page_list = list(map(add_page_id, page_list)) menu_list = json.loads(file.read_file("./config/menu.json")) menu_list = list(map(add_menu_id, menu_list)) file.write_file("./config/page.json", file.json_format_dump(page_list)) file.write_file("./config/menu.json", file.json_format_dump(menu_list)) system_config = json.loads(file.read_file("./config/system.json")) control_config = dict() try: old_password_hash = json.loads( system_config["API_Password"])["hash_password"] control_config["password"] = hmac.new( str("SiLvErBlOg").encode('utf-8'), str(old_password_hash).encode('utf-8'), hashlib.sha256).hexdigest() except (ValueError, KeyError, TypeError): pass del system_config["API_Password"] system_config["Pinyin"] = True system_config["Use_CDN"] = True system_config["Lazyload"] = False file.write_file("./config/system.json", file.json_format_dump(system_config)) file.write_file("./config/control.json", file.json_format_dump(control_config)) if os.path.exists("./upgrade/last_fetch_time.json"): os.remove("./upgrade/last_fetch_time.json")
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 select_list(list_name): page_title_list = list() page_list = json.loads(file.read_file(list_name)) i = 1 if len(page_list) == 0: dialog.alert("The page list can not be blank.") return [], 0 for item in page_list: page_title_list.append("{}. {}".format(i, item["title"])) i += 1 post_title = dialog.menu("Please select the post to be operated:", page_title_list) return page_list, page_title_list.index(post_title)
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 delete(): if request.json is None: abort(400) page_list = json.loads(file.read_file("./config/page.json")) post_id = int(request.json["post_id"]) sign = str(request.json["sign"]) status = False if page_list[post_id].get("lock", False): return json.dumps({"status": False}) if check_password(str(post_id) + page_list[post_id]["title"], sign): status = True post_manage.delete_post(page_list, post_id) build_rss.build_rss() return json.dumps({"status": status})
def delete(): if request.json is None: abort(400) page_list = json.loads(file.read_file("./config/page.json")) post_uuid = str(request.json["post_uuid"]) post_index = get_index(page_list, post_uuid) sign = str(request.json["sign"]) send_time = int(request.json["send_time"]) status = False hash_content = page_list[post_index]["uuid"] + page_list[post_index][ "title"] + page_list[post_index]["name"] if check_password(hash_content, sign, send_time): status = True post_manage.delete_post(page_list, post_index) build_rss.build_rss() result = {"status": status} return jsonify(result)
def read_save_data(save_data_path: str, key_index: int = 0, default_value_list: list = None, check_duplicate_index: bool = True) -> dict: """ 读取存档文件,并根据指定列生成存档字典 :Args: - save_data_path - 存档路径 - key_index - 配置文件的主键(唯一) - default_value_list - 每一位的默认值 - check_duplicate_index - 是否检测主键的唯一性 """ if default_value_list is None: default_value_list = [] result_list = {} if not os.path.exists(save_data_path): return result_list for single_save_data in file.read_file(save_data_path, file.READ_FILE_TYPE_LINE): single_save_data = single_save_data.replace("\n", "").replace("\r", "") if len(single_save_data) == 0: continue single_save_list = single_save_data.split("\t") if check_duplicate_index and single_save_list[key_index] in result_list: output.print_msg(f"存档中存在重复行{single_save_list[key_index]}") tool.process_exit() # 去除前后空格 single_save_list = [value.strip() for value in single_save_list] # 根据default_value_list给没给字段默认值 index = 0 for default_value in default_value_list: # _开头表示和该数组下标的值一直,如["", "_0"] 表示第1位为空时数值和第0位一致 if default_value != "" and default_value[0] == "_": default_value = single_save_list[int( default_value.replace("_", ""))] if len(single_save_list) <= index: single_save_list.append(default_value) if single_save_list[index] == "": single_save_list[index] = default_value index += 1 result_list[single_save_list[key_index]] = single_save_list return result_list
def article_manager(): from manage import build_rss, post_manage while True: dialog.title = "Article manager" menu_list = [ "New", "Update", "Edit", "Delete", "=" * 25, "Back", "Exit" ] result = dialog.menu("Please select an action", menu_list) if result == "Exit": exit(0) if result == "Back": break if result == "New": dialog.title = "New post" post_info = get_post_info() if os.path.exists("./document/{}.md".format(post_info["name"])): console.log( "Error", "File [./document/{}.md] already exists".format( post_info["name"])) exit(1) if post_info["name"] is not None: post_manage.new_post( post_info, dialog.confirm("Is this an independent page?", "no")) if result == "Edit": dialog.title = "Edit post" page_list, post_index = select_list("./config/page.json") if page_list: config = get_post_info(page_list[post_index]["title"], page_list[post_index]["name"], page_list[post_index]["time"]) system_info = json.loads( file.read_file("./config/system.json")) post_manage.edit_post(page_list, post_index, config, system_info["Editor"]) post_manage.update_post() if result == "Delete": page_list, post_index = select_list("./config/page.json") if page_list and dialog.confirm( "Are you sure you want to delete this article?", "no"): post_manage.delete_post(page_list, post_index) if result == "Update": post_manage.update_post() build_rss.build_rss() time.sleep(0.5)