本文最后更新于 2024年5月3日 晚上
一次致命的失误
今晚清理文件的时候, 不小心用api把图床上所有, 五年来积攒下的全部图片
清空了
只能把所有博客隐藏掉了, 心情很崩溃, 还得去解决一些关键笔记的图片问题…
以后坚决要把图片至少在两个地方 + 本地备份.
想死.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| import os import yaml
for root, dirs, files in os.walk("."): for file in files: if file.endswith(".md"): filepath = os.path.join(root, file) with open(filepath, "r+", encoding="utf-8") as f: content = f.read() if content.startswith("---"): front_matter, _, content = content[3:].partition("---\n") data = yaml.load(front_matter, Loader=yaml.FullLoader) data['hide'] = True front_matter = yaml.dump(data, sort_keys=False, allow_unicode=True) f.seek(0) f.write("---\n" + front_matter.strip() + "\n---\n" + content) f.truncate() print(f"{filepath} 已经添加了 hide: true 标识") else: f.seek(0) f.write("---\nhide: true\n---\n" + content) f.truncate() print(f"{filepath} 新生成 front-matter")
|
后续:
后来采用的图床同步方案:
现在相当于三端同步, 我就不信还能碰到惨案…
附同步code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| import os import time import git import upyun import wget
upyun_service = upyun_username = upyun_password = upyun_path =
github_repo_path = github_username = github_password = github_branch =
up = upyun.UpYun(upyun_service, upyun_username, upyun_password)
repo = git.Repo() git = repo.git
git.pull("origin", github_branch)
t = time.localtime() current_time = time.strftime("%Y-%m-%d %H:%M:%S", t)
files = []
for item in up.getlist(upyun_path): filepath = item["name"] filename = os.path.basename(filepath) files.append(filename) if not os.path.exists('./' + filename): wget.download(f"https://youpai.roccoshi.top/img/{filename}") print(f"\n Committing new file {filepath}...") git.add(filepath)
git.commit("-m", f"sync from upyun at {current_time}") git.push('origin', github_branch)
print("Pushing changes to GitHub...") git.push("origin", github_branch)
print("Backup finished!")
|