From c419aa90da6cad733dd72834e0c2f66859c89b72 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Tue, 12 Jan 2021 14:53:30 +1300 Subject: [PATCH] Normalise info for both channels and search --- api/channels.js | 4 +++- api/search.js | 11 +---------- utils/converters.js | 24 ++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/api/channels.js b/api/channels.js index 444076f..b340bbd 100644 --- a/api/channels.js +++ b/api/channels.js @@ -2,6 +2,7 @@ const {render} = require("pinski/plugins") const constants = require("../utils/constants") const {fetchChannel} = require("../utils/youtube") const {getUser} = require("../utils/getuser") +const converters = require("../utils/converters") module.exports = [ { @@ -12,10 +13,11 @@ module.exports = [ const data = await fetchChannel(id, settings.instance) const subscribed = user.isSubscribed(id) const instanceOrigin = settings.instance - // apply watched status + // normalise info, apply watched status const watchedVideos = user.getWatchedVideos() if (data.latestVideos) { data.latestVideos.forEach(video => { + converters.normaliseVideoInfo(video) video.watched = watchedVideos.includes(video.videoId) }) } diff --git a/api/search.js b/api/search.js index 1c44155..0fd668b 100644 --- a/api/search.js +++ b/api/search.js @@ -13,16 +13,7 @@ module.exports = [ const results = await fetch(fetchURL.toString()).then(res => res.json()) for (const video of results) { - if (!video.second__lengthText && video.lengthSeconds > 0) { - video.second__lengthText = converters.lengthSecondsToLengthText(video.lengthSeconds) - } - if (!video.second__lengthText && video.lengthSeconds === 0) { - video.second__lengthText = "LIVE" - video.liveNow = true - } - if (video.publishedText === "0 seconds ago") { - video.publishedText = "Live now" - } + converters.normaliseVideoInfo(video) } return render(200, "pug/search.pug", {query, results, instanceOrigin}) diff --git a/utils/converters.js b/utils/converters.js index e875a44..1beb969 100644 --- a/utils/converters.js +++ b/utils/converters.js @@ -27,5 +27,29 @@ function lengthSecondsToLengthText(seconds) { .join(":") } +/** + * Second and Invidious don't return quite the same data. This + * function normalises them so that all the useful properties are + * available no matter the kind of instance. The video is modified + * in-place. + * + * Changes: + * - second__lengthText is added, may be [hh:]mm:ss or "LIVE" + * - publishedText may be changed to "Live now" + */ +function normaliseVideoInfo(video) { + if (!video.second__lengthText && video.lengthSeconds > 0) { + video.second__lengthText = converters.lengthSecondsToLengthText(video.lengthSeconds) + } + if (!video.second__lengthText && video.lengthSeconds === 0) { + video.second__lengthText = "LIVE" + video.liveNow = true + } + if (video.publishedText === "0 seconds ago") { + video.publishedText = "Live now" + } +} + module.exports.timeToPastText = timeToPastText module.exports.lengthSecondsToLengthText = lengthSecondsToLengthText +module.exports.normaliseVideoInfo = normaliseVideoInfo