FantLab match provider fixes after code review

This commit is contained in:
Dmitry Naboychenko 2023-02-08 22:32:27 +03:00
parent 36e44e902a
commit b9307143bd

View File

@ -19,57 +19,59 @@ class FantLab {
// 56 - anthology series // 56 - anthology series
// 57 - newspaper // 57 - newspaper
// types can get here https://api.fantlab.ru/config.json // types can get here https://api.fantlab.ru/config.json
_filterWorkType = [7, 11, 12, 22, 23, 24, 25, 26, 46, 47, 49, 51, 52, 55, 56, 57]; _filterWorkType = [7, 11, 12, 22, 23, 24, 25, 26, 46, 47, 49, 51, 52, 55, 56, 57]
_baseUrl = 'https://api.fantlab.ru' _baseUrl = 'https://api.fantlab.ru'
constructor() { } constructor() { }
async search(title, author) { async search(title, author) {
var searchString = encodeURIComponent(title) let searchString = encodeURIComponent(title)
if (author) { if (author) {
searchString += encodeURIComponent(' ' + author) searchString += encodeURIComponent(' ' + author)
} }
var url = `${this._baseUrl}/search-works?q=${searchString}&page=1&onlymatches=1` const url = `${this._baseUrl}/search-works?q=${searchString}&page=1&onlymatches=1`
Logger.debug(`[FantLab] Search url: ${url}`) Logger.debug(`[FantLab] Search url: ${url}`)
var items = await axios.get(url).then((res) => { const items = await axios.get(url).then((res) => {
return res.data || [] return res.data || []
}).catch(error => { }).catch(error => {
Logger.error('[FantLab] search error', error) Logger.error('[FantLab] search error', error)
return [] return []
}) })
return Promise.all(items.map(async item => await this.getWork(item))) return Promise.all(items.map(async item => await this.getWork(item))).then(resArray => {
return resArray.filter(res => res != null)
})
} }
async getWork(item) { async getWork(item) {
var { work_id, work_type_id } = item const { work_id, work_type_id } = item
if (this._filterWorkType.includes(work_type_id)) { if (this._filterWorkType.includes(work_type_id)) {
return { title: null } return { title: null }
} }
var url = `${this._baseUrl}/work/${work_id}/extended` const url = `${this._baseUrl}/work/${work_id}/extended`
var 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 reques error`, error)
return null return null
}) })
return await this.cleanBookData(bookData) return this.cleanBookData(bookData)
} }
async cleanBookData(bookData) { async cleanBookData(bookData) {
var { 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
var subtitle = Array.isArray(work_name_alts) ? work_name_alts[0] : null const subtitle = Array.isArray(work_name_alts) ? work_name_alts[0] : null
var auth = authors.map(function (author) { const auth = authors.map(function (author) {
return author.name return author.name
}); })
var genres = classificatory ? this.tryGetGenres(classificatory) : [] const genres = classificatory ? this.tryGetGenres(classificatory) : []
var imageAndIsbn = await this.tryGetCoverFromEditions(editions_blocks) const imageAndIsbn = await this.tryGetCoverFromEditions(editions_blocks)
if (imageAndIsbn) { if (imageAndIsbn) {
var { imageUrl, isbn } = imageAndIsbn var { imageUrl, isbn } = imageAndIsbn
@ -79,7 +81,7 @@ class FantLab {
} }
} }
var cover = 'https://fantlab.ru' + image const cover = 'https://fantlab.ru' + image
return { return {
id: work_id, id: work_id,
@ -96,11 +98,11 @@ class FantLab {
} }
tryGetGenres(classificatory) { tryGetGenres(classificatory) {
var { genre_group } = classificatory; const { genre_group } = classificatory
if (!genre_group) { if (!genre_group) {
return [] return []
} }
var genresGroup = 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,16 +113,16 @@ class FantLab {
if (!genresGroup) return [] if (!genresGroup) return []
var { genre } = genresGroup; const { genre } = genresGroup
var rootGenre = genre[0]; const rootGenre = genre[0]
var { label } = rootGenre const { label } = rootGenre
return [label].concat(this.tryGetSubGenres(rootGenre)) return [label].concat(this.tryGetSubGenres(rootGenre))
} }
tryGetSubGenres(rootGenre) { tryGetSubGenres(rootGenre) {
var { genre } = rootGenre const { genre } = rootGenre
return genre ? genre.map(genreObj => genreObj.label) : [] return genre ? genre.map(genreObj => genreObj.label) : []
} }
@ -130,7 +132,7 @@ class FantLab {
return null return null
} }
var bookEditions = editions['30'] // try get audiobooks first let bookEditions = editions['30'] // try get audiobooks first
if (!bookEditions) { if (!bookEditions) {
bookEditions = editions['10'] // paper editions in ru lang bookEditions = editions['10'] // paper editions in ru lang
} }
@ -139,12 +141,12 @@ class FantLab {
return null return null
} }
var { list } = bookEditions const { list } = bookEditions
var lastEdition = list[list.length - 1] const lastEdition = list[list.length - 1]
var editionId = lastEdition['edition_id'] const editionId = lastEdition['edition_id']
var isbn = lastEdition['isbn'] // get only from paper edition const isbn = lastEdition['isbn'] // get only from paper edition
return { return {
imageUrl: await this.getCoverFromEdition(editionId), imageUrl: await this.getCoverFromEdition(editionId),
@ -153,9 +155,9 @@ class FantLab {
} }
async getCoverFromEdition(editionId) { async getCoverFromEdition(editionId) {
var url = `${this._baseUrl}/edition/${editionId}` const url = `${this._baseUrl}/edition/${editionId}`
var 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 error', error)