organize-bookmarks/main.py

95 lines
3.1 KiB
Python
Raw Permalink Normal View History

2026-02-22 20:48:18 +01:00
import json
import sqlite3
import os
import urllib.parse
dir = os.listdir('./data')
# To add to db: Title, URL, Domain
# print(dir)
all_bookmarks = []
def check_domain(all_bookmarks, domain):
present = False
for item in all_bookmarks:
if domain == item['domain']:
present = True
return present
def parse_object(object_):
for child in object_['children']:
if 'children' in child:
parse_object(child)
else:
if child['type'] == 'text/x-moz-place' and child['title'] != 'Recent Tags' and child['title'] != 'Most Visited':
dropped = ''
url = child['uri']
if 'https' in child['uri']:
dropped = child['uri'].removeprefix('https://')
else:
dropped = child['uri'].removeprefix('http://')
# print('Dropped: ' + dropped)
if 'about:reader' in dropped:
# print('Firefox Reader bookmarked!')
dropped = dropped.removeprefix('about:reader?url=')
dropped = dropped.replace('%3A', ':').replace('%2F', '/')
url = dropped
# print(dropped)
elif "read://" in dropped:
# print('Edge Reader bookmarked!')
dropped = dropped.split('?url=')[1]
dropped = dropped.replace('%3A', ':').replace('%2F', '/')
url = dropped
# print(dropped)
url = dropped
if len(url.split('/')) == 1:
print(child['uri'])
print(child)
domain = url.split('/')[0]
new_dict = {
'url': url,
'title': child['title']
}
if check_domain(all_bookmarks, domain):
for item in all_bookmarks:
if domain == item['domain'] and new_dict not in item['bookmarks']:
item['bookmarks'].append(new_dict)
else:
new_domain = {
'domain': domain,
'bookmarks': []
}
new_domain['bookmarks'].append(new_dict)
all_bookmarks.append(new_domain)
# Check if domain already in -> otherwise
# print('</p> <a href="https://{}" target="_blank"> Title: {} </a>'.format(url, child['title']))
for file in dir:
# print('File: ' + file)
with open('./data/' + file) as opened:
object_ = json.load(opened)
parse_object(object_)
for item in all_bookmarks:
link = ''
for bookmark in item['bookmarks']:
link = link + '''
<li>
<a href="https://{0}" target="_blank"> {1} </a>
</li>
'''.format(bookmark['url'], bookmark['title'])
template = '''
<details>
<summary> {0} </summary>
<ol>
{1}
</ol>
</details>
'''.format(item['domain'], link)
print(template)