-
Notifications
You must be signed in to change notification settings - Fork 0
/
folders2tags.py
36 lines (31 loc) · 1.58 KB
/
folders2tags.py
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
import sys
from dataclasses import dataclass
from NetscapeBookmarkFileParser.parser import parse, NetscapeBookmark, NetscapeBookmarkFileData
from NetscapeBookmarkFileParser.exporter import netscape_html
def merge_tags(old: str, new: str) -> str:
merged_tags = {tag.strip().casefold(): tag.strip() for tag in old.split(',')}
for tag in new.split(','):
if tag.strip().casefold() not in merged_tags:
merged_tags[tag.strip().casefold()] = tag.strip()
merged_tags.pop('', None)
merged_tags = ','.join(merged_tags.values())
return merged_tags
def collect_bookmarks(bookmarks_dict: dict, bookmarks_data: NetscapeBookmarkFileData, path: str = '') -> dict:
for bookmark in bookmarks_data.bookmarks:
bookmark.attributes['tags'] = merge_tags(bookmark.attributes.get('tags', ''), path.replace('/', ','))
if bookmark.attributes['href'] in bookmarks_dict:
bookmarks_dict[bookmark.attributes['href']].attributes['tags'] = merge_tags(bookmarks_dict[bookmark.attributes['href']].attributes['tags'], bookmark.attributes['tags'])
else:
bookmarks_dict[bookmark.attributes['href']] = bookmark
for sub_folder in bookmarks_data.sub_folders:
bookmarks_dict = collect_bookmarks(bookmarks_dict, sub_folder, path+'/'+sub_folder.folder)
return bookmarks_dict
netscape_bookmarks = parse(sys.stdin.read())
netscape_bookmarks = NetscapeBookmarkFileData(
folder='',
attributes={},
description='',
bookmarks=collect_bookmarks({}, netscape_bookmarks).values(),
sub_folders=[],
)
print(netscape_html(netscape_bookmarks))