Update podcast search page to support manually entering podcast RSS feed

This commit is contained in:
advplyr 2022-04-13 16:55:48 -05:00
parent 2c6e1cc2b5
commit 4edba20e9e
6 changed files with 87 additions and 37 deletions

View File

@ -56,7 +56,6 @@
<p class="break-words mb-1">{{ episode.title }}</p> <p class="break-words mb-1">{{ episode.title }}</p>
<p v-if="episode.subtitle" class="break-words mb-1 text-sm text-gray-300 episode-subtitle">{{ episode.subtitle }}</p> <p v-if="episode.subtitle" class="break-words mb-1 text-sm text-gray-300 episode-subtitle">{{ episode.subtitle }}</p>
<p class="text-xs text-gray-300">Published {{ episode.publishedAt ? $dateDistanceFromNow(episode.publishedAt) : 'Unknown' }}</p> <p class="text-xs text-gray-300">Published {{ episode.publishedAt ? $dateDistanceFromNow(episode.publishedAt) : 'Unknown' }}</p>
<!-- <span class="material-icons cursor-pointer text-lg hover:text-success" @click="saveEpisode(episode)">save</span> -->
</div> </div>
</div> </div>
</div> </div>
@ -246,17 +245,15 @@ export default {
this.$toast.error(errorMsg) this.$toast.error(errorMsg)
}) })
}, },
saveEpisode(episode) {
console.log('Save episode', episode)
},
init() { init() {
this.podcast.title = this._podcastData.title // Prefer using itunes podcast data but not always passed in if manually entering rss feed
this.podcast.author = this._podcastData.artistName || '' this.podcast.title = this._podcastData.title || this.feedMetadata.title || ''
this.podcast.description = this._podcastData.description || this.feedMetadata.description || '' this.podcast.author = this._podcastData.artistName || this.feedMetadata.author || ''
this.podcast.description = this._podcastData.description || this.feedMetadata.descriptionPlain || ''
this.podcast.releaseDate = this._podcastData.releaseDate || '' this.podcast.releaseDate = this._podcastData.releaseDate || ''
this.podcast.genres = this._podcastData.genres || [] this.podcast.genres = this._podcastData.genres || this.feedMetadata.categories || []
this.podcast.feedUrl = this._podcastData.feedUrl this.podcast.feedUrl = this._podcastData.feedUrl || this.feedMetadata.feedUrl || ''
this.podcast.imageUrl = this._podcastData.cover || '' this.podcast.imageUrl = this._podcastData.cover || this.feedMetadata.image || ''
this.podcast.itunesPageUrl = this._podcastData.pageUrl || '' this.podcast.itunesPageUrl = this._podcastData.pageUrl || ''
this.podcast.itunesId = this._podcastData.id || '' this.podcast.itunesId = this._podcastData.id || ''
this.podcast.itunesArtistId = this._podcastData.artistId || '' this.podcast.itunesArtistId = this._podcastData.artistId || ''

View File

@ -342,15 +342,16 @@ export default {
return this.$toast.error('Podcast does not have an RSS Feed') return this.$toast.error('Podcast does not have an RSS Feed')
} }
this.fetchingRSSFeed = true this.fetchingRSSFeed = true
var podcastfeed = await this.$axios.$post(`/api/podcasts/feed`, { rssFeed: this.mediaMetadata.feedUrl }).catch((error) => { var payload = await this.$axios.$post(`/api/podcasts/feed`, { rssFeed: this.mediaMetadata.feedUrl }).catch((error) => {
console.error('Failed to get feed', error) console.error('Failed to get feed', error)
this.$toast.error('Failed to get podcast feed') this.$toast.error('Failed to get podcast feed')
return null return null
}) })
this.fetchingRSSFeed = false this.fetchingRSSFeed = false
if (!podcastfeed) return if (!payload) return
console.log('Podcast feed', podcastfeed) console.log('Podcast feed', payload)
const podcastfeed = payload.podcast
if (!podcastfeed.episodes || !podcastfeed.episodes.length) { if (!podcastfeed.episodes || !podcastfeed.episodes.length) {
this.$toast.info('No episodes found in RSS feed') this.$toast.info('No episodes found in RSS feed')
return return

View File

@ -6,9 +6,9 @@
<app-book-shelf-toolbar page="podcast-search" /> <app-book-shelf-toolbar page="podcast-search" />
<div class="w-full h-full overflow-y-auto p-12 relative"> <div class="w-full h-full overflow-y-auto p-12 relative">
<div class="w-full max-w-3xl mx-auto"> <div class="w-full max-w-3xl mx-auto">
<form @submit.prevent="submitSearch" class="flex"> <form @submit.prevent="submit" class="flex">
<ui-text-input v-model="searchTerm" :disabled="processing" placeholder="Search term" class="flex-grow mr-2" /> <ui-text-input v-model="searchInput" :disabled="processing" placeholder="Enter search term or RSS feed URL" class="flex-grow mr-2" />
<ui-btn type="submit" :disabled="processing">Search Podcasts</ui-btn> <ui-btn type="submit" :disabled="processing">Submit</ui-btn>
</form> </form>
</div> </div>
@ -54,11 +54,10 @@ export default {
}, },
data() { data() {
return { return {
searchTerm: '', searchInput: '',
results: [], results: [],
termSearched: '', termSearched: '',
processing: false, processing: false,
showNewPodcastModal: false, showNewPodcastModal: false,
selectedPodcast: null, selectedPodcast: null,
selectedPodcastFeed: null selectedPodcastFeed: null
@ -70,13 +69,35 @@ export default {
} }
}, },
methods: { methods: {
async submitSearch() { submit() {
if (!this.searchTerm) return if (!this.searchInput) return
console.log('Searching', this.searchTerm)
var term = this.searchTerm if (this.searchInput.startsWith('http:') || this.searchInput.startsWith('https:')) {
this.termSearched = ''
this.results = []
this.checkRSSFeed(this.searchInput)
} else {
this.submitSearch(this.searchInput)
}
},
async checkRSSFeed(rssFeed) {
this.processing = true
var payload = await this.$axios.$post(`/api/podcasts/feed`, { rssFeed }).catch((error) => {
console.error('Failed to get feed', error)
this.$toast.error('Failed to get podcast feed')
return null
})
this.processing = false
if (!payload) return
this.selectedPodcastFeed = payload.podcast
this.selectedPodcast = null
this.showNewPodcastModal = true
},
async submitSearch(term) {
this.processing = true this.processing = true
this.termSearched = '' this.termSearched = ''
var results = await this.$axios.$get(`/api/search/podcast?term=${encodeURIComponent(this.searchTerm)}`).catch((error) => { var results = await this.$axios.$get(`/api/search/podcast?term=${encodeURIComponent(term)}`).catch((error) => {
console.error('Search request failed', error) console.error('Search request failed', error)
return [] return []
}) })
@ -92,17 +113,18 @@ export default {
return return
} }
this.processing = true this.processing = true
var podcastfeed = await this.$axios.$post(`/api/podcasts/feed`, { rssFeed: podcast.feedUrl }).catch((error) => { var payload = await this.$axios.$post(`/api/podcasts/feed`, { rssFeed: podcast.feedUrl }).catch((error) => {
console.error('Failed to get feed', error) console.error('Failed to get feed', error)
this.$toast.error('Failed to get podcast feed') this.$toast.error('Failed to get podcast feed')
return null return null
}) })
this.processing = false this.processing = false
if (!podcastfeed) return if (!payload) return
this.selectedPodcastFeed = podcastfeed
this.selectedPodcastFeed = payload.podcast
this.selectedPodcast = podcast this.selectedPodcast = podcast
this.showNewPodcastModal = true this.showNewPodcastModal = true
console.log('Got podcast feed', podcastfeed) console.log('Got podcast feed', payload.podcast)
} }
}, },
mounted() {} mounted() {}

View File

@ -94,17 +94,18 @@ class PodcastController {
if (!url) { if (!url) {
return res.status(400).send('Bad request') return res.status(400).send('Bad request')
} }
var includeRaw = req.query.raw == 1 // Include raw json
axios.get(url).then(async (data) => { axios.get(url).then(async (data) => {
if (!data || !data.data) { if (!data || !data.data) {
Logger.error('Invalid podcast feed request response') Logger.error('Invalid podcast feed request response')
return res.status(500).send('Bad response from feed request') return res.status(500).send('Bad response from feed request')
} }
var podcast = await parsePodcastRssFeedXml(data.data) var payload = await parsePodcastRssFeedXml(data.data, includeRaw)
if (!podcast) { if (!payload) {
return res.status(500).send('Invalid podcast RSS feed') return res.status(500).send('Invalid podcast RSS feed')
} }
res.json(podcast) res.json(payload)
}).catch((error) => { }).catch((error) => {
console.error('Failed', error) console.error('Failed', error)
res.status(500).send(error) res.status(500).send(error)

View File

@ -190,11 +190,11 @@ class PodcastManager {
Logger.error('Invalid podcast feed request response') Logger.error('Invalid podcast feed request response')
return false return false
} }
var podcast = await parsePodcastRssFeedXml(data.data) var payload = await parsePodcastRssFeedXml(data.data)
if (!podcast) { if (!payload) {
return false return false
} }
return podcast return payload.podcast
}).catch((error) => { }).catch((error) => {
console.error('Failed', error) console.error('Failed', error)
return false return false

View File

@ -1,5 +1,6 @@
const Logger = require('../Logger') const Logger = require('../Logger')
const { xmlToJSON } = require('./index') const { xmlToJSON } = require('./index')
const { stripHtml } = require('string-strip-html')
function extractFirstArrayItem(json, key) { function extractFirstArrayItem(json, key) {
if (!json[key] || !json[key].length) return null if (!json[key] || !json[key].length) return null
@ -39,11 +40,26 @@ function extractCategories(channel) {
} }
function extractPodcastMetadata(channel) { function extractPodcastMetadata(channel) {
var arrayFields = ['title', 'language', 'description', 'itunes:explicit', 'itunes:author']
var metadata = { var metadata = {
image: extractImage(channel), image: extractImage(channel),
categories: extractCategories(channel) categories: extractCategories(channel),
feedUrl: null,
description: null,
descriptionPlain: null
} }
if (channel['itunes:new-feed-url']) {
metadata.feedUrl = extractFirstArrayItem(channel, 'itunes:new-feed-url')
} else if (channel['atom:link'] && channel['atom:link'].length && channel['atom:link'][0]['$']) {
metadata.feedUrl = channel['atom:link'][0]['$'].href || null
}
if (channel['description']) {
metadata.description = extractFirstArrayItem(channel, 'description')
metadata.descriptionPlain = stripHtml(metadata.description || '').result
}
var arrayFields = ['title', 'language', 'itunes:explicit', 'itunes:author', 'pubDate', 'link']
arrayFields.forEach((key) => { arrayFields.forEach((key) => {
var cleanKey = key.split(':').pop() var cleanKey = key.split(':').pop()
metadata[cleanKey] = extractFirstArrayItem(channel, key) metadata[cleanKey] = extractFirstArrayItem(channel, key)
@ -114,12 +130,25 @@ function cleanPodcastJson(rssJson) {
return podcast return podcast
} }
module.exports.parsePodcastRssFeedXml = async (xml) => { module.exports.parsePodcastRssFeedXml = async (xml, includeRaw = false) => {
if (!xml) return null if (!xml) return null
var json = await xmlToJSON(xml) var json = await xmlToJSON(xml)
if (!json || !json.rss) { if (!json || !json.rss) {
Logger.error('[podcastUtils] Invalid XML or RSS feed') Logger.error('[podcastUtils] Invalid XML or RSS feed')
return null return null
} }
return cleanPodcastJson(json.rss)
const podcast = cleanPodcastJson(json.rss)
if (!podcast) return null
if (includeRaw) {
return {
podcast,
rawJson: json
}
} else {
return {
podcast
}
}
} }