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
|
2024-03-12 09:18:52 +01:00
|
|
|
* @param {string} isbn
|
2024-02-11 23:48:16 +01:00
|
|
|
* @param {string} providerSlug
|
2024-02-13 00:12:49 +01:00
|
|
|
* @param {string} mediaType
|
2024-02-11 23:48:16 +01:00
|
|
|
* @returns {Promise<Object[]>}
|
|
|
|
*/
|
2024-03-12 09:18:52 +01:00
|
|
|
async search(title, author, isbn, providerSlug, mediaType) {
|
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-13 00:12:49 +01:00
|
|
|
// Setup query params
|
|
|
|
const queryObj = {
|
|
|
|
mediaType,
|
|
|
|
query: title
|
|
|
|
}
|
|
|
|
if (author) {
|
|
|
|
queryObj.author = author
|
|
|
|
}
|
2024-03-12 09:18:52 +01:00
|
|
|
if (isbn) {
|
|
|
|
queryObj.isbn = isbn
|
|
|
|
}
|
2024-02-13 00:12:49 +01:00
|
|
|
const queryString = (new URLSearchParams(queryObj)).toString()
|
|
|
|
|
|
|
|
// Setup headers
|
2024-02-11 23:48:16 +01:00
|
|
|
const axiosOptions = {}
|
|
|
|
if (provider.authHeaderValue) {
|
|
|
|
axiosOptions.headers = {
|
|
|
|
'Authorization': provider.authHeaderValue
|
|
|
|
}
|
|
|
|
}
|
2024-02-13 00:12:49 +01:00
|
|
|
|
|
|
|
const matches = await axios.get(`${provider.url}/search?${queryString}}`, axiosOptions).then((res) => {
|
2024-02-11 23:48:16 +01:00
|
|
|
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
|