2022-03-06 23:32:04 +01:00
|
|
|
const AuthorFinder = require('../finders/AuthorFinder')
|
2021-11-26 22:46:07 +01:00
|
|
|
|
|
|
|
class AuthorScanner {
|
2022-02-27 20:47:52 +01:00
|
|
|
constructor(db) {
|
2021-11-26 22:46:07 +01:00
|
|
|
this.db = db
|
2022-02-27 20:47:52 +01:00
|
|
|
this.authorFinder = new AuthorFinder()
|
2021-11-26 22:46:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getUniqueAuthors() {
|
|
|
|
var authorFls = this.db.audiobooks.map(b => b.book.authorFL)
|
|
|
|
var authors = []
|
|
|
|
authorFls.forEach((auth) => {
|
|
|
|
authors = authors.concat(auth.split(', ').map(a => a.trim()))
|
|
|
|
})
|
|
|
|
return [...new Set(authors)]
|
|
|
|
}
|
|
|
|
|
|
|
|
async scanAuthors() {
|
|
|
|
var authors = this.getUniqueAuthors()
|
|
|
|
for (let i = 0; i < authors.length; i++) {
|
|
|
|
var authorName = authors[i]
|
|
|
|
var author = await this.authorFinder.getAuthorByName(authorName)
|
|
|
|
if (!author) {
|
|
|
|
return res.status(500).send('Failed to create author')
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.db.insertEntity('author', author)
|
|
|
|
this.emitter('author_added', author.toJSON())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = AuthorScanner
|