mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-07-16 14:21:41 +00:00
Add kindle provider
This commit is contained in:
parent
565ff36d4e
commit
fe1edd24bc
5 changed files with 767 additions and 8 deletions
|
|
@ -1,5 +1,6 @@
|
|||
const OpenLibrary = require('../providers/OpenLibrary')
|
||||
const GoogleBooks = require('../providers/GoogleBooks')
|
||||
const Kindle = require('../providers/Kindle')
|
||||
const Audible = require('../providers/Audible')
|
||||
const iTunes = require('../providers/iTunes')
|
||||
const Audnexus = require('../providers/Audnexus')
|
||||
|
|
@ -12,13 +13,14 @@ class BookFinder {
|
|||
constructor() {
|
||||
this.openLibrary = new OpenLibrary()
|
||||
this.googleBooks = new GoogleBooks()
|
||||
this.kindle = new Kindle()
|
||||
this.audible = new Audible()
|
||||
this.iTunesApi = new iTunes()
|
||||
this.audnexus = new Audnexus()
|
||||
this.fantLab = new FantLab()
|
||||
this.audiobookCovers = new AudiobookCovers()
|
||||
|
||||
this.providers = ['google', 'itunes', 'openlibrary', 'fantlab', 'audiobookcovers', 'audible', 'audible.ca', 'audible.uk', 'audible.au', 'audible.fr', 'audible.de', 'audible.jp', 'audible.it', 'audible.in', 'audible.es']
|
||||
this.providers = ['google', 'kindle', 'itunes', 'openlibrary', 'fantlab', 'audiobookcovers', 'audible', 'audible.ca', 'audible.uk', 'audible.au', 'audible.fr', 'audible.de', 'audible.jp', 'audible.it', 'audible.in', 'audible.es']
|
||||
|
||||
this.verbose = false
|
||||
}
|
||||
|
|
@ -150,6 +152,16 @@ class BookFinder {
|
|||
return books
|
||||
}
|
||||
|
||||
async getKindleResults(title, author) {
|
||||
var books = await this.kindle.search(title, author)
|
||||
if (this.verbose) Logger.debug(`Kindle Book Search Results: ${books.length || 0}`)
|
||||
if (books.errorCode) {
|
||||
Logger.error(`Kindle Search Error ${books.errorCode}`)
|
||||
return []
|
||||
}
|
||||
return books
|
||||
}
|
||||
|
||||
async getFantLabResults(title, author) {
|
||||
var books = await this.fantLab.search(title, author)
|
||||
if (this.verbose) Logger.debug(`FantLab Book Search Results: ${books.length || 0}`)
|
||||
|
|
@ -309,6 +321,8 @@ class BookFinder {
|
|||
|
||||
if (provider === 'google') {
|
||||
books = await this.getGoogleBooksResults(title, author)
|
||||
} else if (provider === 'kindle') {
|
||||
books = await this.getKindleResults(title, author)
|
||||
} else if (provider.startsWith('audible')) {
|
||||
books = await this.getAudibleResults(title, author, asin, provider)
|
||||
} else if (provider === 'itunes') {
|
||||
|
|
@ -319,8 +333,7 @@ class BookFinder {
|
|||
books = await this.getFantLabResults(title, author)
|
||||
} else if (provider === 'audiobookcovers') {
|
||||
books = await this.getAudiobookCoversResults(title)
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
books = await this.getGoogleBooksResults(title, author)
|
||||
}
|
||||
return books
|
||||
|
|
|
|||
61
server/providers/Kindle.js
Normal file
61
server/providers/Kindle.js
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
const axios = require('axios')
|
||||
const htmlSanitizer = require('../utils/htmlSanitizer') // Assuming htmlSanitizer is in the same folder as in Audible
|
||||
const Logger = require('../Logger')
|
||||
const xpath = require('xpath')
|
||||
const jsdom = require('jsdom')
|
||||
|
||||
class Kindle {
|
||||
cleanResult(result) {
|
||||
const format = xpath.select('.//a[contains(text(), "Kindle Edition")]//text()', result).join('')
|
||||
if (!format || !format.toLowerCase().includes('kindle')) return null
|
||||
|
||||
const asin = result.getAttribute('data-asin')
|
||||
if (!asin) return null
|
||||
|
||||
const coverUrlSet = xpath.select('.//img/@srcset', result)[0].value.split(/\s*,\s*/)
|
||||
const cover = coverUrlSet[coverUrlSet.length - 1].split(' ')[0] // Get largest cover
|
||||
const title = xpath.select('.//h2', result)[0].textContent.trim()
|
||||
const authorDiv = xpath.select('.//div[contains(@class, "a-color-secondary")]', result)[0]
|
||||
const authorParts = authorDiv.textContent.split(' ')
|
||||
const idx = authorParts.indexOf('by')
|
||||
const author = authorParts
|
||||
.slice(idx + 1)
|
||||
.join(' ')
|
||||
.split('|')[0]
|
||||
.trim()
|
||||
|
||||
return {
|
||||
asin,
|
||||
title,
|
||||
author,
|
||||
cover
|
||||
}
|
||||
}
|
||||
|
||||
async search(title, author) {
|
||||
const queryParams = new URLSearchParams()
|
||||
queryParams.append('i', 'digital-text')
|
||||
queryParams.append('k', title)
|
||||
if (author) {
|
||||
author = encodeURIComponent(author)
|
||||
queryParams.append('inauthor', author)
|
||||
}
|
||||
var url = `https://www.amazon.com/s/?${queryParams.toString()}`
|
||||
Logger.debug(`[Kindle] Search url: ${url}`)
|
||||
|
||||
var items = await axios
|
||||
.get(url)
|
||||
.then((result) => {
|
||||
if (!result || !result.data) return []
|
||||
const dom = new jsdom.JSDOM(result.data)
|
||||
return xpath.select('//div[contains(@class, "s-result-list")]//div[@data-index and @data-asin]', dom)
|
||||
})
|
||||
.catch((error) => {
|
||||
Logger.error('[Kindle] Query search error', error)
|
||||
return []
|
||||
})
|
||||
return items.map((item) => this.cleanResult(item)).filter((item) => item !== null)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Kindle
|
||||
Loading…
Add table
Add a link
Reference in a new issue