Last active
April 9, 2025 12:15
-
-
Save duolanda/6d422212020c38be2c8167bb0f2261e0 to your computer and use it in GitHub Desktop.
Change siyuan note date by exported md file from evernote2md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
import re | |
import datetime | |
def process_files(directory_path): | |
''' | |
遍历 SiYuan/data 目录下所有 .sy 结尾的文件,提取文件的标题和唯一码,生成映射数组,将映射数组保存到 mappings.txt 文件中 | |
''' | |
mapping_array = [] | |
# 遍历 SiYuan/data 目录下所有 .sy 结尾的文件 | |
for root, dirs, files in os.walk(directory_path): | |
for file in files: | |
if file.endswith(".sy"): | |
file_path = os.path.join(root, file) | |
# 提取文件名中的时间和唯一码 | |
match = re.match(r'(\d+)-(\w+)\.sy', file) | |
if match: | |
create_time, file_key = match.groups() | |
else: | |
continue | |
# 读取 JSON 文件内容,根据这个title你可以确定笔记创建时间(从其他笔记应用导过来) | |
with open(file_path, 'r', encoding='utf-8') as json_file: | |
data = json.load(json_file) | |
title_content = data.get("Properties", {}).get("title", "") | |
# 生成映射数组 | |
mapping = { | |
'key': file_key, | |
'title': title_content, | |
'real_create_time': '', | |
'filePath': file_path | |
} | |
mapping_array.append(mapping) | |
# 将映射数组保存到 mappings.txt 文件中 | |
with open('mappings.txt', 'w', encoding='utf-8') as mappings_file: | |
json.dump(mapping_array, mappings_file, ensure_ascii=False, indent=2) | |
def get_real_create_time(raw_file_directory): | |
''' | |
获取真实的创建时间 | |
''' | |
# 读取映射数组 | |
with open('mappings.txt', 'r', encoding='utf-8') as mappings_file: | |
mapping_array = json.load(mappings_file) | |
create_date = {} | |
for root, dirs, files in os.walk(raw_file_directory): | |
for file in files: | |
if file.endswith(".md"): | |
file_path = os.path.join(root, file) | |
file_name = os.path.splitext(file)[0] | |
creation_time = os.path.getmtime(file_path) | |
dt = datetime.datetime.fromtimestamp(creation_time) | |
formatted_time = dt.strftime('%Y%m%d%H%M%S') | |
create_date[file_name] = formatted_time | |
for mapping in mapping_array: | |
title = mapping['title'].replace('amp;', '') #去掉空格以匹配 | |
if title in create_date: | |
mapping['real_create_time'] = create_date[title] | |
else: | |
mapping['real_create_time'] = '' | |
# 更新 mappings.txt 文件 | |
with open('mappings.txt', 'w', encoding='utf-8') as mappings_file: | |
json.dump(mapping_array, mappings_file, ensure_ascii=False, indent=2) | |
def replace_time_in_files(): | |
''' | |
通过替换文件名,替换思源笔记文件的时间 | |
''' | |
# 读取映射数组 | |
with open('mappings.txt', 'r', encoding='utf-8') as mappings_file: | |
mapping_array = json.load(mappings_file) | |
for mapping in mapping_array: | |
# 只有当 real_create_time 不为空时才执行替换操作 | |
if mapping['real_create_time']: | |
old_file_path = mapping['filePath'] | |
# 读取旧文件内容 | |
with open(old_file_path, 'r', encoding='utf-8') as old_file: | |
file_content = old_file.read() | |
# 替换文件内容中的时间戳 | |
new_file_content = re.sub( | |
r'(?<="ID":")(\d{14})|(?<="id":")(\d{14})|(?<="updated":")(\d{14})', | |
mapping['real_create_time'], | |
file_content | |
) | |
# 构造新文件名 | |
old_dir, old_file_name = os.path.split(old_file_path) | |
new_file_name = mapping['real_create_time']+'-'+mapping['key']+'.sy' | |
new_file_name = os.path.join(old_dir, new_file_name) | |
# print(old_dir,old_file_path,new_file_name,new_file_content,sep='\r\n\r\n') | |
# 写入新文件 | |
with open(new_file_name, 'w', encoding='utf-8') as new_file: | |
new_file.write(new_file_content) | |
# 删除旧文件 | |
os.remove(old_file_path) | |
if __name__ == '__main__': | |
# 创建时间对应数组 | |
directory_path = '/Users/username/SiYuan/data/your_siyuan_dir/' | |
process_files(directory_path) | |
# 根据导出的 md 文件得到真实的创建时间 | |
raw_file_direcroty = '/Users/username/Documents/evernote2md/your_md_dir/' | |
get_real_create_time(raw_file_direcroty) | |
# 替换文件的时间 | |
replace_time_in_files() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment