From b613aeb7163b9c75b5aa661d8a4175ff7a7b0faf Mon Sep 17 00:00:00 2001 From: Eric P Date: Thu, 9 Mar 2023 22:41:11 -0600 Subject: [PATCH] Check the episodes endpoint to see if recentl live items are actually live --- server/index.js | 50 ++++++++++++++++++++++++++++++---------- ui/src/pages/landing.tsx | 2 +- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/server/index.js b/server/index.js index f08d0db7..0fcf5813 100644 --- a/server/index.js +++ b/server/index.js @@ -113,24 +113,50 @@ app.use('/api/episodes/byfeedid', async (req, res) => { }) app.use('/api/episodes/live', async (req, res) => { - let max = req.query.max - const response = await api.custom('episodes/live', {pretty: true, max: max}) - const minPublished = Math.floor(new Date().getTime() / 1000) - 3600 + const max = req.query.max - // assume live episodes posted within the past hour are live - // since the api doesn't provide start and end times + // grab live items from recently updated feeds + // (large max value to work around sorting issue `docs-api#96`) + const updatedLiveItems = await api.custom('episodes/live', {max: 100}) - response.items = response.items.filter(item => { - return item.datePublished >= minPublished - }) + // re-sort to put recently updated live items first + updatedLiveItems.items.sort((a, b) => { + return (b.datePublished - a.datePublished) + }); + + const feedIds = new Set(updatedLiveItems.items.map(feed => feed.feedId)) + const feedTitles = updatedLiveItems.items.reduce((result, item) => { + result[item.feedId] = item.feedTitle + return result + }, {}) - response.count = response.items.length; + const liveItems = [] + const liveCutoff = 3600 // cut off perpetually live shows after an hour - if (response.count === 0) { - response.description = 'No matching items' + const now = Math.floor(new Date().getTime() / 1000) + + // check each individual feed and collect live items that are actually live (status = live) + for (const feedId of feedIds) { + const feed = await api.episodesByFeedId(feedId) + + for (const item of feed.liveItems) { + if (item.status === 'live' && now < (item.startTime + liveCutoff) && liveItems.length < max) { + item.feedTitle = feedTitles[item.feedId]; + liveItems.push(item); + } + } + + if (liveItems.length >= max) { + break; + } } - res.send(response) + // sort and put newly started items first + liveItems.sort((a, b) => { + return (b.startTime - a.startTime) + }); + + res.send(liveItems) }) app.use('/api/add/byfeedurl', async (req, res) => { diff --git a/ui/src/pages/landing.tsx b/ui/src/pages/landing.tsx index 743100a3..01e424d4 100644 --- a/ui/src/pages/landing.tsx +++ b/ui/src/pages/landing.tsx @@ -78,7 +78,7 @@ export default class Landing extends React.Component { async getEpisodes() { let liveResponse = await this.getLiveEpisodes() - let episodes = liveResponse.items.map(ep => { + let episodes = liveResponse.map(ep => { ep.live = true return ep })