audiobookshelf/server/objects/metadata/PodcastMetadata.js

128 lines
3.5 KiB
JavaScript
Raw Normal View History

const Logger = require('../../Logger')
2023-10-20 00:20:12 +02:00
const { areEquivalent, copyValue, getTitleIgnorePrefix, getTitlePrefixAtEnd } = require('../../utils/index')
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
this.imageUrl = null
2022-03-09 02:31:44 +01:00
this.itunesPageUrl = null
this.itunesId = null
this.itunesArtistId = null
this.explicit = false
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
this.imageUrl = metadata.imageUrl
2022-03-09 02:31:44 +01:00
this.itunesPageUrl = metadata.itunesPageUrl
this.itunesId = metadata.itunesId
this.itunesArtistId = metadata.itunesArtistId
this.explicit = metadata.explicit
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,
imageUrl: this.imageUrl,
2022-03-09 02:31:44 +01:00
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-03-09 02:31:44 +01:00
}
}
toJSONMinified() {
return {
title: this.title,
titleIgnorePrefix: this.titlePrefixAtEnd,
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
}
}
toJSONExpanded() {
return this.toJSONMinified()
}
clone() {
return new PodcastMetadata(this.toJSON())
}
get titleIgnorePrefix() {
return getTitleIgnorePrefix(this.title)
}
get titlePrefixAtEnd() {
return getTitlePrefixAtEnd(this.title)
}
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
if (mediaMetadata.genres && mediaMetadata.genres.length) {
this.genres = [...mediaMetadata.genres]
}
}
update(payload) {
const json = this.toJSON()
let hasUpdates = false
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
}
2022-03-09 02:31:44 +01:00
}
2023-02-22 19:22:52 +01:00
module.exports = PodcastMetadata