2021-11-18 02:19:24 +01:00
|
|
|
const fs = require('fs-extra')
|
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() {
|
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async findAuthorByName(name, options = {}) {
|
|
|
|
if (!name) return null
|
|
|
|
const maxLevenshtein = !isNaN(options.maxLevenshtein) ? Number(options.maxLevenshtein) : 2
|
|
|
|
|
|
|
|
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')
|
|
|
|
await fs.ensureDir(authorDir)
|
|
|
|
|
|
|
|
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
|