// Author: Joschua // Original gist: https://gist.github.com/selfire1/bd1ebf8fd5af629f45a43cef99623236
// Set the amount of search results to fetch maxResults = 3 // Opens a dialogue for book to search for bookQuery = await tp.system.prompt("Enter book to search for ππ");
// Calls the Google Book API to fetch book data let url = "https://www.googleapis.com/books/v1/volumes?q=" + bookQuery + "&maxResults=" + maxResults.toString() let response = await fetch(url);
if (response.ok) { // if the response is okay (HTTP-status is 200-299) // Call API // Parse the return let objectText = await response.text(); let parsedCall = JSON.parse(objectText); // I need to parse this twice to get all results somehow?
// Inititalise two arrays: results for the search (book titles and author) and length (0, 1, 2, 3)
let searchResults = [];
let lengthResults = [];
// Fill both arrays
for (let i = 0; i < maxResults; i++) {
if (parsedCall.items[i].saleInfo.isEbook) {
searchResults.push(parsedCall.items[i].volumeInfo.title + " (ebook)\n" + parsedCall.items[i].volumeInfo.authors[0]);
} else {
searchResults.push(parsedCall.items[i].volumeInfo.title + "\n" + parsedCall.items[i].volumeInfo.authors[0]);
}
lengthResults.push(i)
}
// Display results in a list for the user to choose one
const chosenBookNo = await tp.system.suggester(searchResults, lengthResults, "", "Choose a book!");
// Log chosen book
console.log(parsedCall.items[chosenBookNo]);
// Fill variables from book selected
// Predefined
let todayDate = tp.date.now("YYYY-MM-DD");
// Automatically from API
let pageCount = parsedCall.items[chosenBookNo].volumeInfo.pageCount;
let title = parsedCall.items[chosenBookNo].volumeInfo.title;
let encoded = encodeURI(title);
let author = parsedCall.items[chosenBookNo].volumeInfo.authors[0];
let published = parsedCall.items[chosenBookNo].volumeInfo.publishedDate.slice(0, 4);
// Note: the thumbnail provided from Google often doesn't work or has poor quality
// Usually I copy and paste the thumbnail url manually from amazon.
// Comment out the below code
let thumbnailLink = parsedCall.items[chosenBookNo].volumeInfo.imageLinks.thumbnail;
// and uncomment this code for a manual solution:
// uri = `https://www.amazon.com/s?k=${encoded}`;
// window.open(uri);
// let thumbnailLink = await tp.system.prompt("Kindle URL", "", "");
// FROM USER
let libaryURL = "";
let uri = "";
let ddcNumber = "";
let kindleURL = "";
let series = "";
const bookType = "";
// Genre of book
var extraInfo = confirm("Do you want to add extra info?");
if (extraInfo == true) {
bookType = await tp.system.suggester(["βοΈ Fiction", "π€ Non-Fiction", "π€ Biography"], ["fiction", "nonfiction", "biographical"], "", "Choose a genre!");
// Series
series = await tp.system.prompt("Enter series", "");
if (series != null) {
series = `\n* Universe/Series: [[${series}]]`
}
// LIBRARY: Open libray url
uri = `https://libris.kb.se/hitlist?q=${encoded}&r=;spr:eng`;
window.open(uri);
// Respond if in library
var inLibrary = await tp.system.suggester(["β
In library", "β Not in library"], ["Y", "N"], "", "Is the book in the library?");
if (inLibrary === "Y") {
libaryURL = await tp.system.prompt("Library URL", "", "");
}
// DDC: Enter ddc number
uri = `http://classify.oclc.org/classify2/ClassifyDemo?search-title-txt=${encoded}`;
window.open(uri);
ddcNumber = await tp.system.prompt("DDC number", "", "");
// KINDLE: Enter kindle link
uri = `https://www.amazon.com/s?k=${encoded}`;
window.open(uri);
kindleURL = await tp.system.prompt("Kindle URL", "", "");
// Thumbnail URL
// uri = kindleURL;
// window.open(uri);
thumbnailLink = await tp.system.prompt("Thumbnail link", "", "");
}
// Output for template
tR += `---
added: ${todayDate}
status: to-read
read-next: false
pages: $ {pageCount}
ddc: ${ddcNumber}
owned: false
library: $ {inLibrary}
urls:
library-url: ${libaryURL}
kindle-url: $ {kindleURL}
thumbnail-url: ${thumbnailLink}
up:: [[π Books]]
- Type: #book/${bookType}
- Author:: [[${author}]]${series}
- Year published:: [[${published}]]
`
await tp.file.rename(title);
if (tp.file.folder != 'Books') {
tp.file.move('' + tp.file.title)
}
// Create file
if (!tp.file.exists(author)) {
var authorTemplate = tp.file.find_tfile("βοΈ Authors");
tp.file.create_new(authorTemplate, author, true, "Books/Author");
}
} else { // if there is an error with the call alert("HTTP-Error: " + response.status); }