Merge pull request #1680 from lukeIam/region_authors

Use region for author queries
This commit is contained in:
advplyr 2023-04-16 15:54:49 -05:00 committed by GitHub
commit eb5f257b8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 59 additions and 23 deletions

View File

@ -189,6 +189,9 @@ export default {
currentLibraryId() { currentLibraryId() {
return this.$store.state.libraries.currentLibraryId return this.$store.state.libraries.currentLibraryId
}, },
libraryProvider() {
return this.$store.getters['libraries/getLibraryProvider'](this.currentLibraryId) || 'google'
},
currentLibraryMediaType() { currentLibraryMediaType() {
return this.$store.getters['libraries/getCurrentLibraryMediaType'] return this.$store.getters['libraries/getCurrentLibraryMediaType']
}, },
@ -323,7 +326,11 @@ export default {
const payload = {} const payload = {}
if (author.asin) payload.asin = author.asin if (author.asin) payload.asin = author.asin
else payload.q = author.name else payload.q = author.name
console.log('Payload', payload, 'author', author)
payload.region = 'us'
if (this.libraryProvider.startsWith('audible.')) {
payload.region = this.libraryProvider.split('.').pop() || 'us'
}
this.$eventBus.$emit(`searching-author-${author.id}`, true) this.$eventBus.$emit(`searching-author-${author.id}`, true)

View File

@ -77,6 +77,12 @@ export default {
}, },
userCanUpdate() { userCanUpdate() {
return this.$store.getters['user/getUserCanUpdate'] return this.$store.getters['user/getUserCanUpdate']
},
currentLibraryId() {
return this.$store.state.libraries.currentLibraryId
},
libraryProvider() {
return this.$store.getters['libraries/getLibraryProvider'](this.currentLibraryId) || 'google'
} }
}, },
methods: { methods: {
@ -92,6 +98,11 @@ export default {
if (this.asin) payload.asin = this.asin if (this.asin) payload.asin = this.asin
else payload.q = this.name else payload.q = this.name
payload.region = 'us'
if (this.libraryProvider.startsWith('audible.')) {
payload.region = this.libraryProvider.split('.').pop() || 'us'
}
var response = await this.$axios.$post(`/api/authors/${this.authorId}/match`, payload).catch((error) => { var response = await this.$axios.$post(`/api/authors/${this.authorId}/match`, payload).catch((error) => {
console.error('Failed', error) console.error('Failed', error)
return null return null

View File

@ -85,6 +85,12 @@ export default {
}, },
title() { title() {
return this.$strings.HeaderUpdateAuthor return this.$strings.HeaderUpdateAuthor
},
currentLibraryId() {
return this.$store.state.libraries.currentLibraryId
},
libraryProvider() {
return this.$store.getters['libraries/getLibraryProvider'](this.currentLibraryId) || 'google'
} }
}, },
methods: { methods: {
@ -151,6 +157,11 @@ export default {
if (this.authorCopy.asin) payload.asin = this.authorCopy.asin if (this.authorCopy.asin) payload.asin = this.authorCopy.asin
else payload.q = this.authorCopy.name else payload.q = this.authorCopy.name
payload.region = 'us'
if (this.libraryProvider.startsWith('audible.')) {
payload.region = this.libraryProvider.split('.').pop() || 'us'
}
var response = await this.$axios.$post(`/api/authors/${this.authorId}/match`, payload).catch((error) => { var response = await this.$axios.$post(`/api/authors/${this.authorId}/match`, payload).catch((error) => {
console.error('Failed', error) console.error('Failed', error)
return null return null

View File

@ -167,18 +167,19 @@ class AuthorController {
} }
async match(req, res) { async match(req, res) {
var authorData = null let authorData = null
const region = req.body.region || 'us'
if (req.body.asin) { if (req.body.asin) {
authorData = await this.authorFinder.findAuthorByASIN(req.body.asin) authorData = await this.authorFinder.findAuthorByASIN(req.body.asin, region)
} else { } else {
authorData = await this.authorFinder.findAuthorByName(req.body.q) authorData = await this.authorFinder.findAuthorByName(req.body.q, region)
} }
if (!authorData) { if (!authorData) {
return res.status(404).send('Author not found') return res.status(404).send('Author not found')
} }
Logger.debug(`[AuthorController] match author with "${req.body.q || req.body.asin}"`, authorData) Logger.debug(`[AuthorController] match author with "${req.body.q || req.body.asin}"`, authorData)
var hasUpdates = false let hasUpdates = false
if (authorData.asin && req.author.asin !== authorData.asin) { if (authorData.asin && req.author.asin !== authorData.asin) {
req.author.asin = authorData.asin req.author.asin = authorData.asin
hasUpdates = true hasUpdates = true
@ -188,7 +189,7 @@ class AuthorController {
if (authorData.image && (!req.author.imagePath || hasUpdates)) { if (authorData.image && (!req.author.imagePath || hasUpdates)) {
this.cacheManager.purgeImageCache(req.author.id) this.cacheManager.purgeImageCache(req.author.id)
var imageData = await this.authorFinder.saveAuthorImage(req.author.id, authorData.image) const imageData = await this.authorFinder.saveAuthorImage(req.author.id, authorData.image)
if (imageData) { if (imageData) {
req.author.imagePath = imageData.path req.author.imagePath = imageData.path
hasUpdates = true hasUpdates = true
@ -204,7 +205,7 @@ class AuthorController {
req.author.updatedAt = Date.now() req.author.updatedAt = Date.now()
await this.db.updateEntity('author', req.author) await this.db.updateEntity('author', req.author)
var numBooks = this.db.libraryItems.filter(li => { const numBooks = this.db.libraryItems.filter(li => {
return li.media.metadata.hasAuthor && li.media.metadata.hasAuthor(req.author.id) return li.media.metadata.hasAuthor && li.media.metadata.hasAuthor(req.author.id)
}).length }).length
SocketAuthority.emitter('author_updated', req.author.toJSONExpanded(numBooks)) SocketAuthority.emitter('author_updated', req.author.toJSONExpanded(numBooks))

View File

@ -20,16 +20,16 @@ class AuthorFinder {
}) })
} }
findAuthorByASIN(asin) { findAuthorByASIN(asin, region) {
if (!asin) return null if (!asin) return null
return this.audnexus.findAuthorByASIN(asin) return this.audnexus.findAuthorByASIN(asin, region)
} }
async findAuthorByName(name, options = {}) { async findAuthorByName(name, region, options = {}) {
if (!name) return null if (!name) return null
const maxLevenshtein = !isNaN(options.maxLevenshtein) ? Number(options.maxLevenshtein) : 3 const maxLevenshtein = !isNaN(options.maxLevenshtein) ? Number(options.maxLevenshtein) : 3
var author = await this.audnexus.findAuthorByName(name, maxLevenshtein) const author = await this.audnexus.findAuthorByName(name, region, maxLevenshtein)
if (!author || !author.name) { if (!author || !author.name) {
return null return null
} }

View File

@ -7,9 +7,12 @@ class Audnexus {
this.baseUrl = 'https://api.audnex.us' this.baseUrl = 'https://api.audnex.us'
} }
authorASINsRequest(name) { authorASINsRequest(name, region) {
name = encodeURIComponent(name); name = encodeURIComponent(name)
return axios.get(`${this.baseUrl}/authors?name=${name}`).then((res) => { const regionQuery = region ? `&region=${region}` : ''
const authorRequestUrl = `${this.baseUrl}/authors?name=${name}${regionQuery}`
Logger.info(`[Audnexus] Searching for author "${authorRequestUrl}"`)
return axios.get(authorRequestUrl).then((res) => {
return res.data || [] return res.data || []
}).catch((error) => { }).catch((error) => {
Logger.error(`[Audnexus] Author ASIN request failed for ${name}`, error) Logger.error(`[Audnexus] Author ASIN request failed for ${name}`, error)
@ -17,9 +20,12 @@ class Audnexus {
}) })
} }
authorRequest(asin) { authorRequest(asin, region) {
asin = encodeURIComponent(asin); asin = encodeURIComponent(asin)
return axios.get(`${this.baseUrl}/authors/${asin}`).then((res) => { const regionQuery = region ? `?region=${region}` : ''
const authorRequestUrl = `${this.baseUrl}/authors/${asin}${regionQuery}`
Logger.info(`[Audnexus] Searching for author "${authorRequestUrl}"`)
return axios.get(authorRequestUrl).then((res) => {
return res.data return res.data
}).catch((error) => { }).catch((error) => {
Logger.error(`[Audnexus] Author request failed for ${asin}`, error) Logger.error(`[Audnexus] Author request failed for ${asin}`, error)
@ -27,8 +33,8 @@ class Audnexus {
}) })
} }
async findAuthorByASIN(asin) { async findAuthorByASIN(asin, region) {
var author = await this.authorRequest(asin) const author = await this.authorRequest(asin, region)
if (!author) { if (!author) {
return null return null
} }
@ -40,14 +46,14 @@ class Audnexus {
} }
} }
async findAuthorByName(name, maxLevenshtein = 3) { async findAuthorByName(name, region, maxLevenshtein = 3) {
Logger.debug(`[Audnexus] Looking up author by name ${name}`) Logger.debug(`[Audnexus] Looking up author by name ${name}`)
var asins = await this.authorASINsRequest(name) const asins = await this.authorASINsRequest(name, region)
var matchingAsin = asins.find(obj => levenshteinDistance(obj.name, name) <= maxLevenshtein) const matchingAsin = asins.find(obj => levenshteinDistance(obj.name, name) <= maxLevenshtein)
if (!matchingAsin) { if (!matchingAsin) {
return null return null
} }
var author = await this.authorRequest(matchingAsin.asin) const author = await this.authorRequest(matchingAsin.asin)
if (!author) { if (!author) {
return null return null
} }