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')
|
2022-06-18 19:05:30 +02:00
|
|
|
const filePerms = require('../utils/filePerms')
|
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() {
|
|
|
|
this.AuthorPath = Path.join(global.MetadataPath, 'authors')
|
2021-11-18 02:19:24 +01:00
|
|
|
|
|
|
|
this.audnexus = new Audnexus()
|
|
|
|
}
|
|
|
|
|
|
|
|
async downloadImage(url, outputPath) {
|
|
|
|
return downloadFile(url, outputPath).then(() => true).catch((error) => {
|
2021-11-26 22:46:07 +01:00
|
|
|
Logger.error('[AuthorFinder] Failed to download author image', error)
|
2021-11-18 02:19:24 +01:00
|
|
|
return null
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-14 01:11:54 +02:00
|
|
|
findAuthorByASIN(asin) {
|
|
|
|
if (!asin) return null
|
|
|
|
return this.audnexus.findAuthorByASIN(asin)
|
|
|
|
}
|
|
|
|
|
2021-11-18 02:19:24 +01:00
|
|
|
async findAuthorByName(name, options = {}) {
|
|
|
|
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
|
|
|
|
|
|
|
var author = await this.audnexus.findAuthorByName(name, maxLevenshtein)
|
|
|
|
if (!author || !author.name) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
return author
|
|
|
|
}
|
|
|
|
|
2022-03-13 12:42:43 +01:00
|
|
|
async saveAuthorImage(authorId, url) {
|
|
|
|
var authorDir = this.AuthorPath
|
|
|
|
var relAuthorDir = Path.posix.join('/metadata', 'authors')
|
2022-06-18 19:05:30 +02:00
|
|
|
|
|
|
|
if (!await fs.pathExists(authorDir)) {
|
|
|
|
await fs.ensureDir(authorDir)
|
|
|
|
await filePerms.setDefault(authorDir)
|
|
|
|
}
|
2022-03-13 12:42:43 +01:00
|
|
|
|
|
|
|
var imageExtension = url.toLowerCase().split('.').pop()
|
|
|
|
var ext = imageExtension === 'png' ? 'png' : 'jpg'
|
|
|
|
var filename = authorId + '.' + ext
|
|
|
|
var outputPath = Path.posix.join(authorDir, filename)
|
|
|
|
var relPath = Path.posix.join(relAuthorDir, filename)
|
|
|
|
|
|
|
|
var success = await this.downloadImage(url, outputPath)
|
|
|
|
if (!success) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
path: outputPath,
|
|
|
|
relPath
|
|
|
|
}
|
|
|
|
}
|
2021-11-18 02:19:24 +01:00
|
|
|
}
|
2021-11-26 22:46:07 +01:00
|
|
|
module.exports = AuthorFinder
|