Skip to content

Instantly share code, notes, and snippets.

@Ninjani
Last active April 8, 2022 15:16
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ninjani/71dfd94e42f6099369ff3b8be86ad9e1 to your computer and use it in GitHub Desktop.
Save Ninjani/71dfd94e42f6099369ff3b8be86ad9e1 to your computer and use it in GitHub Desktop.
Get a book description from the title

Using with Obsidian and the Templater plugin

  1. Download kring.py somewhere and rename as just kring. (e.g. to /usr/local/bin/kring)
  2. Add a Templater template {{book}} with the content: /usr/local/bin/kring "{{note_title}}" "path/to/vault/Covers/{{note_title}}.png"
  3. CHANGE the Timeout setting to at least 20 as the command can take a while.
  4. To use, make a new note with "Book Title" or "Book Title - Author" as the note title and insert the {{book}} template.
#!/usr/bin/env python3
# pip install html2text typer
# Run as kring "<title> - <author>" "path/to/cover.png"
#!/usr/bin/env python3
from xml.etree import ElementTree as ET
from html2text import html2text
import subprocess
import typer
import typing
CALIBRE = "/Applications/calibre.app/Contents/MacOS/fetch-ebook-metadata"
def main(note_title: str, cover: typing.Optional[str] = typer.Argument(None)):
if " - " in note_title:
title, author = note_title.split(" - ")
else:
title, author = note_title, None
commands = [CALIBRE, "-t", title, "-o"]
if author is not None:
commands += ["-a", author]
if cover is not None:
commands += ["-c", cover]
book = subprocess.check_output(commands, stderr=subprocess.DEVNULL)
if cover is not None:
print(f"![[{note_title}.png]]")
root = ET.fromstring(book)
for child in root[0]:
if "description" in child.tag:
print(html2text(child.text).replace('\n', ' '))
if __name__ == "__main__":
typer.run(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment