2020-09-23 12:05:02 +00:00
|
|
|
function timeToPastText(timestamp) {
|
|
|
|
const difference = Date.now() - timestamp
|
|
|
|
return [
|
|
|
|
["year", 365 * 24 * 60 * 60 * 1000],
|
|
|
|
["month", 30 * 24 * 60 * 60 * 1000],
|
|
|
|
["week", 7 * 24 * 60 * 60 * 1000],
|
|
|
|
["day", 24 * 60 * 60 * 1000],
|
|
|
|
["hour", 60 * 60 * 1000],
|
|
|
|
["minute", 60 * 1000],
|
|
|
|
["second", 1 * 1000]
|
2021-01-13 11:55:03 +00:00
|
|
|
].reduce((acc, /** @type {[string, number]} */ [unitName, unitValue]) => {
|
2020-09-23 12:05:02 +00:00
|
|
|
if (acc) return acc
|
|
|
|
if (difference > unitValue) {
|
|
|
|
const number = Math.floor(difference / unitValue)
|
|
|
|
const pluralUnit = unitName + (number == 1 ? "" : "s")
|
|
|
|
return `${number} ${pluralUnit} ago`
|
|
|
|
}
|
|
|
|
}, null) || "just now"
|
|
|
|
}
|
|
|
|
|
2020-10-06 10:43:44 +00:00
|
|
|
function lengthSecondsToLengthText(seconds) {
|
2021-01-13 11:55:03 +00:00
|
|
|
let parts = [Math.floor(seconds/3600), Math.floor(seconds/60)%60, seconds%60]
|
|
|
|
if (parts[0] === 0) parts = parts.slice(1)
|
|
|
|
return parts.map((x, i) => i === 0 ? x : (x+"").padStart(2, "0")).join(":")
|
2020-10-06 10:43:44 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 01:53:30 +00:00
|
|
|
/**
|
|
|
|
* 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) {
|
2021-01-12 10:49:00 +00:00
|
|
|
video.second__lengthText = lengthSecondsToLengthText(video.lengthSeconds)
|
2021-01-12 01:53:30 +00:00
|
|
|
}
|
|
|
|
if (!video.second__lengthText && video.lengthSeconds === 0) {
|
|
|
|
video.second__lengthText = "LIVE"
|
|
|
|
video.liveNow = true
|
|
|
|
}
|
|
|
|
if (video.publishedText === "0 seconds ago") {
|
|
|
|
video.publishedText = "Live now"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-23 12:05:02 +00:00
|
|
|
module.exports.timeToPastText = timeToPastText
|
2020-10-06 10:43:44 +00:00
|
|
|
module.exports.lengthSecondsToLengthText = lengthSecondsToLengthText
|
2021-01-12 01:53:30 +00:00
|
|
|
module.exports.normaliseVideoInfo = normaliseVideoInfo
|