WeTab备份转Infinity格式

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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
WeTab书签数据转Infinity新标签页格式转换工具

此脚本将WeTab备份文件中的书签数据转换为Infinity新标签页格式。
用法:将脚本放在含有wetab-bak.json和infinity-bak.json的目录下执行。
"""

import json
import uuid
import time
import os
from copy import deepcopy

def generate_uuid():
"""生成一个随机UUID字符串,用于Infinity书签ID"""
return str(uuid.uuid4())

def generate_short_uuid():
"""生成一个短UUID字符串,用于Infinity书签uuid字段"""
return f"{uuid.uuid4().hex[:28]}"

def generate_folder_id():
"""生成一个文件夹ID,遵循Infinity的文件夹ID格式"""
return f"folderId-{uuid.uuid4().hex[:28]}"

def wetab_to_infinity(wetab_file, infinity_file, output_file):
"""
将WeTab格式的书签转换为Infinity格式

Args:
wetab_file: WeTab备份文件路径
infinity_file: Infinity备份文件路径(用作模板)
output_file: 输出文件路径

Returns:
str: 转换完成的提示信息
"""
# 读取源文件
print(f"读取WeTab备份文件: {wetab_file}")
with open(wetab_file, 'r', encoding='utf-8') as f:
wetab_data = json.load(f)

print(f"读取Infinity模板文件: {infinity_file}")
with open(infinity_file, 'r', encoding='utf-8') as f:
infinity_data = json.load(f)

# 创建一个新的Infinity数据结构,复制原模板
new_infinity_data = deepcopy(infinity_data)

# 获取WeTab的icons数据
icons = wetab_data.get('data', {}).get('store-icon', {}).get('icons', [])

if not icons:
return "错误: WeTab数据中未找到书签数据 (data.store-icon.icons)"

# 创建新的sites数组
new_sites = []

print(f"开始处理{len(icons)}个分类的书签...")

# 遍历WeTab的categories (主要分类)
for category in icons:
category_items = []
category_name = category.get('name', '未命名类别')
print(f"处理分类: {category_name}")

# 处理分类下的子项
for child in category.get('children', []):
# 检查是否是文件夹
if child.get('type') == 'folder-icon':
folder_name = child.get('name', '未命名文件夹')
print(f" - 处理文件夹: {folder_name}")

# 创建Infinity文件夹
folder = {
"id": generate_folder_id(),
"name": folder_name,
"updatetime": int(time.time() * 1000),
"children": []
}

# 处理文件夹中的子项
for folder_child in child.get('children', []):
if folder_child.get('type') == 'site':
site_name = folder_child.get('name', '未命名')
print(f" - 处理书签: {site_name}")

# 创建书签项
bookmark = {
"id": folder_child.get('id', generate_uuid()),
"uuid": generate_short_uuid(),
"initials": site_name[:1].upper(),
"name": site_name,
"parentId": folder['id'],
"bgFont": 30,
"bgText": "",
"bgColor": folder_child.get('bgColor', ''),
"type": "web",
"updatetime": folder_child.get('updateTime', int(time.time() * 1000)),
"target": folder_child.get('target', ''),
"bgType": folder_child.get('bgType', 'color'),
"bgImage": folder_child.get('bgImage', '')
}
folder['children'].append(bookmark)

# 添加文件夹到类别
if folder['children']:
category_items.append(folder)

# 处理独立书签(不在文件夹内的书签)
elif child.get('type') == 'site':
site_name = child.get('name', '未命名')
print(f" - 处理单独书签: {site_name}")

bookmark = {
"id": child.get('id', generate_uuid()),
"uuid": generate_short_uuid(),
"initials": site_name[:1].upper(),
"name": site_name,
"bgFont": 30,
"bgText": "",
"bgColor": child.get('bgColor', ''),
"type": "web",
"updatetime": child.get('updateTime', int(time.time() * 1000)),
"target": child.get('target', ''),
"bgType": child.get('bgType', 'color'),
"bgImage": child.get('bgImage', '')
}
category_items.append(bookmark)

# 添加该类别到sites
if category_items:
new_sites.append(category_items)

# 替换新数据
new_infinity_data['data']['site']['sites'] = new_sites
new_infinity_data['time'] = int(time.time() * 1000)

# 写入输出文件
print(f"写入转换后的数据到: {output_file}")
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(new_infinity_data, f, ensure_ascii=False, indent=4)

# 统计转换数据
total_categories = len(new_sites)
total_bookmarks = sum(len(category) for category in new_sites)

return f"转换完成! 共处理了{total_categories}个分类,{total_bookmarks}个书签项。已保存到 {output_file}"

if __name__ == "__main__":
# 文件路径
current_dir = os.path.dirname(os.path.abspath(__file__))
wetab_file = os.path.join(current_dir, "wetab-bak.json")
infinity_file = os.path.join(current_dir, "infinity-bak.json")
output_file = os.path.join(current_dir, "converted-infinity.json")

# 执行转换
try:
result = wetab_to_infinity(wetab_file, infinity_file, output_file)
print(result)
except Exception as e:
print(f"转换过程中发生错误: {str(e)}")

WeTab备份转Infinity格式
https://itxiaopang.github.io/p/4352b1bdcb5d4989bcc72741220c08b2/
作者
挨踢小胖
发布于
2025年3月13日
许可协议