mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-06-20 17:58:01 +02:00
Merge pull request #4383 from JKubovy/improve-podcast-episode-search
Use fuse.js for podcast episode search
This commit is contained in:
commit
5025c6a3ea
13
server/libs/fusejs/index.js
Normal file
13
server/libs/fusejs/index.js
Normal file
File diff suppressed because one or more lines are too long
@ -370,7 +370,7 @@ class Scanner {
|
|||||||
|
|
||||||
let numEpisodesUpdated = 0
|
let numEpisodesUpdated = 0
|
||||||
for (const episode of episodesToQuickMatch) {
|
for (const episode of episodesToQuickMatch) {
|
||||||
const episodeMatches = findMatchingEpisodesInFeed(feed, episode.title)
|
const episodeMatches = findMatchingEpisodesInFeed(feed, episode.title, 0.1)
|
||||||
if (episodeMatches?.length) {
|
if (episodeMatches?.length) {
|
||||||
const wasUpdated = await this.updateEpisodeWithMatch(episode, episodeMatches[0].episode, options)
|
const wasUpdated = await this.updateEpisodeWithMatch(episode, episodeMatches[0].episode, options)
|
||||||
if (wasUpdated) numEpisodesUpdated++
|
if (wasUpdated) numEpisodesUpdated++
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
const axios = require('axios')
|
const axios = require('axios')
|
||||||
const ssrfFilter = require('ssrf-req-filter')
|
const ssrfFilter = require('ssrf-req-filter')
|
||||||
const Logger = require('../Logger')
|
const Logger = require('../Logger')
|
||||||
const { xmlToJSON, levenshteinDistance, timestampToSeconds } = require('./index')
|
const { xmlToJSON, timestampToSeconds } = require('./index')
|
||||||
const htmlSanitizer = require('../utils/htmlSanitizer')
|
const htmlSanitizer = require('../utils/htmlSanitizer')
|
||||||
|
const Fuse = require('../libs/fusejs')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef RssPodcastChapter
|
* @typedef RssPodcastChapter
|
||||||
@ -407,7 +408,7 @@ module.exports.getPodcastFeed = (feedUrl, excludeEpisodeMetadata = false) => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return array of episodes ordered by closest match (Levenshtein distance of 6 or less)
|
// Return array of episodes ordered by closest match using fuse.js
|
||||||
module.exports.findMatchingEpisodes = async (feedUrl, searchTitle) => {
|
module.exports.findMatchingEpisodes = async (feedUrl, searchTitle) => {
|
||||||
const feed = await this.getPodcastFeed(feedUrl).catch(() => {
|
const feed = await this.getPodcastFeed(feedUrl).catch(() => {
|
||||||
return null
|
return null
|
||||||
@ -420,32 +421,29 @@ module.exports.findMatchingEpisodes = async (feedUrl, searchTitle) => {
|
|||||||
*
|
*
|
||||||
* @param {RssPodcast} feed
|
* @param {RssPodcast} feed
|
||||||
* @param {string} searchTitle
|
* @param {string} searchTitle
|
||||||
* @returns {Array<{ episode: RssPodcastEpisode, levenshtein: number }>}
|
* @param {number} [threshold=0.4] - 0.0 for perfect match, 1.0 for match anything
|
||||||
|
* @returns {Array<{ episode: RssPodcastEpisode }>}
|
||||||
*/
|
*/
|
||||||
module.exports.findMatchingEpisodesInFeed = (feed, searchTitle) => {
|
module.exports.findMatchingEpisodesInFeed = (feed, searchTitle, threshold = 0.4) => {
|
||||||
searchTitle = searchTitle.toLowerCase().trim()
|
|
||||||
if (!feed?.episodes) {
|
if (!feed?.episodes) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const fuseOptions = {
|
||||||
|
ignoreDiacritics: true,
|
||||||
|
threshold,
|
||||||
|
keys: [
|
||||||
|
{ name: 'title', weight: 0.7 }, // prefer match in title
|
||||||
|
{ name: 'subtitle', weight: 0.3 }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
const fuse = new Fuse(feed.episodes, fuseOptions)
|
||||||
|
|
||||||
const matches = []
|
const matches = []
|
||||||
feed.episodes.forEach((ep) => {
|
fuse.search(searchTitle).forEach((match) => {
|
||||||
if (!ep.title) return
|
|
||||||
const epTitle = ep.title.toLowerCase().trim()
|
|
||||||
if (epTitle === searchTitle) {
|
|
||||||
matches.push({
|
matches.push({
|
||||||
episode: ep,
|
episode: match.item
|
||||||
levenshtein: 0
|
|
||||||
})
|
})
|
||||||
} else {
|
|
||||||
const levenshtein = levenshteinDistance(searchTitle, epTitle, true)
|
|
||||||
if (levenshtein <= 6 && epTitle.length > levenshtein) {
|
|
||||||
matches.push({
|
|
||||||
episode: ep,
|
|
||||||
levenshtein
|
|
||||||
})
|
})
|
||||||
}
|
return matches
|
||||||
}
|
|
||||||
})
|
|
||||||
return matches.sort((a, b) => a.levenshtein - b.levenshtein)
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user