marxbook/misc/ff_import.py
2021-03-21 12:09:07 +01:00

104 行
2.8 KiB
Python
可执行文件

import sqlite3
from marxbook import Store
from pathlib import Path
import sys
import logging
logger = logging.getLogger()
def is_nongeneric_url(url):
"""Returns True for URLs which are non-http and non-generic.
Parameters
----------
url : str
URL to scan.
Returns
-------
bool
True if URL is a non-generic URL, False otherwise.
"""
ignored_prefix = ["about:", "apt:", "chrome://", "file://", "place:"]
for prefix in ignored_prefix:
if url.startswith(prefix):
return True
return False
def load_firefox_database(store: Store, path):
"""Connect to Firefox sqlite db and import bookmarks into BukuDb.
Parameters
----------
path : str
Path to Firefox bookmarks sqlite database.
"""
path = Path(path).expanduser()
# Connect to input DB
if sys.version_info >= (3, 4, 4):
# Python 3.4.4 and above
conn = sqlite3.connect("file:%s?mode=ro" % path, uri=True)
else:
conn = sqlite3.connect(path)
cur = conn.cursor()
res = cur.execute(
"SELECT DISTINCT fk, parent, title FROM moz_bookmarks WHERE type=1"
)
# get id's and remove duplicates
for fk, parent_id, bm_title in res.fetchall():
# get the url
res = cur.execute("SELECT url FROM moz_places where id={}".format(fk))
url = res.fetchone()[0]
if is_nongeneric_url(url):
continue
# get tags
res = cur.execute(
"SELECT parent FROM moz_bookmarks WHERE "
"fk={} AND title IS NULL".format(fk)
)
bm_tag_ids = [tid for item in res.fetchall() for tid in item]
bookmark_tags = []
for bm_tag_id in bm_tag_ids:
res = cur.execute(
"SELECT title FROM moz_bookmarks WHERE id={}".format(bm_tag_id)
)
bookmark_tags.append(res.fetchone()[0])
# add folder name
folder: list = []
while parent_id:
res = cur.execute(
"SELECT title,parent FROM moz_bookmarks "
"WHERE id={}".format(parent_id)
)
parent = res.fetchone()
if parent:
title, parent_id = parent
if title:
folder.insert(0, title)
folder_name = "/".join(folder).lstrip("/")
# get the title
if not bm_title:
bm_title = ""
print(f'store.add({folder_name}, url={url}, title={bm_title}, tags={bookmark_tags})')
store.add(folder_name, url=url, title=bm_title, tags=bookmark_tags)
try:
cur.close()
conn.close()
except Exception:
logger.exception("Couldnt close FF db")
if __name__ == "__main__":
s = Store("~/.local/share/marxbook/bookmarks/")
load_firefox_database(s, sys.argv[1])