gridea用了半年,开始试试hexo。这是一个迁移脚本。
这是deepseek完成的作业。
hexo是一个快速、简洁且高效的博客框架。

gridea是一个自带客户端的博客框架。


hexo本身只有cli,需要md编辑器来写md,由于习惯了gridea写md(gridea能自动把标题转换成英文单词,拼音并由-连接的文件名,而不是直接用带中文的标题当文件名,能插入本地图片,由时间戳重命名),才有了这个需求。
同步脚本
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| import os import shutil
def sync_files(src_dir, dst_dir): """同步源目录到目标目录(增量模式)""" if not os.path.isdir(src_dir): print(f"错误:源目录不存在 {src_dir}") return False if not os.path.exists(dst_dir): print(f"创建目标目录 {dst_dir}") os.makedirs(dst_dir)
print(f"正在同步 {src_dir} -> {dst_dir}") total = copied = skipped = 0 for root, _, files in os.walk(src_dir): for filename in files: total += 1 src_path = os.path.join(root, filename) rel_path = os.path.relpath(src_path, src_dir) dst_path = os.path.join(dst_dir, rel_path) if not os.path.exists(dst_path): copy_reason = "新文件" else: src_stat = os.stat(src_path) dst_stat = os.stat(dst_path) if src_stat.st_mtime > dst_stat.st_mtime or src_stat.st_size != dst_stat.st_size: copy_reason = "已更新" else: skipped += 1 continue os.makedirs(os.path.dirname(dst_path), exist_ok=True) try: shutil.copy2(src_path, dst_path) print(f"已复制 ({copy_reason}): {rel_path}") copied += 1 except Exception as e: print(f"复制失败 {rel_path}: {str(e)}") print(f"同步完成. 总计: {total}, 已复制: {copied}, 已跳过: {skipped}") return True
def main(): sync_files( r"E:\documents\Gridea\post-images", r"D:\hexo\blog\source\post-images" ) sync_files( r"E:\documents\Gridea\posts", r"D:\hexo\blog\source\_posts" )
if __name__ == "__main__": main()
|
保存为 sync_hexo.py,直接运行。
特点:
- 简洁实现:完全去除了日志系统,只保留必要的控制台输出;
- 双目录同步:同时处理图片目录和文章目录;
- 增量同步:通过比较文件修改时间和大小,只复制有变动的文件;
- 自动创建目录:如果目标目录不存在会自动创建;
- 错误处理:会捕获并显示复制过程中出现的错误。
注意事项
确保源目录路径正确(特别是Gridea的默认路径可能有变化)。
更新
由于gridea的md文件封面图使用feature标签,而hexo使用cover,在同步的同时需要进行替换。
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| import os import shutil import time
STATE_FILE = ".last_sync_time"
def get_last_sync_time(): """读取上次同步时间戳,如果没有则返回 0(表示全部同步)""" if os.path.exists(STATE_FILE): with open(STATE_FILE, 'r') as f: return float(f.read().strip()) return 0
def save_sync_time(): """保存当前时间作为最后同步时间""" with open(STATE_FILE, 'w') as f: f.write(str(time.time()))
def convert_feature_to_cover(file_path): """将 md 文件中的 feature: 替换为 cover:""" try: with open(file_path, 'r', encoding='utf-8') as f: content = f.read() if 'feature:' in content: new_content = content.replace('feature:', 'cover:') with open(file_path, 'w', encoding='utf-8') as f: f.write(new_content) return True except Exception as e: print(f"替换失败 {file_path}: {str(e)}") return False
def sync_files(src_dir, dst_dir, last_time): """同步源目录到目标目录,只同步 last_time 之后修改的文件""" if not os.path.isdir(src_dir): print(f"错误:源目录不存在 {src_dir}") return False if not os.path.exists(dst_dir): print(f"创建目标目录 {dst_dir}") os.makedirs(dst_dir)
print(f"正在同步 {src_dir} -> {dst_dir}") print(f"只同步修改时间 > {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(last_time))} 的文件") total = 0 copied = 0 skipped = 0 converted = 0 for root, _, files in os.walk(src_dir): for filename in files: total += 1 src_path = os.path.join(root, filename) rel_path = os.path.relpath(src_path, src_dir) dst_path = os.path.join(dst_dir, rel_path) src_mtime = os.stat(src_path).st_mtime if src_mtime <= last_time: skipped += 1 continue os.makedirs(os.path.dirname(dst_path), exist_ok=True) try: shutil.copy2(src_path, dst_path) print(f"已复制 (更新于 {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(src_mtime))}): {rel_path}") copied += 1 if filename.endswith('.md'): if convert_feature_to_cover(dst_path): converted += 1 print(f" ✅ 已将 feature → cover: {rel_path}") except Exception as e: print(f"复制失败 {rel_path}: {str(e)}") print(f"同步完成. 总计: {total}, 已复制: {copied}, 已跳过(未更新): {skipped}, 字段转换: {converted}") return True
def main(): last_time = get_last_sync_time() sync_files( r"E:\documents\Gridea\post-images", r"D:\hexo\blog\source\post-images", last_time ) sync_files( r"E:\documents\Gridea\posts", r"D:\hexo\blog\source\_posts", last_time ) save_sync_time() print(f"✅ 同步时间已保存: {time.strftime('%Y-%m-%d %H:%M:%S')}")
if __name__ == "__main__": main()
|
就是任性,用gridea客户端写,用hexo生成网站。