mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-07-30 15:55:26 +02:00
Added support for custom metadata providers
WiP but already open to feedback
This commit is contained in:
parent
8c6a2ac5dd
commit
8027c4a06f
14 changed files with 642 additions and 4 deletions
76
server/providers/CustomProviderAdapter.js
Normal file
76
server/providers/CustomProviderAdapter.js
Normal file
|
@ -0,0 +1,76 @@
|
|||
const Database = require('../Database')
|
||||
const axios = require("axios");
|
||||
const Logger = require("../Logger");
|
||||
|
||||
class CustomProviderAdapter {
|
||||
constructor() {
|
||||
}
|
||||
|
||||
async search(title, author, providerSlug) {
|
||||
const providerId = providerSlug.split("custom-")[1]
|
||||
|
||||
console.log(providerId)
|
||||
const provider = await Database.customMetadataProviderModel.findOne({
|
||||
where: {
|
||||
id: providerId,
|
||||
}
|
||||
});
|
||||
|
||||
if (!provider) {
|
||||
throw new Error("Custom provider not found for the given id");
|
||||
}
|
||||
|
||||
const matches = await axios.get(`${provider.url}/search?query=${encodeURIComponent(title)}${!!author ? `&author=${encodeURIComponent(author)}` : ""}`, {
|
||||
headers: {
|
||||
"Authorization": provider.apiKey,
|
||||
},
|
||||
}).then((res) => {
|
||||
if (!res || !res.data || !Array.isArray(res.data.matches)) return null
|
||||
return res.data.matches
|
||||
}).catch(error => {
|
||||
Logger.error('[CustomMetadataProvider] Search error', error)
|
||||
return []
|
||||
})
|
||||
|
||||
if (matches === null) {
|
||||
throw new Error("Custom provider returned malformed response");
|
||||
}
|
||||
|
||||
// re-map keys to throw out
|
||||
return matches.map(({
|
||||
title,
|
||||
subtitle,
|
||||
author,
|
||||
narrator,
|
||||
publisher,
|
||||
published_year,
|
||||
description,
|
||||
cover,
|
||||
isbn,
|
||||
asin,
|
||||
genres,
|
||||
tags,
|
||||
language,
|
||||
duration,
|
||||
}) => {
|
||||
return {
|
||||
title,
|
||||
subtitle,
|
||||
author,
|
||||
narrator,
|
||||
publisher,
|
||||
publishedYear: published_year,
|
||||
description,
|
||||
cover,
|
||||
isbn,
|
||||
asin,
|
||||
genres,
|
||||
tags: tags.join(","),
|
||||
language,
|
||||
duration,
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = CustomProviderAdapter
|
Loading…
Add table
Add a link
Reference in a new issue