Early out if the description doesn't contain and timestamps

This commit is contained in:
Harry Rose 2026-03-17 19:43:09 +00:00
parent 8710816a6f
commit 7f88d4b036
3 changed files with 24 additions and 3 deletions

View file

@ -91,8 +91,12 @@ class PodcastEpisode extends Model {
Logger.debug("[PodcastEpisode] New episode doesn't have chapters, attempting to generate them from timestamps", rssPodcastEpisode.title)
try {
podcastEpisode.chapters = parsePodcastDescriptionForChapters.parse(podcastEpisode.description, podcastEpisode.audioFile.duration)
if (podcastEpisode.chapters.length > 0) {
Logger.info(`[PodcastEpisode] Successfully generated ${podcastEpisode.chapters.length} chapters`)
}
} catch (error) {
Logger.error(`[PodcastEpisode] createFromRssPodcastEpisode: Failed to auto generate chapters for "${podcastEpisode.title}"`, error)
Logger.error(`[PodcastEpisode] createFromRssPodcastEpisode: Failed to generate chapters for "${podcastEpisode.title}"`, error)
}
}

View file

@ -36,6 +36,12 @@ module.exports.parse = (podcastDescription, audioDurationSecs) => {
// Split on "</p>", "<br />", "\n", </li>
const descriptionLineSplitRegex = /\<\s*\/\s*p\s*\>|\<\s*br\s*\/\>|\n|\<\s*\/\s*li\s*\>/
// Early out if there aren't any timestamps in the entire description
if (timestampRegex.exec(podcastDescription) == null) {
Logger.debug('No timestamps found in description, bailing out early')
return []
}
var descriptionLines = podcastDescription.split(descriptionLineSplitRegex)
var newChapters = []
@ -98,8 +104,6 @@ module.exports.parse = (podcastDescription, audioDurationSecs) => {
newChapters[newChapters.length - 1].end = audioDurationSecs
}
Logger.info(`Successfully generated ${newChapters.length} chapters`)
if (newChapters.length == 1) {
throw new Error('Only one chapter found, treating as invalid description')
}

View file

@ -1,8 +1,21 @@
const chai = require('chai')
const expect = chai.expect
const parsePodcastDescriptionForChapters = require('../../../../server/utils/parsers/parsePodcastDescriptionForChapters')
const sinon = require('sinon')
const Logger = require('../../../../server/Logger')
describe('parsePodcastDescriptionForChapters', () => {
it("should early out if description doens't contain timestamps", () => {
let loggerDebugStub = sinon.stub(Logger, 'debug')
let description = '<p>Introduction text paragraph 1</p><p>Introduction text paragraph 2</p>'
let chapters = parsePodcastDescriptionForChapters.parse(description, 1000)
expect(chapters).to.be.empty
expect(loggerDebugStub.calledWith('No timestamps found in description, bailing out early')).to.be.true
sinon.restore()
})
var testCasesTestingSuccess = [
{
testName: 'Should handle descriptions using html paragraphs',