2021-01-12 10:49:00 +00:00
|
|
|
const {request} = require("../utils/request")
|
2020-08-30 13:54:59 +00:00
|
|
|
const fetch = require("node-fetch")
|
|
|
|
const {render} = require("pinski/plugins")
|
2020-09-23 12:05:02 +00:00
|
|
|
const db = require("../utils/db")
|
|
|
|
const {getToken, getUser} = require("../utils/getuser")
|
2020-08-31 13:22:16 +00:00
|
|
|
const pug = require("pug")
|
2020-10-06 10:43:44 +00:00
|
|
|
const converters = require("../utils/converters")
|
2021-02-20 02:39:27 +00:00
|
|
|
const constants = require("../utils/constants")
|
2020-08-31 13:22:16 +00:00
|
|
|
|
|
|
|
class InstanceError extends Error {
|
|
|
|
constructor(error, identifier) {
|
|
|
|
super(error)
|
|
|
|
this.identifier = identifier
|
|
|
|
}
|
|
|
|
}
|
2020-08-30 13:54:59 +00:00
|
|
|
|
2020-10-06 09:29:45 +00:00
|
|
|
function formatOrder(format) {
|
|
|
|
// most significant to least significant
|
|
|
|
// key, max, order, transform
|
|
|
|
// asc: lower number comes first, desc: higher number comes first
|
|
|
|
const spec = [
|
2021-01-13 11:55:03 +00:00
|
|
|
{key: "second__height", max: 8000, order: "desc", transform: x => x ? Math.floor(x/96) : 0},
|
|
|
|
{key: "fps", max: 100, order: "desc", transform: x => x ? Math.floor(x/10) : 0},
|
|
|
|
{key: "type", max: " ".repeat(60), order: "asc", transform: x => x.length}
|
2020-10-06 09:29:45 +00:00
|
|
|
]
|
|
|
|
let total = 0
|
|
|
|
for (let i = 0; i < spec.length; i++) {
|
|
|
|
const s = spec[i]
|
2021-01-13 11:55:03 +00:00
|
|
|
let diff = s.transform(format[s.key])
|
|
|
|
if (s.order === "asc") diff = s.transform(s.max) - diff
|
2020-10-06 09:29:45 +00:00
|
|
|
total += diff
|
2021-01-13 11:55:03 +00:00
|
|
|
if (i+1 < spec.length) { // not the last spec item?
|
|
|
|
const s2 = spec[i+1]
|
2021-01-14 09:48:25 +00:00
|
|
|
total *= s2.transform(s2.max)
|
2020-10-06 09:29:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -total
|
|
|
|
}
|
|
|
|
|
2021-04-04 04:51:39 +00:00
|
|
|
function sortFormats(video, preference) {
|
|
|
|
const standard = video.formatStreams.slice().sort((a, b) => b.second__height - a.second__height)
|
|
|
|
const adaptive = video.adaptiveFormats.filter(f => f.type.startsWith("video") && f.qualityLabel).sort((a, b) => a.second__order - b.second__order)
|
|
|
|
let formats = standard.concat(adaptive)
|
|
|
|
|
|
|
|
for (const format of formats) {
|
|
|
|
if (!format.second__height && format.resolution) format.second__height = +format.resolution.slice(0, -1)
|
|
|
|
if (!format.second__order) format.second__order = formatOrder(format)
|
|
|
|
format.cloudtube__label = `${format.qualityLabel} ${format.container}`
|
|
|
|
}
|
|
|
|
for (const format of adaptive) {
|
|
|
|
format.cloudtube__label += " *"
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preference === 1) { // best dash
|
|
|
|
formats.sort((a, b) => (b.second__height - a.second__height))
|
|
|
|
} else if (preference === 2) { // best <=1080p
|
|
|
|
formats.sort((a, b) => {
|
|
|
|
if (b.second__height > 1080) {
|
|
|
|
if (a.second__height > 1080) return b.second__height - a.second__height
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if (a.second__height > 1080) return 1
|
|
|
|
return b.second__height - a.second__height
|
|
|
|
})
|
|
|
|
} else if (preference === 3) { // best low-fps
|
|
|
|
formats.sort((a, b) => {
|
|
|
|
if (b.fps > 30) {
|
|
|
|
if (a.fps < 30) return b.second__height - a.second__height
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if (a.fps > 30) return 1
|
|
|
|
return b.second__height - a.second__height
|
|
|
|
})
|
|
|
|
} else if (preference === 4) { // 360p only
|
|
|
|
formats.sort((a, b) => {
|
|
|
|
if (a.itag == 18) return -1
|
|
|
|
if (b.itag == 18) return 1
|
|
|
|
return 0
|
|
|
|
})
|
|
|
|
} else { // preference === 0, best combined
|
|
|
|
// should already be correct
|
|
|
|
}
|
|
|
|
|
|
|
|
return formats
|
|
|
|
}
|
|
|
|
|
2021-02-20 02:39:27 +00:00
|
|
|
function rewriteVideoDescription(descriptionHtml, id) {
|
|
|
|
// replace timestamps to clickable links and rewrite youtube links to stay on the instance instead of pointing to YouTube
|
|
|
|
// test cases
|
|
|
|
// https://www.youtube.com/watch?v=VdPsJW6AHqc 00:00 timestamps, youtu.be/<videoid>
|
|
|
|
// https://www.youtube.com/watch?v=FDMq9ie0ih0 00:00 & 00:00:00 timestamps
|
|
|
|
// https://www.youtube.com/watch?v=fhum63fAwrI www.youtube.com/watch?v=<videoid>
|
|
|
|
// https://www.youtube.com/watch?v=i-szWOrc3Mo www.youtube.com/<channelname> (unsupported by cloudtube currently)
|
|
|
|
// https://www.youtube.com/watch?v=LSG71wbKpbQ www.youtube.com/channel/<id>
|
2021-02-24 11:49:26 +00:00
|
|
|
|
|
|
|
descriptionHtml = descriptionHtml.replace(new RegExp(`<a href="https?://(?:www\\.)?youtu\\.be/(${constants.regex.video_id})([^"]*)">([^<]+)</a>`, "g"), `<a href="/watch?v=$1$2">$3</a>`)
|
|
|
|
descriptionHtml = descriptionHtml.replace(new RegExp(`<a href="https?://(?:www\\.)?youtu(?:\\.be|be\\.com)/([^"]*)">([^<]+)<\/a>`, "g"), `<a href="/$1">$2</a>`)
|
|
|
|
descriptionHtml = descriptionHtml.replace(new RegExp(`(?:([0-9]*):)?([0-5]?[0-9]):([0-5][0-9])`, "g"), (_, hours, minutes, seconds) => {
|
|
|
|
let timeURL, timeDisplay, timeSeconds
|
|
|
|
if (hours === undefined) {
|
|
|
|
timeURL = `${minutes}m${seconds}s`
|
|
|
|
timeDisplay = `${minutes}:${seconds}`
|
|
|
|
timeSeconds = minutes*60 + + seconds
|
|
|
|
} else {
|
|
|
|
timeURL = `${hours}h${minutes}m${seconds}s`
|
|
|
|
timeDisplay = `${hours}:${minutes}:${seconds}`
|
|
|
|
timeSeconds = hours*60*60 + minutes*60 + + seconds
|
2021-02-20 02:39:27 +00:00
|
|
|
}
|
2021-02-24 11:49:26 +00:00
|
|
|
|
|
|
|
const params = new URLSearchParams()
|
|
|
|
params.set("v", id)
|
|
|
|
params.set("t", timeURL)
|
|
|
|
const url = "/watch?" + params
|
|
|
|
|
|
|
|
return pug.render(`a(href=url data-clickable-timestamp=timeSeconds)= timeDisplay`, {url, timeURL, timeDisplay, timeSeconds})
|
2021-02-20 02:39:27 +00:00
|
|
|
})
|
2021-02-24 11:49:26 +00:00
|
|
|
|
2021-02-20 02:39:27 +00:00
|
|
|
return descriptionHtml
|
|
|
|
}
|
|
|
|
|
2021-04-04 04:51:39 +00:00
|
|
|
async function renderVideo(videoPromise, {user, settings, id, instanceOrigin}, locals = {}) {
|
2020-10-26 07:29:05 +00:00
|
|
|
try {
|
2020-12-28 12:42:25 +00:00
|
|
|
// resolve video
|
2020-10-26 07:29:05 +00:00
|
|
|
const video = await videoPromise
|
|
|
|
if (!video) throw new Error("The instance returned null.")
|
|
|
|
if (video.error) throw new InstanceError(video.error, video.identifier)
|
2021-04-04 04:51:39 +00:00
|
|
|
|
2020-12-28 12:42:25 +00:00
|
|
|
// process stream list ordering
|
2021-04-04 04:51:39 +00:00
|
|
|
const formats = sortFormats(video, settings.quality)
|
|
|
|
|
2021-02-10 11:14:37 +00:00
|
|
|
// process length text and view count
|
2020-10-26 07:29:05 +00:00
|
|
|
for (const rec of video.recommendedVideos) {
|
2021-02-10 11:14:37 +00:00
|
|
|
converters.normaliseVideoInfo(rec)
|
2020-10-26 07:29:05 +00:00
|
|
|
}
|
2021-04-04 04:51:39 +00:00
|
|
|
|
2020-12-28 12:42:25 +00:00
|
|
|
// get subscription data
|
2020-10-26 07:29:05 +00:00
|
|
|
const subscribed = user.isSubscribed(video.authorId)
|
2021-04-04 04:51:39 +00:00
|
|
|
|
2020-12-28 12:42:25 +00:00
|
|
|
// process watched videos
|
|
|
|
user.addWatchedVideoMaybe(video.videoId)
|
|
|
|
const watchedVideos = user.getWatchedVideos()
|
|
|
|
if (watchedVideos.length) {
|
|
|
|
for (const rec of video.recommendedVideos) {
|
|
|
|
rec.watched = watchedVideos.includes(rec.videoId)
|
|
|
|
}
|
|
|
|
}
|
2021-04-04 04:51:39 +00:00
|
|
|
|
2021-02-10 11:14:37 +00:00
|
|
|
// normalise view count
|
|
|
|
if (!video.second__viewCountText && video.viewCount) {
|
|
|
|
video.second__viewCountText = converters.viewCountToText(video.viewCount)
|
|
|
|
}
|
2021-04-04 04:51:39 +00:00
|
|
|
|
|
|
|
// rewrite description
|
2021-02-20 02:39:27 +00:00
|
|
|
video.descriptionHtml = rewriteVideoDescription(video.descriptionHtml, id)
|
2021-04-04 04:51:39 +00:00
|
|
|
|
|
|
|
return render(200, "pug/video.pug", Object.assign(locals, {video, formats, subscribed, instanceOrigin}))
|
2020-10-26 07:29:05 +00:00
|
|
|
} catch (e) {
|
2020-12-28 12:42:25 +00:00
|
|
|
// show an appropriate error message
|
|
|
|
// these should probably be split out to their own files
|
2020-10-26 07:29:05 +00:00
|
|
|
let message = pug.render("pre= error", {error: e.stack || e.toString()})
|
|
|
|
if (e instanceof fetch.FetchError) {
|
|
|
|
const template = `
|
2020-10-18 08:51:09 +00:00
|
|
|
p The selected instance, #[code= instanceOrigin], did not respond correctly.
|
2020-08-31 13:22:16 +00:00
|
|
|
`
|
2021-01-13 11:55:03 +00:00
|
|
|
message = pug.render(template, {instanceOrigin})
|
2020-10-26 07:29:05 +00:00
|
|
|
} else if (e instanceof InstanceError) {
|
2021-01-14 09:48:45 +00:00
|
|
|
if (e.identifier === "RATE_LIMITED_BY_YOUTUBE" || e.message === "Could not extract video info. Instance is likely blocked.") {
|
2020-10-26 07:29:05 +00:00
|
|
|
const template = `
|
2020-10-18 09:44:50 +00:00
|
|
|
.blocked-explanation
|
|
|
|
img(src="/static/images/instance-blocked.svg" width=552 height=96)
|
|
|
|
.rows
|
|
|
|
.row
|
|
|
|
h3.actor You
|
|
|
|
| Working
|
|
|
|
.row
|
|
|
|
h3.actor CloudTube
|
|
|
|
| Working
|
|
|
|
.row
|
|
|
|
h3.actor Instance
|
|
|
|
| Blocked by YouTube
|
|
|
|
.row
|
|
|
|
h3.actor YouTube
|
|
|
|
| Working
|
|
|
|
p.
|
2021-02-27 00:11:27 +00:00
|
|
|
CloudTube needs a working NewLeaf/Invidious instance in order to get data about videos.
|
2020-10-18 09:44:50 +00:00
|
|
|
However, the selected instance, #[code= instanceOrigin], has been temporarily blocked by YouTube.
|
|
|
|
p.
|
|
|
|
You will be able to watch this video if you select a working instance in settings.
|
|
|
|
#[br]#[a(href="/settings") Go to settings →]
|
|
|
|
p.
|
|
|
|
(Tip: Try #[code https://invidious.snopyta.org] or #[code https://invidious.site].)
|
|
|
|
p.
|
|
|
|
This situation #[em will] be improved in the future!
|
|
|
|
`
|
2020-10-26 07:29:05 +00:00
|
|
|
message = pug.render(template, {instanceOrigin})
|
|
|
|
} else {
|
|
|
|
const template = `
|
2020-08-31 13:22:16 +00:00
|
|
|
p #[strong= error.message]
|
|
|
|
if error.identifier
|
|
|
|
p #[code= error.identifier]
|
2020-10-18 08:51:09 +00:00
|
|
|
p That error was generated by #[code= instanceOrigin].
|
2020-08-31 13:22:16 +00:00
|
|
|
`
|
2020-10-26 07:29:05 +00:00
|
|
|
message = pug.render(template, {instanceOrigin, error: e})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return render(500, "pug/video.pug", {video: {videoId: id}, error: true, message})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = [
|
|
|
|
{
|
|
|
|
route: "/watch", methods: ["GET", "POST"], upload: true, code: async ({req, url, body}) => {
|
|
|
|
const user = getUser(req)
|
|
|
|
const settings = user.getSettingsOrDefaults()
|
2021-01-13 11:55:31 +00:00
|
|
|
const id = url.searchParams.get("v")
|
2021-02-03 08:04:48 +00:00
|
|
|
const t = url.searchParams.get("t")
|
|
|
|
let mediaFragment = converters.tToMediaFragment(t)
|
2020-10-26 07:29:05 +00:00
|
|
|
if (req.method === "GET") {
|
|
|
|
if (!settings.local) {
|
|
|
|
const instanceOrigin = settings.instance
|
|
|
|
const outURL = `${instanceOrigin}/api/v1/videos/${id}`
|
2021-01-12 10:49:00 +00:00
|
|
|
const videoPromise = request(outURL).then(res => res.json())
|
2021-04-04 04:51:39 +00:00
|
|
|
return renderVideo(videoPromise, {user, settings, id, instanceOrigin}, {mediaFragment})
|
2020-10-26 07:29:05 +00:00
|
|
|
} else {
|
|
|
|
return render(200, "pug/local-video.pug", {id})
|
2020-08-31 13:22:16 +00:00
|
|
|
}
|
2020-10-26 07:29:05 +00:00
|
|
|
} else { // req.method === "POST"
|
|
|
|
const video = JSON.parse(new URLSearchParams(body.toString()).get("video"))
|
|
|
|
const videoPromise = Promise.resolve(video)
|
2021-01-13 11:55:31 +00:00
|
|
|
const instanceOrigin = "http://localhost:3000"
|
2021-04-04 04:51:39 +00:00
|
|
|
return renderVideo(videoPromise, {user, settings, id, instanceOrigin}, {mediaFragment})
|
2020-08-30 13:54:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|