feat: self host books

This commit is contained in:
Cory Dransfeldt 2024-04-18 13:59:09 -07:00
parent 52a6c2ed58
commit 87ae2d6297
No known key found for this signature in database
9 changed files with 53 additions and 91 deletions

View file

@ -1,63 +1,18 @@
import jsdom from 'jsdom'
import { AssetCache } from '@11ty/eleventy-fetch'
const { JSDOM } = jsdom
import { createRequire } from 'module'
const require = createRequire(import.meta.url)
const books = require('./json/read.json')
export default async function () {
const COOKIE = process.env.COOKIE_STORYGRAPH
const url = 'https://app.thestorygraph.com/currently-reading/coryd'
const asset = new AssetCache('books_data')
if (asset.isCacheValid('1h')) return await asset.getCachedValue()
const data = []
await fetch(url, {
headers: {
Cookie: COOKIE,
},
})
.then((res) => res.text())
.then((html) => {
const DOM = new JSDOM(html)
const doc = DOM.window.document
const bookCount = doc.querySelectorAll('.book-pane-content').length
const titles = doc.querySelectorAll('.book-title-author-and-series h3 > a')
const authors = doc.querySelectorAll('.book-title-author-and-series h3 p:last-of-type > a')
const images = doc.querySelectorAll('.md\\:block .book-cover img')
const urls = doc.querySelectorAll('.md\\:block .book-cover a')
const percentages = doc.querySelectorAll('.md\\:block .progress-tracker-pane .font-semibold')
const dates = doc.querySelectorAll('.md\\:block .action-menu a p')
for (let i = 0; i < bookCount; i++) {
const date = new Date(
dates[i]?.textContent.replace('Started ', '').split('\n')[0]
).toLocaleString('en-US', {
timeZone: 'America/Los_Angeles',
})
if (!data[i]) {
data.push({ title: titles[i]?.textContent })
data.push({ author: authors[i]?.textContent })
data.push({ image: `https://coryd.dev/.netlify/images/?url=${images[i]?.src}&fit=cover&w=200&h=307`,
})
data.push({ url: `https://app.thestorygraph.com${urls[i].href}` })
data.push({ percentage: percentages[i]?.textContent })
data.push({
dateAdded: date,
})
data.push({ type: 'book' })
}
if (data[i]) {
data[i]['title'] = titles[i]?.textContent
data[i]['author'] = authors[i]?.textContent
data[i]['image'] = `https://coryd.dev/.netlify/images/?url=${images[i]?.src}&fit=cover&w=200&h=307`,
data[i]['url'] = `https://app.thestorygraph.com${urls[i]?.href}`
data[i]['percentage'] = percentages[i]?.textContent
data[i]['dateAdded'] = date
data[i]['type'] = 'book'
}
}
})
const books = data.filter((book) => book.title)
await asset.save(books, 'json')
return books
return books.map(book => (
{
title: book['title'],
authors: book['authors'].length > 1 ? book['authors'].join(', ') : book['authors'][0],
description: book['description'],
image: `https://coryd.dev/.netlify/images/?url=${encodeURIComponent(book['thumbnail'].replace('&edge=curl', ''))}&fit=cover&w=200&h=307`,
url: `https://openlibrary.org/isbn/${book['isbn']}`,
dateAdded: book['dateStarted'],
status: book['status'],
type: 'book',
}
))
}

View file

