-
Notifications
You must be signed in to change notification settings - Fork 574
Added AudiMeta - an extended Audible provider #4066
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Vito0912
wants to merge
4
commits into
advplyr:master
Choose a base branch
from
Vito0912:feat/audimeta
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
const axios = require('axios').default | ||
const Logger = require('../Logger') | ||
const { isValidASIN } = require('../utils/index') | ||
|
||
class AudiMeta { | ||
#responseTimeout = 30000 | ||
|
||
constructor() { | ||
this.regionMap = { | ||
us: '.com', | ||
ca: '.ca', | ||
uk: '.co.uk', | ||
au: '.com.au', | ||
fr: '.fr', | ||
de: '.de', | ||
jp: '.co.jp', | ||
it: '.it', | ||
in: '.in', | ||
es: '.es' | ||
} | ||
} | ||
|
||
/** | ||
* Audible will sometimes send sequences with "Book 1" or "2, Dramatized Adaptation" | ||
* @see https://github.com/advplyr/audiobookshelf/issues/2380 | ||
* @see https://github.com/advplyr/audiobookshelf/issues/1339 | ||
* | ||
* @param {string} seriesName | ||
* @param {string} sequence | ||
* @returns {string} | ||
*/ | ||
cleanSeriesSequence(seriesName, sequence) { | ||
if (!sequence) return '' | ||
// match any number with optional decimal (e.g, 1 or 1.5 or .5) | ||
let numberFound = sequence.match(/\.\d+|\d+(?:\.\d+)?/) | ||
let updatedSequence = numberFound ? numberFound[0] : sequence | ||
if (sequence !== updatedSequence) { | ||
Logger.debug(`[AudiMeta] Series "${seriesName}" sequence was cleaned from "${sequence}" to "${updatedSequence}"`) | ||
} | ||
return updatedSequence | ||
} | ||
|
||
cleanResult(item) { | ||
const { title, subtitle, asin, authors, narrators, publisherName, summary, releaseDate, imageUrl, genres, series, language, lengthMinutes, bookFormat } = item | ||
|
||
const seriesList = [] | ||
|
||
series.forEach((s) => { | ||
seriesList.push({ | ||
series: s.name, | ||
sequence: this.cleanSeriesSequence(s.name, (s.position || '').toString()) | ||
}) | ||
}) | ||
|
||
// Tags and Genres are flipped for AudiMeta | ||
const genresFiltered = genres ? genres.filter((g) => g.type == 'Tags').map((g) => g.name) : [] | ||
const tagsFiltered = genres ? genres.filter((g) => g.type == 'Genres').map((g) => g.name) : [] | ||
|
||
return { | ||
title, | ||
subtitle: subtitle || null, | ||
author: authors ? authors.map(({ name }) => name).join(', ') : null, | ||
narrator: narrators ? narrators.map(({ name }) => name).join(', ') : null, | ||
publisher: publisherName, | ||
publishedYear: releaseDate ? releaseDate.split('-')[0] : null, | ||
description: summary || null, | ||
cover: imageUrl, | ||
asin, | ||
genres: genresFiltered.length ? genresFiltered : null, | ||
tags: tagsFiltered.length ? tagsFiltered.join(', ') : null, | ||
series: seriesList.length ? seriesList : null, | ||
language: language ? language.charAt(0).toUpperCase() + language.slice(1) : null, | ||
duration: lengthMinutes && !isNaN(lengthMinutes) ? Number(lengthMinutes) : 0, | ||
region: item.region || null, | ||
rating: item.rating || null, | ||
abridged: bookFormat === 'abridged' | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param {string} asin | ||
* @param {string} region | ||
* @param {number} [timeout] response timeout in ms | ||
* @returns {Promise<Object>} | ||
*/ | ||
asinSearch(asin, region, timeout = this.#responseTimeout) { | ||
if (!asin) return null | ||
if (!timeout || isNaN(timeout)) timeout = this.#responseTimeout | ||
|
||
asin = encodeURIComponent(asin.toUpperCase()) | ||
let regionQuery = region ? `?region=${region}` : '' | ||
let url = `https://audimeta.de/book/${asin}${regionQuery}` | ||
Logger.debug(`[AudiMeta] ASIN url: ${url}`) | ||
return axios | ||
.get(url, { | ||
timeout | ||
}) | ||
.then((res) => { | ||
if (!res?.data?.asin) return null | ||
return res.data | ||
}) | ||
.catch((error) => { | ||
Logger.error('[Audible] ASIN search error', error) | ||
return null | ||
}) | ||
} | ||
|
||
/** | ||
* | ||
* @param {string} title | ||
* @param {string} author | ||
* @param {string} asin | ||
* @param {string} region | ||
* @param {number} [timeout] response timeout in ms | ||
* @returns {Promise<Object[]>} | ||
*/ | ||
async search(title, author, asin, region, timeout = this.#responseTimeout) { | ||
if (region && !this.regionMap[region]) { | ||
Logger.error(`[AudiMeta] search: Invalid region ${region}`) | ||
region = '' | ||
} | ||
if (!timeout || isNaN(timeout)) timeout = this.#responseTimeout | ||
|
||
let items = [] | ||
if (asin && isValidASIN(asin.toUpperCase())) { | ||
const item = await this.asinSearch(asin.toUpperCase(), region, timeout) | ||
if (item) items.push(item) | ||
} | ||
|
||
if (!items.length && isValidASIN(title.toUpperCase())) { | ||
const item = await this.asinSearch(title.toUpperCase(), region, timeout) | ||
if (item) items.push(item) | ||
} | ||
|
||
if (!items.length) { | ||
const queryObj = { | ||
title: title, | ||
region: region, | ||
limit: '10' | ||
} | ||
if (author) queryObj.author = author | ||
const queryString = new URLSearchParams(queryObj).toString() | ||
|
||
const url = `https://audimeta.de/search?${queryString}` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hardcoded domain here. |
||
Logger.debug(`[AudiMeta] Search url: ${url}`) | ||
items = await axios | ||
.get(url, { | ||
timeout | ||
}) | ||
.then((res) => { | ||
return res.data | ||
}) | ||
.catch((error) => { | ||
Logger.error('[AudiMeta] query search error', error) | ||
return [] | ||
}) | ||
} | ||
return items.filter(Boolean).map((item) => this.cleanResult(item)) || [] | ||
} | ||
} | ||
|
||
module.exports = AudiMeta |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the domain be hardcoded here?