chore: refactor books

This commit is contained in:
Cory Dransfeldt 2023-10-30 12:55:47 -07:00
parent a1526cdd09
commit 661c5c10b4

View file

@ -17,47 +17,53 @@ module.exports = async function () {
.then((html) => { .then((html) => {
const DOM = new JSDOM(html) const DOM = new JSDOM(html)
const doc = DOM.window.document const doc = DOM.window.document
doc const bookCount = doc.querySelectorAll('.md\\:block .book-pane-content').length
.querySelectorAll('.md\\:block .book-title-author-and-series h3 > a') const titles = doc.querySelectorAll('.md\\:block .book-title-author-and-series h3 > a')
.forEach((title, index) => { const authors = doc.querySelectorAll(
if (!data[index]) data.push({ title: title.textContent }) '.md\\:block .book-title-author-and-series h3 p:last-of-type > a'
if (data[index]) data[index]['title'] = title.textContent )
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++) {
if (!data[i]) {
data.push({ title: titles[i].textContent })
data.push({ author: authors[i].textContent })
data.push({
image: images[i].src.replace(
'https://cdn.thestorygraph.com',
'https://books.coryd.dev'
),
}) })
doc data.push({ url: `https://app.thestorygraph.com${urls[i].href}` })
.querySelectorAll('.md\\:block .book-title-author-and-series h3 p:last-of-type > a') data.push({ percentage: percentages[i].textContent || '0%' })
.forEach((author, index) => { data.push({
if (!data[index]) data.push({ author: author.textContent }) dateAdded: dates[i]
if (data[index]) data[index]['author'] = author.textContent ? new Date(dates[i].textContent.replace('Started ', '').split('\n')[0])
: new Date(),
}) })
doc.querySelectorAll('.md\\:block .book-cover img').forEach((image, index) => { data.push({ type: 'book' })
const img = image.src.replace('https://cdn.thestorygraph.com', 'https://books.coryd.dev') }
if (!data[index]) data.push({ image: img })
if (data[index]) data[index]['image'] = img if (data[i]) {
}) data[i]['title'] = titles[i].textContent
doc.querySelectorAll('.md\\:block .book-cover a').forEach((url, index) => { data[i]['author'] = authors[i].textContent
if (!data[index]) data.push({ url: `https://app.thestorygraph.com${url.href}` }) data[i]['image'] = images[i].src.replace(
if (data[index]) data[index]['url'] = `https://app.thestorygraph.com${url.href}` 'https://cdn.thestorygraph.com',
}) 'https://books.coryd.dev'
doc )
.querySelectorAll('.md\\:block .progress-tracker-pane .font-semibold') data[i]['url'] = `https://app.thestorygraph.com${urls[i].href}`
.forEach((percentage, index) => { data[i]['percentage'] = percentages[i].textContent || '0%'
if (!data[index]) data.push({ percentage: percentage.textContent }) data[i]['dateAdded'] = dates[i]
if (data[index]) data[index]['percentage'] = percentage.textContent ? new Date(dates[i].textContent.replace('Started ', '').split('\n')[0])
}) : new Date()
doc.querySelectorAll('.md\\:block .action-menu a > p').forEach((dateStarted, index) => { data[i]['type'] = 'book'
const date = new Date(dateStarted.textContent.replace('Started ', '').split('\n')[0]) }
if (!data[index]) data.push({ dateAdded: date }) }
if (data[index]) data[index]['dateAdded'] = date
})
})
const books = data
.filter((book) => book.title)
.map((book) => {
book.type = 'book'
if (!('dateAdded' in book)) book.dateAdded = new Date()
if (!('percentage' in book)) book.percentage = '0%'
return book
}) })
const books = data.filter((book) => book.title)
await asset.save(books, 'json') await asset.save(books, 'json')
return books return books
} }