2022-07-06 02:53:01 +02:00
|
|
|
const fs = require('../libs/fsExtra')
|
2022-03-06 23:32:04 +01:00
|
|
|
const Logger = require('../Logger')
|
2021-11-18 02:19:24 +01:00
|
|
|
const Path = require('path')
|
2022-03-06 23:32:04 +01:00
|
|
|
const Audnexus = require('../providers/Audnexus')
|
2021-11-18 02:19:24 +01:00
|
|
|
|
2022-03-06 23:32:04 +01:00
|
|
|
const { downloadFile } = require('../utils/fileUtils')
|
2021-11-18 02:19:24 +01:00
|
|
|
|
2021-11-26 22:46:07 +01:00
|
|
|
class AuthorFinder {
|
2022-02-27 20:47:52 +01:00
|
|
|
constructor() {
|
2021-11-18 02:19:24 +01:00
|
|
|
this.audnexus = new Audnexus()
|
|
|
|
}
|
|
|
|
|
2023-04-16 22:53:46 +02:00
|
|
|
findAuthorByASIN(asin, region) {
|
2022-05-14 01:11:54 +02:00
|
|
|
if (!asin) return null
|
2023-04-16 22:53:46 +02:00
|
|
|
return this.audnexus.findAuthorByASIN(asin, region)
|
2022-05-14 01:11:54 +02:00
|
|
|
}
|
|
|
|
|
2023-04-16 22:53:46 +02:00
|
|
|
async findAuthorByName(name, region, options = {}) {
|
2021-11-18 02:19:24 +01:00
|
|
|
if (!name) return null
|
2022-05-08 03:01:29 +02:00
|
|
|
const maxLevenshtein = !isNaN(options.maxLevenshtein) ? Number(options.maxLevenshtein) : 3
|
2021-11-18 02:19:24 +01:00
|
|
|
|
2023-04-16 22:53:46 +02:00
|
|
|
const author = await this.audnexus.findAuthorByName(name, region, maxLevenshtein)
|
2021-11-18 02:19:24 +01:00
|
|
|
if (!author || !author.name) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
return author
|
|
|
|
}
|
|
|
|
|
2023-10-14 00:37:37 +02:00
|
|
|
/**
|
|
|
|
* Download author image from url and save in authors folder
|
|
|
|
*
|
|
|
|
* @param {string} authorId
|
|
|
|
* @param {string} url
|
|
|
|
* @returns {Promise<{path:string, error:string}>}
|
|
|
|
*/
|
2022-03-13 12:42:43 +01:00
|
|
|
async saveAuthorImage(authorId, url) {
|
2023-10-14 00:37:37 +02:00
|
|
|
const authorDir = Path.join(global.MetadataPath, 'authors')
|
2022-06-18 19:05:30 +02:00
|
|
|
|
|
|
|
if (!await fs.pathExists(authorDir)) {
|
|
|
|
await fs.ensureDir(authorDir)
|
|
|
|
}
|
2022-03-13 12:42:43 +01:00
|
|
|
|
2023-10-14 00:37:37 +02:00
|
|
|
const imageExtension = url.toLowerCase().split('.').pop()
|
|
|
|
const ext = imageExtension === 'png' ? 'png' : 'jpg'
|
|
|
|
const filename = authorId + '.' + ext
|
|
|
|
const outputPath = Path.posix.join(authorDir, filename)
|
|
|
|
|
|
|
|
return downloadFile(url, outputPath).then(() => {
|
|
|
|
return {
|
|
|
|
path: outputPath
|
|
|
|
}
|
|
|
|
}).catch((err) => {
|
|
|
|
let errorMsg = err.message || 'Unknown error'
|
|
|
|
Logger.error(`[AuthorFinder] Download image file failed for "${url}"`, errorMsg)
|
|
|
|
return {
|
|
|
|
error: errorMsg
|
|
|
|
}
|
|
|
|
})
|
2022-03-13 12:42:43 +01:00
|
|
|
}
|
2021-11-18 02:19:24 +01:00
|
|
|
}
|
2023-09-04 23:33:55 +02:00
|
|
|
module.exports = new AuthorFinder()
|