Skip to content

Instantly share code, notes, and snippets.

@ismanapa
Created September 5, 2022 14:34
Show Gist options
  • Save ismanapa/48f58ae795395a289bdc195c45b19e5d to your computer and use it in GitHub Desktop.
Save ismanapa/48f58ae795395a289bdc195c45b19e5d to your computer and use it in GitHub Desktop.
New book template for Obsidian and Templater

<%* const http = require("https"); const fs = require("fs");

isbn = await tp.system.prompt("ISBN");

const downloadFile = (url) => { return new Promise((resolve) => { const path = ${tp.file.path().split("/").slice(0,-1).join("/")}/attachments; const fileName = url.split("/").pop();

if (!fs.existsSync(path)){
	fs.mkdirSync(path);
}

const file = fs.createWriteStream(`${path}/${fileName}`);
const request = http
  .get(url, function (response) {
    response.pipe(file);
    file.on("finish", function () {
      file.close();
      resolve(fileName);
    });
  })
  .on("error", function (err) {
    // Handle errors
    console.log(err.message);
  });

}); };

async function getBookData() { const response = await fetch( https://openlibrary.org/api/books?bibkeys=ISBN:${isbn}&jscmd=data&format=json ); const data = await response.json();

if (response.ok) { const book = data[ISBN:${isbn}]; title = book.title; await tp.file.rename(${title});

coverUrl = book.cover.large;
cover = await downloadFile(coverUrl);

authors = book.authors;
identifiers = book.identifiers;
pages = book.number_of_pages;
url = book.url;

} else { quote = "An error occured"; console.log(data); } }

await getBookData(); tR += "---"; // hack para evitar que haya un salto de línea extra en la cabecera %> tags: source/book authors: <%* tR += authors.map(author => "${author.name}").join(" ") %> created: <% tp.date.now("YYYY-MM-DD") %>

![[<%* tR += ${cover} %>|200]]

Información

Páginas:: <%* tR += ${pages} %> Autores:: <%* tR += authors.map(author => tp.file.exists(author.name) ? [[${author.name}]] : ${author.name}).join(" ") %> <%* tR += Object.keys(identifiers).map(id => **${id}**:: ${identifiers[id]}).join("\n") %>

Notas

@RubenZagon
Copy link

Mil gracias por compartirlo Isma !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment