Compare commits

..

No commits in common. "96825c3c2b199b41975d3ee8a5266ed36288d1ab" and "ddcda197b4a709680089e030ab5ca0b489762786" have entirely different histories.

3 changed files with 2 additions and 83 deletions

View file

@ -80,13 +80,9 @@ class PodcastEpisode extends Model {
if (rssPodcastEpisode.guid) {
podcastEpisode.extraData.guid = rssPodcastEpisode.guid
}
if (audioFile.chapters?.length) {
podcastEpisode.chapters = audioFile.chapters.map((ch) => ({ ...ch }))
} else if (rssPodcastEpisode.chapters?.length) {
podcastEpisode.chapters = rssPodcastEpisode.chapters.map((ch) => ({ ...ch }))
}
return this.create(podcastEpisode)
}

View file

@ -243,29 +243,3 @@ module.exports.isValidASIN = (str) => {
if (!str || typeof str !== 'string') return false
return /^[A-Z0-9]{10}$/.test(str)
}
/**
* Convert timestamp to seconds
* @example "01:00:00" => 3600
* @example "01:00" => 60
* @example "01" => 1
*
* @param {string} timestamp
* @returns {number}
*/
module.exports.timestampToSeconds = (timestamp) => {
if (typeof timestamp !== 'string') {
return null
}
const parts = timestamp.split(':').map(Number)
if (parts.some(isNaN)) {
return null
} else if (parts.length === 1) {
return parts[0]
} else if (parts.length === 2) {
return parts[0] * 60 + parts[1]
} else if (parts.length === 3) {
return parts[0] * 3600 + parts[1] * 60 + parts[2]
}
return null
}

View file

@ -1,17 +1,9 @@
const axios = require('axios')
const ssrfFilter = require('ssrf-req-filter')
const Logger = require('../Logger')
const { xmlToJSON, levenshteinDistance, timestampToSeconds } = require('./index')
const { xmlToJSON, levenshteinDistance } = require('./index')
const htmlSanitizer = require('../utils/htmlSanitizer')
/**
* @typedef RssPodcastChapter
* @property {number} id
* @property {string} title
* @property {number} start
* @property {number} end
*/
/**
* @typedef RssPodcastEpisode
* @property {string} title
@ -30,7 +22,6 @@ const htmlSanitizer = require('../utils/htmlSanitizer')
* @property {string} guid
* @property {string} chaptersUrl
* @property {string} chaptersType
* @property {RssPodcastChapter[]} chapters
*/
/**
@ -214,53 +205,12 @@ function extractEpisodeData(item) {
const cleanKey = key.split(':').pop()
episode[cleanKey] = extractFirstArrayItemString(item, key)
})
// Extract psc:chapters if duration is set
let episodeDuration = !isNaN(episode.duration) ? timestampToSeconds(episode.duration) : null
if (item['psc:chapters']?.[0]?.['psc:chapter']?.length && episodeDuration) {
// Example chapter:
// {"id":0,"start":0,"end":43.004286,"title":"chapter 1"}
const cleanedChapters = item['psc:chapters'][0]['psc:chapter'].map((chapter, index) => {
if (!chapter['$']?.title || !chapter['$']?.start || typeof chapter['$']?.start !== 'string' || typeof chapter['$']?.title !== 'string') {
return null
}
const start = timestampToSeconds(chapter['$'].start)
if (start === null) {
return null
}
return {
id: index,
title: chapter['$'].title,
start
}
})
if (cleanedChapters.some((chapter) => !chapter)) {
Logger.warn(`[podcastUtils] Invalid chapter data for ${episode.enclosure.url}`)
} else {
episode.chapters = cleanedChapters.map((chapter, index) => {
const nextChapter = cleanedChapters[index + 1]
const end = nextChapter ? nextChapter.start : episodeDuration
return {
id: chapter.id,
title: chapter.title,
start: chapter.start,
end
}
})
}
}
return episode
}
function cleanEpisodeData(data) {
const pubJsDate = data.pubDate ? new Date(data.pubDate) : null
const publishedAt = pubJsDate && !isNaN(pubJsDate) ? pubJsDate.valueOf() : null
return {
title: data.title,
subtitle: data.subtitle || '',
@ -277,8 +227,7 @@ function cleanEpisodeData(data) {
enclosure: data.enclosure,
guid: data.guid || null,
chaptersUrl: data.chaptersUrl || null,
chaptersType: data.chaptersType || null,
chapters: data.chapters || []
chaptersType: data.chaptersType || null
}
}