-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnote_merge.py
More file actions
114 lines (100 loc) · 4.4 KB
/
note_merge.py
File metadata and controls
114 lines (100 loc) · 4.4 KB
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
112
113
114
"""
处理所有在手机上记录的日记之类的内容
"""
import os
from datetime import datetime
from u_base import u_log as log
from u_base import u_file
def process_xiaomi_notes():
note_dir = r'G:\AAZZlong\可爱的人生\便签\note'
tag_dirs = os.listdir(note_dir)
notes = []
log.info('tag size: {}'.format(len(tag_dirs)))
for tag_dir in tag_dirs:
tag_path = os.path.join(note_dir, tag_dir)
note_paths = os.listdir(tag_path)
log.info('tag: {}, note size: {}'.format(tag_dir, len(note_paths)))
for note_filename in note_paths:
if '0000.txt' in note_filename:
continue
note_path = os.path.join(tag_path, note_filename)
with open(note_path, 'r', encoding='utf-8') as file_handler:
line = file_handler.readline()
content = ''
note_date_time = None
line_index = -1
while line:
line_index += 1
# 过滤空行
if not line:
continue
# 包含4个空格的为内容,否则为时间
if ' ' in line:
# 内容去掉前面是个空格
content += line.replace(' ', '')
if not note_date_time and '年' in line:
note_date_time = datetime.strptime(line.strip(), '%Y年%m月%d日 %H:%M')
line = file_handler.readline()
if not note_date_time:
log.error('note date time is empty. note_path: {}'.format(note_path))
continue
notes.append({
'tag': tag_dir,
'title': '',
'time': note_date_time.strftime('%Y-%m-%d %H:%M:%S'),
'content': content
})
log.info('process xiaomi notes success. size: {}'.format(len(notes)))
return notes
def process_samsung_notes():
note_dir = r'G:\AAZZlong\可爱的人生\便签\Samsung-Backup\三星S8笔记'
tag_dirs = os.listdir(note_dir)
notes = []
log.info('tag size: {}'.format(len(tag_dirs)))
for tag_dir in tag_dirs:
tag_path = os.path.join(note_dir, tag_dir)
note_paths = os.listdir(tag_path)
log.info('tag: {}, note szie: {}'.format(tag_dir, len(note_paths)))
for note_filename in note_paths:
note_names = note_filename.replace('.txt', '').split('_')
if len(note_names) < 3:
log.warn('The file is unknown note name: {}'.format(note_filename))
note_title = note_names[0] if note_names[0] != 'Notes' else ''
note_date_time_str = '20' + note_names[1] + note_names[2]
note_date_time = datetime.strptime(note_date_time_str, '%Y%m%d%H%M%S')
note_path = os.path.join(tag_path, note_filename)
note_content = u_file.read_content(note_path)
notes.append({
'tag': tag_dir,
'title': note_title,
'time': note_date_time.strftime('%Y-%m-%d %H:%M:%S'),
'content': note_content
})
log.info('process samsung notes finish. size: {}'.format(len(notes)))
return notes
def merge_note_by_tag(note_cache_file_path: str):
"""
将所有便签合并按照文件夹写到一个文件中
:param note_cache_file_path: 便签json缓存路径
:return:
"""
if not os.path.isfile(note_cache_file_path):
notes = process_xiaomi_notes()
notes.extend(process_samsung_notes())
u_file.dump_json_to_file(note_cache_file_path, notes)
else:
notes = u_file.load_json_from_file(note_cache_file_path)
content_map = {}
notes = sorted(notes, key=lambda x: datetime.strptime(x['time'], '%Y-%m-%d %H:%M:%S').timestamp())
for note in notes:
if note['tag'] not in content_map:
content_map[note['tag']] = ''
content_map[note['tag']] += f"{note['title']}({note['time']})\n"
content_map[note['tag']] += f"{note['content']}\n\n\n"
u_file.write_content(f"result\\note-{note['tag']}.txt", content_map[note['tag']])
if __name__ == '__main__':
cache_file_path = r'result\notes.json'
# notes = process_xiaomi_notes()
# notes.extend(process_samsung_notes())
# u_file.dump_json_to_file(cache_file_path, notes)
merge_note_by_tag(cache_file_path)