Skip to content

Commit

Permalink
Book data
Browse files Browse the repository at this point in the history
  • Loading branch information
niclake committed Jan 9, 2025
1 parent b7d6b7f commit 0fee594
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 24 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.cache
.DS_Store
.env
.vscode
_site
node_modules
public
Expand Down
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"cSpell.words": [
"littlefoot",
"wdth",
"wght"
]
}
21 changes: 21 additions & 0 deletions .vscode/snippets.code-snippets
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
// Place your niclake.github.io workspace snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
// is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
// used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
// Placeholders with the same ids are connected.
// Example:
"Print to console": {
"scope": "javascript,typescript",
"prefix": "log",
"body": [
"console.log('$1');",
"$0"
],
"description": "Log output to console"
}

// Snippet for a new wishlist item

}
50 changes: 37 additions & 13 deletions cli/backlogs/bookPages.js → cli/backlogs/bookData.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { GoogleSpreadsheet } from 'google-spreadsheet';
import { JWT } from 'google-auth-library';
import 'node-fetch';
import { setTimeout } from "timers/promises";
import { log } from 'console';

// Initialize auth - see https://theoephraim.github.io/node-google-spreadsheet/#/guides/authentication
const serviceAccountAuth = new JWT({
Expand All @@ -21,38 +22,61 @@ const allBooks = await booksSheet.getRows()

var count = 0;
var manualCount = 0;
var manualIsbnCount = 0;

for (var i = 0; i < allBooks.length; i++) {
if (count >= 30) { console.log("Pausing..."); await setTimeout(30000); count = 0; }
const currPages = allBooks[i].get("Pages");
const currIsbn = allBooks[i].get("ISBN");
const title = allBooks[i].get("Title");
const authorFirst = allBooks[i].get("Author First");
const authorLast = allBooks[i].get("Author Last");
const series = allBooks[i].get("Series");

if (!Number.isNaN(currPages) && !isNaN(parseFloat(currPages))) { continue; }

log(`Processing ${title}...`);

// Stop if we already have a page count and ISBN
if (!Number.isNaN(currPages) && !isNaN(parseFloat(currPages)) && currIsbn.length > 0) { continue; }
// Skip if both are manual
if (currPages == "Manual" && currIsbn == "Manual") { continue; }

const bookInfo = await getBookInfo(title, authorFirst, authorLast, series);

var newPages = '';
var saveMessage = '';
if (bookInfo && bookInfo["number_of_pages_median"] && bookInfo["number_of_pages_median"] > 1) {
newPages = bookInfo["number_of_pages_median"];
saveMessage = `Saved ${title} (${newPages} pages)!`;
} else {
manualCount++;
newPages = "Manual";
saveMessage = `Saved ${title} - needs manual pages.`;
}
var saveMessage = `Saved ${title}`;

allBooks[i].set("Pages",newPages);
if (Number.isNaN(currPages) || isNaN(parseFloat(currPages))) {
var newPages = '';
if (bookInfo && bookInfo["number_of_pages_median"] && bookInfo["number_of_pages_median"] > 1) {
newPages = bookInfo["number_of_pages_median"];
saveMessage += ` (${newPages} pages)`;
} else {
manualCount++;
newPages = "Manual";
saveMessage += ` - needs manual pages`;
}
allBooks[i].set("Pages",newPages);
}

if (!currIsbn.length > 0 || currIsbn == "Manual") {
var newIsbn = '';
if (bookInfo && bookInfo["isbn"]) {
newIsbn = bookInfo["isbn"][0];
saveMessage += `, ISBN: ${newIsbn}`;
} else {
manualIsbnCount++;
newIsbn = "Manual";
saveMessage += `, ISBN needs manual entry.`;
}
allBooks[i].set("ISBN",newIsbn);
}

await allBooks[i].save().then(() => console.log(`${saveMessage}`)).catch((err) => console.log(err));
count++;
}

console.log(`Total books: ${allBooks.length}`);
console.log(`Books needing manual pagecounts: ${manualCount}`);
console.log(`Books needing manual ISBNs: ${manualIsbnCount}`);
console.log("Finished!");

async function getBookInfo(title, authorFirst, authorLast, series) {
Expand Down
44 changes: 34 additions & 10 deletions cli/backlogs/books.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import fs from 'fs'
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename)
const __siteroot = __dirname.replace('/cli/backlogs', '');
const __target = '/src/_data/backlogs/'
const __target = '/src/_data/catalog/'
const __targetFile = 'books.json'

// Initialize auth - see https://theoephraim.github.io/node-google-spreadsheet/#/guides/authentication
const serviceAccountAuth = new JWT({
Expand All @@ -26,24 +27,47 @@ const booksSheet = books.sheetsByTitle['Books'] // or use `doc.sheetsById[id]` o
const allBooks = await booksSheet.getRows()
const jsonArr = []
for (var i = 0; i < allBooks.length; i++) {
// Skip unless there's a status or read status
if (allBooks[i].get("Status") === "" && allBooks[i].get("Read?") === "") { continue; }

jsonArr.push({
title: allBooks[i].get("Title"),
authorFirst: allBooks[i].get("Author First"),
authorLast: allBooks[i].get("Author Last"),
genre: allBooks[i].get("Genre"),
series: allBooks[i].get("Series"),
owned: allBooks[i].get("Owned?"),
read: allBooks[i].get("Read?"),
read: (allBooks[i].get("Read?") === "X") ? true : false,
compDate: allBooks[i].get("Comp Date"),
status: allBooks[i].get("Status"),
information: allBooks[i].get("Information"),
rating: getStars(allBooks[i].get("Rating")),
status: trimStatus(allBooks[i].get("Status")),
pages: allBooks[i].get("Pages"),
isbn: allBooks[i].get("ISBN"),
})
}
// console.log(allBooks[8]._rawData.slice(0,10))
const __targetFile = 'books.json'

fs.writeFile(__siteroot + __target + __targetFile, JSON.stringify(jsonArr), function(err) {
if (err) {
console.log(err)
}
})
})

function getStars(rating) {
var stars = ''
if (rating === "") {
return stars
}
const fullStar = "★";
const halfStar = "½";
for (var i = 1; i <= rating; i++) {
stars += fullStar
}
if (rating % 1 !== 0) {
stars += halfStar
}
return stars
}

function trimStatus(status) {
if (status === "") {
return ""
}
return status.substring(2).trim()
}
1 change: 1 addition & 0 deletions src/_data/catalog/books.json

Large diffs are not rendered by default.

0 comments on commit 0fee594

Please sign in to comment.