2023-02-06 22:25:18 +01:00
|
|
|
const axios = require('axios')
|
|
|
|
const Logger = require('../Logger')
|
|
|
|
|
|
|
|
class FantLab {
|
|
|
|
// 7 - other
|
|
|
|
// 11 - essay
|
|
|
|
// 12 - article
|
|
|
|
// 22 - disser
|
|
|
|
// 23 - monography
|
|
|
|
// 24 - study
|
|
|
|
// 25 - encyclopedy
|
|
|
|
// 26 - magazine
|
|
|
|
// 46 - sketch
|
|
|
|
// 47 - reportage
|
|
|
|
// 49 - excerpt
|
|
|
|
// 51 - interview
|
|
|
|
// 52 - review
|
|
|
|
// 55 - libretto
|
|
|
|
// 56 - anthology series
|
|
|
|
// 57 - newspaper
|
|
|
|
// types can get here https://api.fantlab.ru/config.json
|
2023-02-08 20:32:27 +01:00
|
|
|
_filterWorkType = [7, 11, 12, 22, 23, 24, 25, 26, 46, 47, 49, 51, 52, 55, 56, 57]
|
2023-02-06 22:25:18 +01:00
|
|
|
_baseUrl = 'https://api.fantlab.ru'
|
|
|
|
|
|
|
|
constructor() { }
|
|
|
|
|
|
|
|
async search(title, author) {
|
2023-02-08 20:32:27 +01:00
|
|
|
let searchString = encodeURIComponent(title)
|
2023-02-06 22:25:18 +01:00
|
|
|
if (author) {
|
|
|
|
searchString += encodeURIComponent(' ' + author)
|
|
|
|
}
|
2023-02-08 20:32:27 +01:00
|
|
|
const url = `${this._baseUrl}/search-works?q=${searchString}&page=1&onlymatches=1`
|
2023-02-06 22:25:18 +01:00
|
|
|
Logger.debug(`[FantLab] Search url: ${url}`)
|
2023-02-08 20:32:27 +01:00
|
|
|
const items = await axios.get(url).then((res) => {
|
2023-02-06 22:25:18 +01:00
|
|
|
return res.data || []
|
|
|
|
}).catch(error => {
|
|
|
|
Logger.error('[FantLab] search error', error)
|
|
|
|
return []
|
|
|
|
})
|
|
|
|
|
2023-02-08 20:32:27 +01:00
|
|
|
return Promise.all(items.map(async item => await this.getWork(item))).then(resArray => {
|
|
|
|
return resArray.filter(res => res != null)
|
|
|
|
})
|
2023-02-06 22:25:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async getWork(item) {
|
2023-02-08 20:32:27 +01:00
|
|
|
const { work_id, work_type_id } = item
|
2023-02-06 22:25:18 +01:00
|
|
|
|
|
|
|
if (this._filterWorkType.includes(work_type_id)) {
|
|
|
|
return { title: null }
|
|
|
|
}
|
|
|
|
|
2023-02-08 20:32:27 +01:00
|
|
|
const url = `${this._baseUrl}/work/${work_id}/extended`
|
|
|
|
const bookData = await axios.get(url).then((resp) => {
|
2023-02-06 22:25:18 +01:00
|
|
|
return resp.data || null
|
|
|
|
}).catch((error) => {
|
|
|
|
Logger.error(`[FantLab] work info reques error`, error)
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
|
2023-02-08 20:32:27 +01:00
|
|
|
return this.cleanBookData(bookData)
|
2023-02-06 22:25:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async cleanBookData(bookData) {
|
2023-02-08 20:32:27 +01:00
|
|
|
let { authors, work_name_alts, work_id, work_name, work_year, work_description, image, classificatory, editions_blocks } = bookData
|
2023-02-06 22:25:18 +01:00
|
|
|
|
2023-02-08 20:32:27 +01:00
|
|
|
const subtitle = Array.isArray(work_name_alts) ? work_name_alts[0] : null
|
|
|
|
const auth = authors.map(function (author) {
|
2023-02-06 22:25:18 +01:00
|
|
|
return author.name
|
2023-02-08 20:32:27 +01:00
|
|
|
})
|
2023-02-06 22:25:18 +01:00
|
|
|
|
2023-02-08 20:32:27 +01:00
|
|
|
const genres = classificatory ? this.tryGetGenres(classificatory) : []
|
2023-02-06 22:25:18 +01:00
|
|
|
|
2023-02-08 20:32:27 +01:00
|
|
|
const imageAndIsbn = await this.tryGetCoverFromEditions(editions_blocks)
|
2023-02-06 22:25:18 +01:00
|
|
|
|
|
|
|
if (imageAndIsbn) {
|
|
|
|
var { imageUrl, isbn } = imageAndIsbn
|
|
|
|
|
|
|
|
if (imageUrl) {
|
|
|
|
image = imageUrl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-08 20:32:27 +01:00
|
|
|
const cover = 'https://fantlab.ru' + image
|
2023-02-06 22:25:18 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
id: work_id,
|
|
|
|
title: work_name,
|
|
|
|
subtitle: subtitle || null,
|
|
|
|
author: auth ? auth.join(', ') : null,
|
|
|
|
publisher: null,
|
|
|
|
publishedYear: work_year,
|
|
|
|
description: work_description,
|
|
|
|
cover: image ? cover : null,
|
|
|
|
genres: genres,
|
|
|
|
isbn: isbn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tryGetGenres(classificatory) {
|
2023-02-08 20:32:27 +01:00
|
|
|
const { genre_group } = classificatory
|
2023-02-06 22:25:18 +01:00
|
|
|
if (!genre_group) {
|
|
|
|
return []
|
|
|
|
}
|
2023-02-08 20:32:27 +01:00
|
|
|
const genresGroup = genre_group.find(group => group.genre_group_id == 1) // genres and subgenres
|
2023-02-06 22:25:18 +01:00
|
|
|
|
|
|
|
// genre_group_id=2 - General Characteristics
|
|
|
|
// genre_group_id=3 - Arena
|
|
|
|
// genre_group_id=4 - Duration of action
|
|
|
|
// genre_group_id=6 - Story moves
|
|
|
|
// genre_group_id=7 - Story linearity
|
|
|
|
// genre_group_id=5 - Recommended age of the reader
|
|
|
|
|
|
|
|
if (!genresGroup) return []
|
|
|
|
|
2023-02-08 20:32:27 +01:00
|
|
|
const { genre } = genresGroup
|
|
|
|
const rootGenre = genre[0]
|
2023-02-06 22:25:18 +01:00
|
|
|
|
2023-02-08 20:32:27 +01:00
|
|
|
const { label } = rootGenre
|
2023-02-06 22:25:18 +01:00
|
|
|
|
|
|
|
return [label].concat(this.tryGetSubGenres(rootGenre))
|
|
|
|
}
|
|
|
|
|
|
|
|
tryGetSubGenres(rootGenre) {
|
2023-02-08 20:32:27 +01:00
|
|
|
const { genre } = rootGenre
|
2023-02-06 22:25:18 +01:00
|
|
|
return genre ? genre.map(genreObj => genreObj.label) : []
|
|
|
|
}
|
|
|
|
|
|
|
|
async tryGetCoverFromEditions(editions) {
|
|
|
|
|
|
|
|
if (!editions) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2023-02-08 20:32:27 +01:00
|
|
|
let bookEditions = editions['30'] // try get audiobooks first
|
2023-02-06 22:25:18 +01:00
|
|
|
if (!bookEditions) {
|
|
|
|
bookEditions = editions['10'] // paper editions in ru lang
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bookEditions) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2023-02-08 20:32:27 +01:00
|
|
|
const { list } = bookEditions
|
2023-02-06 22:25:18 +01:00
|
|
|
|
2023-02-08 20:32:27 +01:00
|
|
|
const lastEdition = list[list.length - 1]
|
2023-02-06 22:25:18 +01:00
|
|
|
|
2023-02-08 20:32:27 +01:00
|
|
|
const editionId = lastEdition['edition_id']
|
|
|
|
const isbn = lastEdition['isbn'] // get only from paper edition
|
2023-02-06 22:25:18 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
imageUrl: await this.getCoverFromEdition(editionId),
|
|
|
|
isbn: isbn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async getCoverFromEdition(editionId) {
|
2023-02-08 20:32:27 +01:00
|
|
|
const url = `${this._baseUrl}/edition/${editionId}`
|
2023-02-06 22:25:18 +01:00
|
|
|
|
2023-02-08 20:32:27 +01:00
|
|
|
const editionInfo = await axios.get(url).then((resp) => {
|
2023-02-06 22:25:18 +01:00
|
|
|
return resp.data || null
|
|
|
|
}).catch(error => {
|
|
|
|
Logger.error('[FantLab] search cover from edition error', error)
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
|
|
|
|
return editionInfo ? editionInfo['image'] : null
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = FantLab
|