2022-03-26 21:23:25 +01:00
|
|
|
const Logger = require('../../Logger')
|
2022-11-03 05:14:07 +01:00
|
|
|
const { areEquivalent, copyValue, cleanStringForSearch, getTitleIgnorePrefix, getTitlePrefixAtEnd } = require('../../utils/index')
|
2022-03-26 21:23:25 +01:00
|
|
|
|
2022-03-09 02:31:44 +01:00
|
|
|
class PodcastMetadata {
|
|
|
|
constructor(metadata) {
|
|
|
|
this.title = null
|
2022-03-19 01:16:54 +01:00
|
|
|
this.author = null
|
2022-03-09 02:31:44 +01:00
|
|
|
this.description = null
|
|
|
|
this.releaseDate = null
|
|
|
|
this.genres = []
|
|
|
|
this.feedUrl = null
|
2022-03-19 12:41:54 +01:00
|
|
|
this.imageUrl = null
|
2022-03-09 02:31:44 +01:00
|
|
|
this.itunesPageUrl = null
|
|
|
|
this.itunesId = null
|
|
|
|
this.itunesArtistId = null
|
2022-03-11 01:45:02 +01:00
|
|
|
this.explicit = false
|
2022-03-19 12:41:54 +01:00
|
|
|
this.language = null
|
2023-02-22 19:22:52 +01:00
|
|
|
this.type = null
|
2022-03-09 02:31:44 +01:00
|
|
|
|
|
|
|
if (metadata) {
|
|
|
|
this.construct(metadata)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
construct(metadata) {
|
|
|
|
this.title = metadata.title
|
2022-03-19 01:16:54 +01:00
|
|
|
this.author = metadata.author
|
2022-03-09 02:31:44 +01:00
|
|
|
this.description = metadata.description
|
|
|
|
this.releaseDate = metadata.releaseDate
|
|
|
|
this.genres = [...metadata.genres]
|
|
|
|
this.feedUrl = metadata.feedUrl
|
2022-03-19 12:41:54 +01:00
|
|
|
this.imageUrl = metadata.imageUrl
|
2022-03-09 02:31:44 +01:00
|
|
|
this.itunesPageUrl = metadata.itunesPageUrl
|
|
|
|
this.itunesId = metadata.itunesId
|
|
|
|
this.itunesArtistId = metadata.itunesArtistId
|
2022-03-11 01:45:02 +01:00
|
|
|
this.explicit = metadata.explicit
|
2022-03-19 12:41:54 +01:00
|
|
|
this.language = metadata.language || null
|
2023-02-22 19:22:52 +01:00
|
|
|
this.type = metadata.type || 'episodic'
|
2022-03-09 02:31:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
title: this.title,
|
2022-03-19 01:16:54 +01:00
|
|
|
author: this.author,
|
2022-03-09 02:31:44 +01:00
|
|
|
description: this.description,
|
|
|
|
releaseDate: this.releaseDate,
|
|
|
|
genres: [...this.genres],
|
|
|
|
feedUrl: this.feedUrl,
|
2022-03-19 12:41:54 +01:00
|
|
|
imageUrl: this.imageUrl,
|
2022-03-09 02:31:44 +01:00
|
|
|
itunesPageUrl: this.itunesPageUrl,
|
|
|
|
itunesId: this.itunesId,
|
|
|
|
itunesArtistId: this.itunesArtistId,
|
2022-03-19 12:41:54 +01:00
|
|
|
explicit: this.explicit,
|
2023-02-22 19:22:52 +01:00
|
|
|
language: this.language,
|
|
|
|
type: this.type
|
2022-03-09 02:31:44 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-11 01:45:02 +01:00
|
|
|
|
2022-04-12 02:42:09 +02:00
|
|
|
toJSONMinified() {
|
|
|
|
return {
|
|
|
|
title: this.title,
|
2022-11-03 05:14:07 +01:00
|
|
|
titleIgnorePrefix: this.titlePrefixAtEnd,
|
2022-04-12 02:42:09 +02:00
|
|
|
author: this.author,
|
|
|
|
description: this.description,
|
|
|
|
releaseDate: this.releaseDate,
|
|
|
|
genres: [...this.genres],
|
|
|
|
feedUrl: this.feedUrl,
|
|
|
|
imageUrl: this.imageUrl,
|
|
|
|
itunesPageUrl: this.itunesPageUrl,
|
|
|
|
itunesId: this.itunesId,
|
|
|
|
itunesArtistId: this.itunesArtistId,
|
|
|
|
explicit: this.explicit,
|
2023-02-22 19:22:52 +01:00
|
|
|
language: this.language,
|
|
|
|
type: this.type
|
2022-04-12 02:42:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-11 01:45:02 +01:00
|
|
|
toJSONExpanded() {
|
2022-04-12 02:42:09 +02:00
|
|
|
return this.toJSONMinified()
|
2022-03-11 01:45:02 +01:00
|
|
|
}
|
2022-03-13 18:39:12 +01:00
|
|
|
|
2022-03-16 00:57:15 +01:00
|
|
|
clone() {
|
|
|
|
return new PodcastMetadata(this.toJSON())
|
|
|
|
}
|
|
|
|
|
2022-04-12 02:42:09 +02:00
|
|
|
get titleIgnorePrefix() {
|
2022-11-03 05:14:07 +01:00
|
|
|
return getTitleIgnorePrefix(this.title)
|
|
|
|
}
|
|
|
|
|
|
|
|
get titlePrefixAtEnd() {
|
|
|
|
return getTitlePrefixAtEnd(this.title)
|
2022-04-12 02:42:09 +02:00
|
|
|
}
|
|
|
|
|
2022-03-13 18:39:12 +01:00
|
|
|
searchQuery(query) { // Returns key if match is found
|
2022-12-21 14:13:28 +01:00
|
|
|
const keysToCheck = ['title', 'author', 'itunesId', 'itunesArtistId']
|
|
|
|
for (const key of keysToCheck) {
|
2022-06-26 22:46:16 +02:00
|
|
|
if (this[key] && cleanStringForSearch(String(this[key])).includes(query)) {
|
2022-03-13 18:39:12 +01:00
|
|
|
return {
|
|
|
|
matchKey: key,
|
|
|
|
matchText: this[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
2022-03-22 01:24:38 +01:00
|
|
|
|
|
|
|
setData(mediaMetadata = {}) {
|
|
|
|
this.title = mediaMetadata.title || null
|
|
|
|
this.author = mediaMetadata.author || null
|
|
|
|
this.description = mediaMetadata.description || null
|
|
|
|
this.releaseDate = mediaMetadata.releaseDate || null
|
|
|
|
this.feedUrl = mediaMetadata.feedUrl || null
|
|
|
|
this.imageUrl = mediaMetadata.imageUrl || null
|
|
|
|
this.itunesPageUrl = mediaMetadata.itunesPageUrl || null
|
|
|
|
this.itunesId = mediaMetadata.itunesId || null
|
|
|
|
this.itunesArtistId = mediaMetadata.itunesArtistId || null
|
|
|
|
this.explicit = !!mediaMetadata.explicit
|
|
|
|
this.language = mediaMetadata.language || null
|
2023-02-22 19:22:52 +01:00
|
|
|
this.type = mediaMetadata.type || null
|
2022-03-22 01:24:38 +01:00
|
|
|
if (mediaMetadata.genres && mediaMetadata.genres.length) {
|
|
|
|
this.genres = [...mediaMetadata.genres]
|
|
|
|
}
|
|
|
|
}
|
2022-03-26 21:23:25 +01:00
|
|
|
|
|
|
|
update(payload) {
|
2022-12-21 14:13:28 +01:00
|
|
|
const json = this.toJSON()
|
|
|
|
let hasUpdates = false
|
2022-03-26 21:23:25 +01:00
|
|
|
for (const key in json) {
|
|
|
|
if (payload[key] !== undefined) {
|
|
|
|
if (!areEquivalent(payload[key], json[key])) {
|
|
|
|
this[key] = copyValue(payload[key])
|
|
|
|
Logger.debug('[PodcastMetadata] Key updated', key, this[key])
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hasUpdates
|
|
|
|
}
|
2023-03-31 01:04:21 +02:00
|
|
|
|
|
|
|
setDataFromAudioMetaTags(audioFileMetaTags, overrideExistingDetails = false) {
|
|
|
|
const MetadataMapArray = [
|
|
|
|
{
|
|
|
|
tag: 'tagAlbum',
|
|
|
|
altTag: 'tagSeries',
|
|
|
|
key: 'title'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: 'tagArtist',
|
|
|
|
key: 'author'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: 'tagGenre',
|
|
|
|
key: 'genres'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: 'tagLanguage',
|
|
|
|
key: 'language'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: 'tagItunesId',
|
|
|
|
key: 'itunesId'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: 'tagPodcastType',
|
|
|
|
key: 'type',
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
const updatePayload = {}
|
|
|
|
|
|
|
|
MetadataMapArray.forEach((mapping) => {
|
|
|
|
let value = audioFileMetaTags[mapping.tag]
|
|
|
|
let tagToUse = mapping.tag
|
|
|
|
if (!value && mapping.altTag) {
|
|
|
|
value = audioFileMetaTags[mapping.altTag]
|
|
|
|
tagToUse = mapping.altTag
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value && typeof value === 'string') {
|
|
|
|
value = value.trim() // Trim whitespace
|
|
|
|
|
|
|
|
if (mapping.key === 'genres' && (!this.genres.length || overrideExistingDetails)) {
|
|
|
|
updatePayload.genres = this.parseGenresTag(value)
|
|
|
|
Logger.debug(`[Podcast] Mapping metadata to key ${tagToUse} => ${mapping.key}: ${updatePayload.genres.join(', ')}`)
|
|
|
|
} else if (!this[mapping.key] || overrideExistingDetails) {
|
|
|
|
updatePayload[mapping.key] = value
|
|
|
|
Logger.debug(`[Podcast] Mapping metadata to key ${tagToUse} => ${mapping.key}: ${updatePayload[mapping.key]}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (Object.keys(updatePayload).length) {
|
|
|
|
return this.update(updatePayload)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
parseGenresTag(genreTag) {
|
|
|
|
if (!genreTag || !genreTag.length) return []
|
|
|
|
const separators = ['/', '//', ';']
|
|
|
|
for (let i = 0; i < separators.length; i++) {
|
|
|
|
if (genreTag.includes(separators[i])) {
|
|
|
|
return genreTag.split(separators[i]).map(genre => genre.trim()).filter(g => !!g)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [genreTag]
|
|
|
|
}
|
2022-03-09 02:31:44 +01:00
|
|
|
}
|
2023-02-22 19:22:52 +01:00
|
|
|
module.exports = PodcastMetadata
|