2021-01-12 10:49:00 +00:00
|
|
|
const {request} = require("../utils/request")
|
2021-05-11 09:27:57 +00:00
|
|
|
/** @type {import("node-fetch").default} */
|
|
|
|
// @ts-ignore
|
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
|
|
|
|
2021-04-27 12:56:00 +00:00
|
|
|
class MessageError extends Error {
|
|
|
|
}
|
|
|
|
|
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) {
|
2021-11-05 03:13:20 +00:00
|
|
|
// Add second__ extensions to format objects, required if Invidious was the extractor
|
|
|
|
let formats = video.formatStreams.concat(video.adaptiveFormats)
|
2021-04-04 04:51:39 +00:00
|
|
|
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}`
|
|
|
|
}
|
2021-11-05 03:13:20 +00:00
|
|
|
|
|
|
|
// Properly build and order format list
|
|
|
|
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)
|
2021-04-04 04:51:39 +00:00
|
|
|
for (const format of adaptive) {
|
2021-11-05 03:13:20 +00:00
|
|
|
if (!format.cloudtube__label.endsWith("*")) format.cloudtube__label += " *"
|
2021-04-04 04:51:39 +00:00
|
|
|
}
|
2021-11-05 03:13:20 +00:00
|
|
|
formats = standard.concat(adaptive)
|
2021-04-04 04:51:39 +00:00
|
|
|
|
2021-11-05 03:13:20 +00:00
|
|
|
// Reorder fomats based on user preference
|
2021-04-04 04:51:39 +00:00
|
|
|
if (preference === 1) { // best dash
|
2021-04-25 10:13:04 +00:00
|
|
|
formats.sort((a, b) => {
|
|
|
|
const a1 = a.second__height + a.fps / 100
|
|
|
|
const b1 = b.second__height + b.fps / 100
|
|
|
|
return b1 - a1
|
|
|
|
})
|
2021-04-04 04:51:39 +00:00
|
|
|
} else if (preference === 2) { // best <=1080p
|
|
|
|
formats.sort((a, b) => {
|
2021-04-25 10:13:04 +00:00
|
|
|
const a1 = a.second__height + a.fps / 100
|
|
|
|
const b1 = b.second__height + b.fps / 100
|
|
|
|
if (b1 > 1081) {
|
|
|
|
if (a1 > 1081) return b1 - a1
|
2021-04-04 04:51:39 +00:00
|
|
|
return -1
|
|
|
|
}
|
2021-04-25 10:13:04 +00:00
|
|
|
if (a1 > 1081) return 1
|
|
|
|
return b1 - a1
|
2021-04-04 04:51:39 +00:00
|
|
|
})
|
|
|
|
} 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
|
|
|
|
}
|
|
|
|
|
2020-10-26 07:29:05 +00:00
|
|
|
module.exports = [
|
|
|
|
{
|
|
|
|
route: "/watch", methods: ["GET", "POST"], upload: true, code: async ({req, url, body}) => {
|
2021-04-27 12:56:00 +00:00
|
|
|
// Prepare data needed to render video page
|
|
|
|
|
2020-10-26 07:29:05 +00:00
|
|
|
const user = getUser(req)
|
|
|
|
const settings = user.getSettingsOrDefaults()
|
2021-01-13 11:55:31 +00:00
|
|
|
const id = url.searchParams.get("v")
|
2021-04-25 12:02:59 +00:00
|
|
|
|
2024-07-25 08:21:15 +00:00
|
|
|
// Check if should watch on YouTube
|
|
|
|
if (settings.local === 2) {
|
|
|
|
const dest = `https://www.youtube.com${url.pathname}${url.search}#cloudtube`
|
|
|
|
user.addWatchedVideoMaybe(id)
|
|
|
|
return {
|
|
|
|
statusCode: 302,
|
|
|
|
contentType: "text/plain",
|
|
|
|
headers: {
|
|
|
|
"Location": dest
|
|
|
|
},
|
|
|
|
content: `Redirecting to ${dest}...`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 06:42:34 +00:00
|
|
|
// Check if playback is allowed
|
|
|
|
const videoTakedownInfo = db.prepare("SELECT id, org, url FROM TakedownVideos WHERE id = ?").get(id)
|
|
|
|
if (videoTakedownInfo) {
|
2023-01-15 09:06:31 +00:00
|
|
|
return render(451, "pug/takedown-video.pug", Object.assign({req, settings}, videoTakedownInfo))
|
2021-11-20 06:42:34 +00:00
|
|
|
}
|
|
|
|
|
2021-04-25 12:02:59 +00:00
|
|
|
// Media fragment
|
2021-02-03 08:04:48 +00:00
|
|
|
const t = url.searchParams.get("t")
|
|
|
|
let mediaFragment = converters.tToMediaFragment(t)
|
2021-04-25 12:02:59 +00:00
|
|
|
|
|
|
|
// Continuous mode
|
|
|
|
const continuous = url.searchParams.get("continuous") === "1"
|
2021-04-26 06:42:42 +00:00
|
|
|
const autoplay = url.searchParams.get("autoplay") === "1"
|
2021-04-25 12:02:59 +00:00
|
|
|
const swp = url.searchParams.get("session-watched")
|
|
|
|
const sessionWatched = swp ? swp.split(" ") : []
|
|
|
|
const sessionWatchedNext = sessionWatched.concat([id]).join("+")
|
|
|
|
if (continuous) settings.quality = 0 // autoplay with synced streams does not work
|
|
|
|
|
2021-04-27 12:56:00 +00:00
|
|
|
// Work out how to fetch the video
|
2020-10-26 07:29:05 +00:00
|
|
|
if (req.method === "GET") {
|
2024-07-25 08:21:15 +00:00
|
|
|
if (settings.local === 1) { // skip to the local fetching page, which will then POST video data in a moment
|
2023-01-15 09:06:31 +00:00
|
|
|
return render(200, "pug/local-video.pug", {req, settings, id})
|
2020-08-31 13:22:16 +00:00
|
|
|
}
|
2021-04-25 12:02:59 +00:00
|
|
|
var instanceOrigin = settings.instance
|
|
|
|
var outURL = `${instanceOrigin}/api/v1/videos/${id}`
|
2021-04-27 12:56:00 +00:00
|
|
|
var videoFuture = request(outURL).then(res => res.json())
|
2020-10-26 07:29:05 +00:00
|
|
|
} else { // req.method === "POST"
|
2021-04-25 12:02:59 +00:00
|
|
|
var instanceOrigin = "http://localhost:3000"
|
2021-04-27 12:56:00 +00:00
|
|
|
var videoFuture = JSON.parse(new URLSearchParams(body.toString()).get("video"))
|
2020-08-30 13:54:59 +00:00
|
|
|
}
|
2021-04-25 12:02:59 +00:00
|
|
|
|
2021-04-27 12:56:00 +00:00
|
|
|
try {
|
|
|
|
// Fetch the video
|
|
|
|
const video = await videoFuture
|
|
|
|
|
|
|
|
// Error handling
|
|
|
|
if (!video) throw new MessageError("The instance returned null.")
|
|
|
|
if (video.error) throw new InstanceError(video.error, video.identifier)
|
|
|
|
|
2021-11-20 06:42:34 +00:00
|
|
|
// Check if channel playback is allowed
|
|
|
|
const channelTakedownInfo = db.prepare("SELECT ucid, org, url FROM TakedownChannels WHERE ucid = ?").get(video.authorId)
|
|
|
|
if (channelTakedownInfo) {
|
|
|
|
// automatically add the entry to the videos list, so it won't be fetched again
|
|
|
|
const args = {id, ...channelTakedownInfo}
|
|
|
|
db.prepare("INSERT INTO TakedownVideos (id, org, url) VALUES (@id, @org, @url)").run(args)
|
2023-01-15 09:06:31 +00:00
|
|
|
return render(451, "pug/takedown-video.pug", Object.assign({req, settings}, channelTakedownInfo))
|
2021-11-20 06:42:34 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 12:56:00 +00:00
|
|
|
// process stream list ordering
|
|
|
|
const formats = sortFormats(video, settings.quality)
|
|
|
|
|
|
|
|
// process length text and view count
|
|
|
|
for (const rec of video.recommendedVideos) {
|
|
|
|
converters.normaliseVideoInfo(rec)
|
|
|
|
}
|
|
|
|
|
2021-05-11 12:29:44 +00:00
|
|
|
// filter list
|
|
|
|
const {videos, filteredCount} = converters.applyVideoFilters(video.recommendedVideos, user.getFilters())
|
|
|
|
video.recommendedVideos = videos
|
|
|
|
|
2021-04-27 12:56:00 +00:00
|
|
|
// get subscription data
|
|
|
|
const subscribed = user.isSubscribed(video.authorId)
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// normalise view count
|
|
|
|
if (!video.second__viewCountText && video.viewCount) {
|
|
|
|
video.second__viewCountText = converters.viewCountToText(video.viewCount)
|
|
|
|
}
|
|
|
|
|
2021-05-01 12:59:28 +00:00
|
|
|
// apply media fragment to all sources
|
|
|
|
for (const format of formats) {
|
|
|
|
format.url += mediaFragment
|
|
|
|
}
|
|
|
|
|
2021-04-27 12:56:00 +00:00
|
|
|
// rewrite description
|
|
|
|
video.descriptionHtml = converters.rewriteVideoDescription(video.descriptionHtml, id)
|
|
|
|
|
2021-05-22 00:32:43 +00:00
|
|
|
// rewrite captions urls so they are served on the same domain via the /proxy route
|
|
|
|
for (const caption of video.captions) {
|
|
|
|
caption.url = `/proxy?${new URLSearchParams({"url": caption.url})}`
|
|
|
|
}
|
|
|
|
|
2021-04-27 12:56:00 +00:00
|
|
|
return render(200, "pug/video.pug", {
|
2023-01-15 09:06:31 +00:00
|
|
|
req, url, video, formats, subscribed, instanceOrigin, mediaFragment, autoplay, continuous,
|
2021-08-16 10:37:12 +00:00
|
|
|
sessionWatched, sessionWatchedNext, settings
|
2021-04-27 12:56:00 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
// Something went wrong, somewhere! Find out where.
|
|
|
|
|
|
|
|
let errorType = "unrecognised-error"
|
|
|
|
const locals = {instanceOrigin, error}
|
|
|
|
|
|
|
|
// Sort error category
|
|
|
|
if (error instanceof fetch.FetchError) {
|
|
|
|
errorType = "fetch-error"
|
|
|
|
} else if (error instanceof MessageError) {
|
|
|
|
errorType = "message-error"
|
|
|
|
} else if (error instanceof InstanceError) {
|
|
|
|
if (error.identifier === "RATE_LIMITED_BY_YOUTUBE" || error.message === "Could not extract video info. Instance is likely blocked.") {
|
|
|
|
errorType = "rate-limited"
|
|
|
|
} else {
|
|
|
|
errorType = "instance-error"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create appropriate formatted message
|
|
|
|
const message = render(0, `pug/errors/${errorType}.pug`, locals).content
|
|
|
|
|
2023-01-15 09:06:31 +00:00
|
|
|
return render(500, "pug/video.pug", {video: {videoId: id}, error: true, message, req, settings})
|
2021-04-27 12:56:00 +00:00
|
|
|
}
|
2020-08-30 13:54:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|