mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-05-13 06:51:29 +00:00
Merge 6f19bb1e0c into 47ea6b5092
This commit is contained in:
commit
2ec3255676
4 changed files with 188 additions and 4 deletions
54
server/utils/parsers/parseDate.js
Normal file
54
server/utils/parsers/parseDate.js
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
/**
|
||||
* Parse a date string with multiple fallback formats
|
||||
*
|
||||
* @example
|
||||
* parse('2024-01-15') => Date object for Jan 15, 2024
|
||||
* parse('20240325') => Date object for Mar 25, 2024
|
||||
* parse('250312') => Date object for Mar 12, 2025 (year <= 50 as 20xx)
|
||||
* parse('750312') => Date object for Mar 12, 1975 (year > 50 as 19xx)
|
||||
*
|
||||
* @param {string} dateString The date string to parse
|
||||
* @returns {Date|null} Date object or null if unparseable
|
||||
*/
|
||||
function parseDate(dateString) {
|
||||
if (!dateString || typeof dateString !== 'string') return null
|
||||
|
||||
const date = new Date(dateString)
|
||||
if (!isNaN(date.getTime())) {
|
||||
const year = date.getFullYear()
|
||||
if (year >= 0 && year <= 9999) {
|
||||
return date
|
||||
}
|
||||
}
|
||||
|
||||
const yyyymmddMatch = dateString.match(/^(\d{4})(\d{2})(\d{2})$/)
|
||||
if (yyyymmddMatch) {
|
||||
const [, year, month, day] = yyyymmddMatch
|
||||
const monthNum = parseInt(month)
|
||||
const dayNum = parseInt(day)
|
||||
if (monthNum >= 1 && monthNum <= 12 && dayNum >= 1 && dayNum <= 31) {
|
||||
const parsedDate = new Date(parseInt(year), monthNum - 1, dayNum)
|
||||
if (!isNaN(parsedDate.getTime())) {
|
||||
return parsedDate
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const yymmddMatch = dateString.match(/^(\d{2})(\d{2})(\d{2})$/)
|
||||
if (yymmddMatch) {
|
||||
const [, year, month, day] = yymmddMatch
|
||||
const monthNum = parseInt(month)
|
||||
const dayNum = parseInt(day)
|
||||
if (monthNum >= 1 && monthNum <= 12 && dayNum >= 1 && dayNum <= 31) {
|
||||
const fullYear = parseInt(year) > 50 ? 1900 + parseInt(year) : 2000 + parseInt(year)
|
||||
const parsedDate = new Date(fullYear, monthNum - 1, dayNum)
|
||||
if (!isNaN(parsedDate.getTime())) {
|
||||
return parsedDate
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
module.exports.parse = parseDate
|
||||
|
|
@ -4,6 +4,7 @@ const Logger = require('../Logger')
|
|||
const { xmlToJSON, timestampToSeconds } = require('./index')
|
||||
const htmlSanitizer = require('../utils/htmlSanitizer')
|
||||
const Fuse = require('../libs/fusejs')
|
||||
const parseDate = require('../utils/parsers/parseDate')
|
||||
|
||||
/**
|
||||
* @typedef RssPodcastChapter
|
||||
|
|
@ -147,6 +148,12 @@ function extractPodcastMetadata(channel) {
|
|||
if (value?.['_']) value = value['_']
|
||||
metadata[cleanKey] = value
|
||||
})
|
||||
|
||||
if (!metadata['pubDate']) {
|
||||
const dateVal = extractFirstArrayItem(channel, 'date')
|
||||
if (dateVal) metadata['pubDate'] = dateVal
|
||||
}
|
||||
|
||||
return metadata
|
||||
}
|
||||
|
||||
|
|
@ -198,6 +205,15 @@ function extractEpisodeData(item) {
|
|||
} else {
|
||||
Logger.error(`[podcastUtils] Invalid pubDate ${item['pubDate']} for ${episode.enclosure.url}`)
|
||||
}
|
||||
} else if (item['date']) {
|
||||
const date = extractFirstArrayItem(item, 'date')
|
||||
if (typeof date === 'string') {
|
||||
episode.pubDate = date
|
||||
} else if (typeof date?._ === 'string') {
|
||||
episode.pubDate = date._
|
||||
} else {
|
||||
Logger.error(`[podcastUtils] Invalid date ${item['date']} for ${episode.enclosure.url}`)
|
||||
}
|
||||
}
|
||||
|
||||
if (item['guid']) {
|
||||
|
|
@ -265,7 +281,7 @@ function extractEpisodeData(item) {
|
|||
}
|
||||
|
||||
function cleanEpisodeData(data) {
|
||||
const pubJsDate = data.pubDate ? new Date(data.pubDate) : null
|
||||
const pubJsDate = data.pubDate ? parseDate.parse(data.pubDate) : null
|
||||
const publishedAt = pubJsDate && !isNaN(pubJsDate) ? pubJsDate.valueOf() : null
|
||||
|
||||
return {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue