<%* 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]]
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") %>
Mil gracias por compartirlo Isma !