Skip to content

Instantly share code, notes, and snippets.

@jermspeaks
Created January 30, 2024 21:45
Show Gist options
  • Save jermspeaks/209c47e99ee6e32ef83e9e2da40c3e36 to your computer and use it in GitHub Desktop.
Save jermspeaks/209c47e99ee6e32ef83e9e2da40c3e36 to your computer and use it in GitHub Desktop.
Using the QuickAdd Obsidian plugin, the BookFinder uses Google API Books v1 to fetch the book and make a note
const notice = (e) => new Notice(e, 5e3);
const log = (e) => console.log(e);
const logError = (e) => console.error(e);
const GOOGLE_BOOKS_API_URL = "https://www.googleapis.com/books/v1/volumes";
const GOOGLE_BOOKS_TITLE_TERM = "intitle:";
function replaceIllegalFileNameCharactersInString(e) {
return e.replace(/[\\,#%&\{\}\/*<>?$\'\":@]*/g, "");
}
function formatTitleForSuggestion(resultItem) {
return `${resultItem.volumeInfo.title} - ${
resultItem.volumeInfo.authors ? resultItem.volumeInfo.authors[0] : ""
} (${new Date(resultItem.volumeInfo.publishedDate).getFullYear()})`;
}
function getISBNs(industryIdentifiers = []) {
let ISBN10 = "";
let ISBN13 = "";
if (industryIdentifiers) {
const isbn10_data = industryIdentifiers.find(
(identifier) => identifier.type === "ISBN_10"
);
const isbn13_data = industryIdentifiers.find(
(identifier) => identifier.type === "ISBN_13"
);
if (isbn10_data) ISBN10 = isbn10_data.identifier;
if (isbn13_data) ISBN13 = isbn13_data.identifier;
}
return { ISBN10, ISBN13 };
}
const getPublicationYear = (publishedDate) => {
if (!publishedDate) return "";
return new Date(publishedDate).getFullYear();
};
/**
* Add a book to Obsidian vault
* @param {Object} params Quick add object
* @param {Object} settings Any settings that can be passed to the function
*/
const addBook = async function (params) {
const QuickAdd = params;
const clipboard = await QuickAdd.quickAddApi.utility.getClipboard();
// Assuming the clipboard will have the book title
const inputResponse = await QuickAdd.quickAddApi.inputPrompt(
"Enter Book title: ",
clipboard,
clipboard
);
if (!inputResponse)
throw (notice("No title entered.", 5e3), new Error("No title entered."));
try {
const encodedTitle = encodeURIComponent(
GOOGLE_BOOKS_TITLE_TERM + inputResponse
);
const link = GOOGLE_BOOKS_API_URL + "?q=" + encodedTitle + "&maxResults=10";
const response = await fetch(link);
const jsonResponse = await response.json();
if (jsonResponse?.error || !Array.isArray(jsonResponse?.items)) {
notice("Request failed");
throw new Error("Request failed");
}
if (jsonResponse.items.length == 0) {
notice("No results found.");
throw new Error("No results found.");
}
const searchResults = jsonResponse.items;
const selection = await QuickAdd.quickAddApi.suggester(
searchResults.map(formatTitleForSuggestion),
searchResults
);
if (!selection) {
notice("No choice selected.");
throw new Error("No choice selected.");
}
const selectedBook = selection.volumeInfo;
// Here is the full list of properties from the Google Books API response:
// allowAnonLogging
// authors
// canonicalVolumeLink
// categories
// contentVersion
// description
// imageLinks
// industryIdentifiers
// infoLink
// language
// maturityRating
// pageCount
// panelizationSummary
// previewLink
// printType
// publishedDate
// publisher
// readingModes
// subtitle
// title
const isbns = getISBNs(selectedBook.industryIdentifiers);
const variables = {
...selectedBook,
authors: selectedBook.authors,
categories: selectedBook.categories,
description: selectedBook.description,
fileName: replaceIllegalFileNameCharactersInString(selectedBook.title),
isbn10: isbns.ISBN10,
isbn13: isbns.ISBN13,
pageCount: selectedBook.pageCount,
Poster: selectedBook.imageLinks.thumbnail,
publishedDate: selectedBook.publishedDate,
publisher: selectedBook.publisher,
subtitle: selectedBook.subtitle,
title: selectedBook.title,
year: getPublicationYear(selectedBook.publishedDate),
};
// log("--- Variables ---", variables);
QuickAdd.variables = variables;
} catch (error) {
logError(error);
}
};
module.exports = {
entry: addBook,
settings: {
name: "BookFinder",
author: "Jeremy Wong",
},
};
by title tags in year published related category pages publisher isbn10 isbn13 created
{{VALUE:authors}}
{{VALUE:title}}
#highlights/waiting
[[Books]]
{"VALUE:year"=>nil}
{{VALUE:publishedDate}}
{{VALUE:categories}}
{{VALUE:pageCount}}
{{VALUE:publisher}}
{{VALUE:isbn10}}
{{VALUE:isbn13}}
{{date:YYYY-MM-DD}}

{{VALUE:title}}

poster

  • Subtitle: {{VALUE:subtitle}}

My Notes

Details

{{VALUE:description}}

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