Skip to content

Instantly share code, notes, and snippets.

@selfire1
Last active July 11, 2022 20:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save selfire1/bd1ebf8fd5af629f45a43cef99623236 to your computer and use it in GitHub Desktop.
Save selfire1/bd1ebf8fd5af629f45a43cef99623236 to your computer and use it in GitHub Desktop.
A template for Obsidian.md to fetch book data from the Google Book API.

// 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]]

${title}

thumbnail|150

  • 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); }

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