Skip to content

Commit

Permalink
Corrections dans l'obtention des couvertures.
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyost committed Jul 19, 2024
1 parent 11632bf commit d395a6b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions content/latex/bibliographie/dan.bib
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ @book { [DAN]
buy = "https://url.skyost.eu/agreg-dan",
url = "https://www.deboecksuperieur.com/ouvrage/9782807332195-mathematiques-pour-l-agregation-analyse-et-probabilites",
comment = "Véritable pendant de la version algèbre et géométrie, ce livre est très utile pour réviser la partie analyse du programme de prépa ainsi que pour remplir les leçons de même niveau."
altcover = "https://m.media-amazon.com/images/I/71---uqzNiL._SL1500_.jpg"
}
32 changes: 24 additions & 8 deletions modules/books-cover-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@ abstract class DownloadSource {
async downloadImage(book: Book, url: string, destinationFile: string): Promise<boolean> {
try {
const blob = await ofetch(url, { responseType: 'blob' })
let description = book.title
if (book.subtitle) {
description += ` ${book.subtitle}`
}
if (book.edition) {
description += `, Éd. ${book.edition}`
}
if (blob.type.startsWith('image/') && blob.size > 0) {
let description = book.title
if (book.subtitle) {
description += ` ${book.subtitle}`
}
if (book.edition) {
description += `, Éd. ${book.edition}`
}
const buffer = Buffer.from(await blob.arrayBuffer())
await sharp(buffer)
.resize(null, 250)
Expand Down Expand Up @@ -273,7 +273,7 @@ class OpenGraphImageDownloadSource extends DownloadSource {

override async getBookCoverUrl(book: Book): Promise<string | null> {
try {
const root: HTMLElement = await ofetch(book.website, { parseResponse: parse })
const root: HTMLElement = await ofetch(book.website, { parseResponse: parse, timeout: 10000 })
const image = root.querySelector('meta[property="og:image"]')?.getAttribute('content')
return image ?? null
}
Expand All @@ -298,4 +298,20 @@ class PreviousBuildDownloadSource extends DownloadSource {
override getBookCoverUrl(book: Book): Promise<string | null> {
return Promise.resolve(`${this.siteUrl}${this.booksImagesUrl}${book.isbn10}.jpg`)
}

override async downloadImage(book: Book, url: string, destinationFile: string): Promise<boolean> {
try {
const blob = await ofetch(url, { responseType: 'blob' })
if (blob.type === 'image/jpeg' && blob.size > 0) {
const buffer = Buffer.from(await blob.arrayBuffer())
fs.writeFileSync(destinationFile, buffer)
return true
}
return false
}
catch (ex) {
logger.warn(ex)
}
return false
}
}

0 comments on commit d395a6b

Please sign in to comment.