-
-
Save iqiancheng/4e68f3d5469b5f3ec3758ce1d4f92add to your computer and use it in GitHub Desktop.
把mi-note-export.js导出的data.json文件转换成能导入到mac便签的文件夹
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
#!/usr/bin/python | |
#-*- coding: utf-8 -*- | |
from __future__ import print_function, unicode_literals | |
import sys | |
import json | |
import os | |
import shutil | |
import re | |
import subprocess | |
import platform | |
from io import open | |
SOURCE_FILE = 'data.json' | |
TARGET_DIR = 'notes' | |
def _save_file(file_name, content, date): | |
global save_created_time | |
# file name | |
file_name = os.path.join(TARGET_DIR, file_name) | |
# content | |
content = '\n'.join([d.strip() for d in content.split('\n')[1:]]) | |
# save | |
with open(file_name, 'w+', encoding='utf-8') as fp: | |
fp.write(content) | |
if platform.system() == 'Darwin': | |
# 2001年2月10日 10:00 to 2/10/2001 10:00 | |
print(date) | |
date = re.findall(r'\d+', date) | |
print(date) | |
year, month, day, time = date[0], date[1], date[2], ':'.join(date[3:]) | |
date = month + '/' + day + '/' + year + ' ' + time | |
# set file's creation time | |
# SetFile -d '12/31/1999 23:59[:59]' file.txt | |
print(subprocess.check_output(['SetFile', '-d', date, file_name])) | |
print(subprocess.check_output(['SetFile', '-m', date, file_name])) | |
print('Done. ', file_name) | |
def mkdir(path, prefix=TARGET_DIR): | |
if not os.path.exists(os.path.join(prefix, path)): | |
os.makedirs(os.path.join(prefix, path)) | |
else: | |
shutil.rmtree(os.path.join(prefix, path)) | |
def _parse_notes(notes, box): | |
for d in notes: | |
date = d['created_time'] | |
content = d['content'] | |
_save_file(os.path.join(box, date), content, date) | |
def run(): | |
mkdir('', TARGET_DIR) | |
file_path = '' | |
if os.path.isfile(SOURCE_FILE): | |
file_path = SOURCE_FILE | |
elif os.path.isfile('~/Downloads/' + SOURCE_FILE): | |
file_path = '~/Downloads/' + SOURCE_FILE | |
else: | |
raise ValueError("找不到data.json文件,请将data.json与此脚本置于同一目录") | |
with open(file_path, mode='r', encoding='utf-8') as fp: | |
source = json.load(fp) | |
notes = [] | |
for item in source: | |
box_name = item.get('box_name') | |
if box_name: | |
mkdir(box_name) | |
_parse_notes(item['content'], box_name) | |
else: | |
notes.append(item) | |
_parse_notes(notes, '') | |
if __name__ == '__main__': | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment