From 5168eaf3c7d1808625a3918b0f133bdf26d9c7ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20N=C3=A1jera?= Date: Mon, 28 Nov 2022 15:50:52 +0100 Subject: import notes from kindle to org file --- bin/kindlenotes2org.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 bin/kindlenotes2org.py (limited to 'bin') diff --git a/bin/kindlenotes2org.py b/bin/kindlenotes2org.py new file mode 100644 index 0000000..1769c8c --- /dev/null +++ b/bin/kindlenotes2org.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +r""" +extract book highlights to orgmode +================================== + +Extracts from exported html notes from kindle to org subtree +""" +# Created: Sat Jul 14 02:10:24 2018 +# Author: Óscar Nájera +# License: GPL-3 + +import argparse +from textwrap import fill as filltxt + +from bs4 import BeautifulSoup + + +def html_notes2org(page): + soup = BeautifulSoup(page, "lxml") + + docs = "" + for div in soup.find_all("div"): + hcl = div.attrs.get("class") + if "bookTitle" in hcl: + docs += "* {}".format(div.text.strip()) + if "authors" in hcl: + docs += " -- {}\n".format(div.text.strip()) + if "sectionHeading" in hcl: + docs += "** " + div.text.lstrip() + if "noteText" in hcl: + docs += "#+begin_quote\n{}\n#+end_quote\n\n".format( + filltxt(div.text.strip()) + ) + + return docs + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("file", help="HTML file from kindle export") + args = parser.parse_args() + + with open(args.file, "rb") as fid: + page = fid.read() + + docs = html_notes2org(page) + + with open("sub.org", "w") as fid: + fid.write(docs) + + +if __name__ == "__main__": + main() -- cgit v1.2.3