ff_import.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import sqlite3
  2. from marxbook import Store
  3. from pathlib import Path
  4. import sys
  5. import logging
  6. logger = logging.getLogger()
  7. def is_nongeneric_url(url):
  8. """Returns True for URLs which are non-http and non-generic.
  9. Parameters
  10. ----------
  11. url : str
  12. URL to scan.
  13. Returns
  14. -------
  15. bool
  16. True if URL is a non-generic URL, False otherwise.
  17. """
  18. ignored_prefix = ["about:", "apt:", "chrome://", "file://", "place:"]
  19. for prefix in ignored_prefix:
  20. if url.startswith(prefix):
  21. return True
  22. return False
  23. def load_firefox_database(store: Store, path):
  24. """Connect to Firefox sqlite db and import bookmarks into BukuDb.
  25. Parameters
  26. ----------
  27. path : str
  28. Path to Firefox bookmarks sqlite database.
  29. """
  30. path = Path(path).expanduser()
  31. # Connect to input DB
  32. if sys.version_info >= (3, 4, 4):
  33. # Python 3.4.4 and above
  34. conn = sqlite3.connect("file:%s?mode=ro" % path, uri=True)
  35. else:
  36. conn = sqlite3.connect(path)
  37. cur = conn.cursor()
  38. res = cur.execute(
  39. "SELECT DISTINCT fk, parent, title FROM moz_bookmarks WHERE type=1"
  40. )
  41. # get id's and remove duplicates
  42. for fk, parent_id, bm_title in res.fetchall():
  43. # get the url
  44. res = cur.execute("SELECT url FROM moz_places where id={}".format(fk))
  45. url = res.fetchone()[0]
  46. if is_nongeneric_url(url):
  47. continue
  48. # get tags
  49. res = cur.execute(
  50. "SELECT parent FROM moz_bookmarks WHERE "
  51. "fk={} AND title IS NULL".format(fk)
  52. )
  53. bm_tag_ids = [tid for item in res.fetchall() for tid in item]
  54. bookmark_tags = []
  55. for bm_tag_id in bm_tag_ids:
  56. res = cur.execute(
  57. "SELECT title FROM moz_bookmarks WHERE id={}".format(bm_tag_id)
  58. )
  59. bookmark_tags.append(res.fetchone()[0])
  60. # add folder name
  61. folder: list = []
  62. while parent_id:
  63. res = cur.execute(
  64. "SELECT title,parent FROM moz_bookmarks "
  65. "WHERE id={}".format(parent_id)
  66. )
  67. parent = res.fetchone()
  68. if parent:
  69. title, parent_id = parent
  70. if title:
  71. folder.insert(0, title)
  72. folder_name = "/".join(folder).lstrip("/")
  73. # get the title
  74. if not bm_title:
  75. bm_title = ""
  76. print(f'store.add({folder_name}, url={url}, title={bm_title}, tags={bookmark_tags})')
  77. store.add(folder_name, url=url, title=bm_title, tags=bookmark_tags)
  78. try:
  79. cur.close()
  80. conn.close()
  81. except Exception:
  82. logger.exception("Couldnt close FF db")
  83. if __name__ == "__main__":
  84. s = Store("~/.local/share/marxbook/bookmarks/")
  85. load_firefox_database(s, sys.argv[1])