FantLab minor refactor

This commit is contained in:
advplyr 2023-02-11 14:25:25 -06:00
parent 371cd3b2e5
commit f35c96e118

View File

@ -39,7 +39,7 @@ class FantLab {
}) })
return Promise.all(items.map(async item => await this.getWork(item))).then(resArray => { return Promise.all(items.map(async item => await this.getWork(item))).then(resArray => {
return resArray.filter(res => res != null) return resArray.filter(res => res)
}) })
} }
@ -54,7 +54,7 @@ class FantLab {
const bookData = await axios.get(url).then((resp) => { const bookData = await axios.get(url).then((resp) => {
return resp.data || null return resp.data || null
}).catch((error) => { }).catch((error) => {
Logger.error(`[FantLab] work info reques error`, error) Logger.error(`[FantLab] work info request for url "${url}" error`, error)
return null return null
}) })
@ -65,44 +65,30 @@ class FantLab {
let { authors, work_name_alts, work_id, work_name, work_year, work_description, image, classificatory, editions_blocks } = bookData let { authors, work_name_alts, work_id, work_name, work_year, work_description, image, classificatory, editions_blocks } = bookData
const subtitle = Array.isArray(work_name_alts) ? work_name_alts[0] : null const subtitle = Array.isArray(work_name_alts) ? work_name_alts[0] : null
const auth = authors.map(function (author) { const authorNames = authors.map(au => (au.name || '').trim()).filter(au => au)
return author.name
})
const genres = classificatory ? this.tryGetGenres(classificatory) : []
const imageAndIsbn = await this.tryGetCoverFromEditions(editions_blocks) const imageAndIsbn = await this.tryGetCoverFromEditions(editions_blocks)
if (imageAndIsbn) { const imageToUse = imageAndIsbn?.imageUrl || image
var { imageUrl, isbn } = imageAndIsbn
if (imageUrl) {
image = imageUrl
}
}
const cover = 'https://fantlab.ru' + image
return { return {
id: work_id, id: work_id,
title: work_name, title: work_name,
subtitle: subtitle || null, subtitle: subtitle || null,
author: auth ? auth.join(', ') : null, author: authorNames.length ? authorNames.join(', ') : null,
publisher: null, publisher: null,
publishedYear: work_year, publishedYear: work_year,
description: work_description, description: work_description,
cover: image ? cover : null, cover: imageToUse ? `https://fantlab.ru${imageToUse}` : null,
genres: genres, genres: this.tryGetGenres(classificatory),
isbn: isbn isbn: imageAndIsbn?.isbn || null
} }
} }
tryGetGenres(classificatory) { tryGetGenres(classificatory) {
const { genre_group } = classificatory if (!classificatory || !classificatory.genre_group) return []
if (!genre_group) {
return [] const genresGroup = classificatory.genre_group.find(group => group.genre_group_id == 1) // genres and subgenres
}
const genresGroup = genre_group.find(group => group.genre_group_id == 1) // genres and subgenres
// genre_group_id=2 - General Characteristics // genre_group_id=2 - General Characteristics
// genre_group_id=3 - Arena // genre_group_id=3 - Arena
@ -111,10 +97,9 @@ class FantLab {
// genre_group_id=7 - Story linearity // genre_group_id=7 - Story linearity
// genre_group_id=5 - Recommended age of the reader // genre_group_id=5 - Recommended age of the reader
if (!genresGroup) return [] if (!genresGroup || !genresGroup.genre || !genresGroup.genre.length) return []
const { genre } = genresGroup const rootGenre = genresGroup.genre[0]
const rootGenre = genre[0]
const { label } = rootGenre const { label } = rootGenre
@ -122,51 +107,46 @@ class FantLab {
} }
tryGetSubGenres(rootGenre) { tryGetSubGenres(rootGenre) {
const { genre } = rootGenre if (!rootGenre.genre || !rootGenre.genre.length) return []
return genre ? genre.map(genreObj => genreObj.label) : [] return rootGenre.genre.map(g => g.label).filter(g => g)
} }
async tryGetCoverFromEditions(editions) { async tryGetCoverFromEditions(editions) {
if (!editions) { if (!editions) {
return null return null
} }
let bookEditions = editions['30'] // try get audiobooks first // 30 = audio, 10 = paper
if (!bookEditions) { // Prefer audio if available
bookEditions = editions['10'] // paper editions in ru lang const bookEditions = editions['30'] || editions['10']
} if (!bookEditions || !bookEditions.list || !bookEditions.list.length) {
if (!bookEditions) {
return null return null
} }
const { list } = bookEditions const lastEdition = bookEditions.list.pop()
const lastEdition = list[list.length - 1]
const editionId = lastEdition['edition_id'] const editionId = lastEdition['edition_id']
const isbn = lastEdition['isbn'] // get only from paper edition const isbn = lastEdition['isbn'] || null // get only from paper edition
return { return {
imageUrl: await this.getCoverFromEdition(editionId), imageUrl: await this.getCoverFromEdition(editionId),
isbn: isbn isbn
} }
} }
async getCoverFromEdition(editionId) { async getCoverFromEdition(editionId) {
if (!editionId) return null
const url = `${this._baseUrl}/edition/${editionId}` const url = `${this._baseUrl}/edition/${editionId}`
const editionInfo = await axios.get(url).then((resp) => { const editionInfo = await axios.get(url).then((resp) => {
return resp.data || null return resp.data || null
}).catch(error => { }).catch(error => {
Logger.error('[FantLab] search cover from edition error', error) Logger.error(`[FantLab] search cover from edition with url "${url}" error`, error)
return null return null
}) })
return editionInfo ? editionInfo['image'] : null return editionInfo?.image || null
} }
} }
module.exports = FantLab module.exports = FantLab