@ -3,38 +3,44 @@ export default {
{
alt: 'Stay True',
author: 'Hua Hsu',
image: 'https://coryd.dev/.netlify/images/?url=https://cdn.thestorygraph.com/8jdewx4ipwb9hro2oedjkfj1cy0f&fit=cover&w=200&h=307',
url: 'https://app.thestorygraph.com/books/9946c834-81f1-4c7f-b7d1-30a804e9874f',
image: 'https://coryd.dev/.netlify/images/?url=https%3A%2F%2Fbooks.google.com%2Fbooks%2Fcontent%3Fid%3DXJ-OEAAAQBAJ%26printsec%3Dfrontcover%26img%3D1%26zoom%3D1%26source%3Dgbs_api%26w%3D512&fit=cover&w=200&h=307',
url: 'https://openlibrary.org/isbn/9780593663660',
type: 'book',
},
{
alt: 'Where Are Your Boys Tonight?',
author: 'Chris Payne',
image: 'https://coryd.dev/.netlify/images/?url=https://cdn.thestorygraph.com/vajp3jxy6kee5ka2ymbvjc2fqkvf&fit=cover&w=200&h=307',
url: 'https://app.thestorygraph.com/books/f074d4e3-a9fc-42af-889e-54697a1fece0',
image: 'https://coryd.dev/.netlify/images/?url=https%3A%2F%2Fbooks.google.com%2Fbooks%2Fcontent%3Fid%3DsQtcEAAAQBAJ%26printsec%3Dfrontcover%26img%3D1%26zoom%3D1%26source%3Dgbs_api%26w%3D512&fit=cover&w=200&h=307',
url: 'https://openlibrary.org/isbn/9780063161573',
type: 'book',
},
{
title: 'Trouble Boys',
author: 'Bob Mehr',
image: 'https://coryd.dev/.netlify/images/?url=https://cdn.thestorygraph.com/66eh71z4igv2dsinrk7mif50fa6y&fit=cover&w=200&h=307',
url: 'https://app.thestorygraph.com/books/8dce0e20-fef1-42a4-a59b-b4ec084dc6f4',
image: 'https://coryd.dev/.netlify/images/?url=https%3A%2F%2Fbooks.google.com%2Fbooks%2Fcontent%3Fid%3DxQ9SCwAAQBAJ%26printsec%3Dfrontcover%26img%3D1%26zoom%3D1%26source%3Dgbs_api%26w%3D512&fit=cover&w=200&h=307',
url: 'https://openlibrary.org/isbn/9780306818790',
type: 'book',
},
{
alt: 'Corporate Rock Sucks',
author: 'Jim Ruland',
image: 'https://coryd.dev/.netlify/images/?url=https://cdn.thestorygraph.com/tzf2l7725ydzzvvmzpbky7wj7ckc&fit=cover&w=200&h=307',
url: 'https://app.thestorygraph.com/books/8a0b8649-8939-4753-8e8d-18500574614e',
image: 'https://coryd.dev/.netlify/images/?url=https%3A%2F%2Fbooks.google.com%2Fbooks%2Fcontent%3Fid%3DPEU7EAAAQBAJ%26printsec%3Dfrontcover%26img%3D1%26zoom%3D1%26source%3Dgbs_api%26w%3D512&fit=cover&w=200&h=307',
url: 'https://openlibrary.org/isbn/9780306925474',
type: 'book',
},
{
alt: 'Tracers in the Dark',
author: 'Andy Greenberg',
image: 'https://coryd.dev/.netlify/images/?url=https://cdn.thestorygraph.com/m4s6lp9eljzk5vjm1xauou8frxde&fit=cover&w=200&h=307',
url: 'https://app.thestorygraph.com/books/4f1f21f8-3d1f-4162-9f6c-5a00a33f629c',
image: 'https://coryd.dev/.netlify/images/?url=https%3A%2F%2Fbooks.google.com%2Fbooks%2Fcontent%3Fid%3DuytfEAAAQBAJ%26printsec%3Dfrontcover%26img%3D1%26zoom%3D1%26source%3Dgbs_api%26w%3D512&fit=cover&w=200&h=307',
url: 'http://openlibrary.org/isbn/9780385548106',
type: 'book',
},
{
alt: 'Girl in a Band',
author: 'Kim Gordon',
image: 'https://coryd.dev/.netlify/images/?url=https://cdn.thestorygraph.com/qnxw68i4xn3byegvkzq6kty0rlx7&fit=cover&w=200&h=307',
url: 'https://app.thestorygraph.com/books/c0ea8ac6-d2c6-43b6-be16-ba793e71bfc2',
image: 'https://coryd.dev/.netlify/images/?url=https%3A%2F%2Fbooks.google.com%2Fbooks%2Fcontent%3Fid%3DJNOIBAAAQBAJ%26printsec%3Dfrontcover%26img%3D1%26zoom%3D1%26source%3Dgbs_api%26w%3D512&fit=cover&w=200&h=307',
url: 'https://openlibrary.org/isbn/9780062295910',
type: 'book',
}
],
albums: [