-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
265 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import 'dotenv/config' | ||
import { GoogleSpreadsheet } from 'google-spreadsheet'; | ||
import { JWT } from 'google-auth-library'; | ||
import 'node-fetch'; | ||
import { setTimeout } from "timers/promises"; | ||
|
||
// Initialize auth - see https://theoephraim.github.io/node-google-spreadsheet/#/guides/authentication | ||
const serviceAccountAuth = new JWT({ | ||
// env var values here are copied from service account credentials generated by google | ||
// see "Authentication" section in docs for more info | ||
email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL, | ||
key: process.env.GOOGLE_PRIVATE_KEY, | ||
scopes: ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive.file'], | ||
}) | ||
|
||
const books = new GoogleSpreadsheet(process.env.BOOKS_SHEET_ID, serviceAccountAuth) | ||
|
||
await books.loadInfo() // loads document properties and worksheets | ||
const booksSheet = books.sheetsByTitle['Books'] // or use `doc.sheetsById[id]` or `doc.sheetsByTitle[title]` | ||
const allBooks = await booksSheet.getRows() | ||
|
||
var count = 0; | ||
var manualCount = 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 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; } | ||
|
||
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.`; | ||
} | ||
|
||
allBooks[i].set("Pages",newPages); | ||
|
||
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("Finished!"); | ||
|
||
async function getBookInfo(title, authorFirst, authorLast, series) { | ||
var formAuthor = ''; | ||
if (authorFirst != 'Various' && authorLast != 'Various') { | ||
formAuthor = `+${cleanText(authorFirst)}+${cleanText(authorLast)}`; | ||
} else { | ||
formAuthor = `+${cleanText(series.split(" ")[0])}`; | ||
} | ||
const searchString = `q=${cleanText(title)}${formAuthor}&lang=en` | ||
|
||
const path = `https://openlibrary.org/search.json?${searchString}`; | ||
const request = await fetch(path); | ||
const data = await request.json(); | ||
for (let z = 0; z < data["docs"].length; z++) { | ||
if (data["docs"][z]["number_of_pages_median"] > 0) { | ||
return data["docs"][z]; | ||
} | ||
}; | ||
return data["docs"][0]; | ||
} | ||
|
||
function cleanText(text) { | ||
return text.toLowerCase().replace(/[^\w\s]|\bthe\b|\b\d\b/g, '').trim().replace(/\s+/g, '+'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.