feat: self host books
This commit is contained in:
parent
52a6c2ed58
commit
87ae2d6297
9 changed files with 53 additions and 91 deletions
|
@ -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',
|
||||
}
|
||||
))
|
||||
}
|
||||
|
|
Reference in a new issue