Skip to content

Commit

Permalink
Check the episodes endpoint to see if updated live items are actually…
Browse files Browse the repository at this point in the history
… live
  • Loading branch information
ericpp committed Mar 10, 2023
1 parent d191649 commit a956dd3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
50 changes: 38 additions & 12 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion ui/src/pages/landing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default class Landing extends React.Component<IProps, IState> {

async getEpisodes() {
let liveResponse = await this.getLiveEpisodes()
let episodes = liveResponse.items.map(ep => {
let episodes = liveResponse.map(ep => {
ep.live = true
return ep
})
Expand Down

0 comments on commit a956dd3

Please sign in to comment.