2024-01-03 01:36:56 +01:00
|
|
|
const Database = require('../Database')
|
2024-02-11 23:48:16 +01:00
|
|
|
const axios = require('axios')
|
|
|
|
const Logger = require('../Logger')
|
2024-01-03 01:36:56 +01:00
|
|
|
|
|
|
|
class CustomProviderAdapter {
|
2024-02-11 23:48:16 +01:00
|
|
|
constructor() { }
|
2024-01-03 01:36:56 +01:00
|
|
|
|
2024-02-11 23:48:16 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {string} title
|
|
|
|
* @param {string} author
|
|
|
|
* @param {string} providerSlug
|
|
|
|
* @returns {Promise<Object[]>}
|
|
|
|
*/
|
2024-01-03 01:36:56 +01:00
|
|
|
async search(title, author, providerSlug) {
|
2024-02-11 23:48:16 +01:00
|
|
|
const providerId = providerSlug.split('custom-')[1]
|
2024-01-12 21:45:03 +01:00
|
|
|
const provider = await Database.customMetadataProviderModel.findByPk(providerId)
|
2024-01-03 01:36:56 +01:00
|
|
|
|
|
|
|
if (!provider) {
|
2024-01-12 21:45:03 +01:00
|
|
|
throw new Error("Custom provider not found for the given id")
|
2024-01-03 01:36:56 +01:00
|
|
|
}
|
|
|
|
|
2024-02-11 23:48:16 +01:00
|
|
|
const axiosOptions = {}
|
|
|
|
if (provider.authHeaderValue) {
|
|
|
|
axiosOptions.headers = {
|
|
|
|
'Authorization': provider.authHeaderValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const matches = await axios.get(`${provider.url}/search?query=${encodeURIComponent(title)}${!!author ? `&author=${encodeURIComponent(author)}` : ""}`, axiosOptions).then((res) => {
|
|
|
|
if (!res?.data || !Array.isArray(res.data.matches)) return null
|
2024-01-03 01:36:56 +01:00
|
|
|
return res.data.matches
|
|
|
|
}).catch(error => {
|
|
|
|
Logger.error('[CustomMetadataProvider] Search error', error)
|
|
|
|
return []
|
|
|
|
})
|
|
|
|
|
2024-02-11 23:48:16 +01:00
|
|
|
if (!matches) {
|
2024-01-12 21:45:03 +01:00
|
|
|
throw new Error("Custom provider returned malformed response")
|
2024-01-03 01:36:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// re-map keys to throw out
|
|
|
|
return matches.map(({
|
|
|
|
title,
|
|
|
|
subtitle,
|
|
|
|
author,
|
|
|
|
narrator,
|
|
|
|
publisher,
|
2024-01-12 21:45:03 +01:00
|
|
|
publishedYear,
|
2024-01-03 01:36:56 +01:00
|
|
|
description,
|
|
|
|
cover,
|
|
|
|
isbn,
|
|
|
|
asin,
|
|
|
|
genres,
|
|
|
|
tags,
|
2024-01-12 21:45:03 +01:00
|
|
|
series,
|
2024-01-03 01:36:56 +01:00
|
|
|
language,
|
2024-02-11 23:48:16 +01:00
|
|
|
duration
|
2024-01-03 01:36:56 +01:00
|
|
|
}) => {
|
|
|
|
return {
|
|
|
|
title,
|
|
|
|
subtitle,
|
|
|
|
author,
|
|
|
|
narrator,
|
|
|
|
publisher,
|
2024-01-12 21:45:03 +01:00
|
|
|
publishedYear,
|
2024-01-03 01:36:56 +01:00
|
|
|
description,
|
|
|
|
cover,
|
|
|
|
isbn,
|
|
|
|
asin,
|
|
|
|
genres,
|
2024-02-11 23:48:16 +01:00
|
|
|
tags: tags?.join(',') || null,
|
|
|
|
series: series?.length ? series : null,
|
2024-01-03 01:36:56 +01:00
|
|
|
language,
|
2024-02-11 23:48:16 +01:00
|
|
|
duration
|
2024-01-03 01:36:56 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = CustomProviderAdapter
|