1
0
mirror of https://git.sr.ht/~cadence/cloudtube synced 2024-09-19 18:57:30 +00:00
cloudtube/api/video.js

115 lines
3.7 KiB
JavaScript
Raw Normal View History

2020-08-30 13:54:59 +00:00
const fetch = require("node-fetch")
const {render} = require("pinski/plugins")
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")
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
function formatOrder(format) {
// most significant to least significant
// key, max, order, transform
// asc: lower number comes first, desc: higher number comes first
const spec = [
["second__height", 8000, "desc", x => x ? Math.floor(x/96) : 0],
["fps", 100, "desc", x => x ? Math.floor(x/10) : 0],
["type", " ".repeat(60), "asc", x => x.length],
]
let total = 0
for (let i = 0; i < spec.length; i++) {
const s = spec[i]
let diff = s[3](format[s[0]])
if (s[2] === "asc") diff = s[3](s[1]) - diff
total += diff
if (i+1 < spec.length) {
s2 = spec[i+1]
total *= s2[3](s2[1])
}
}
return -total
}
2020-08-30 13:54:59 +00:00
module.exports = [
{
route: "/watch", methods: ["GET"], code: async ({req, url}) => {
const id = url.searchParams.get("v")
2020-08-31 13:22:16 +00:00
const user = getUser(req)
const settings = user.getSettingsOrDefaults()
const instanceOrigin = settings.instance
const outURL = `${instanceOrigin}/api/v1/videos/${id}`
2020-08-31 13:22:16 +00:00
try {
const video = await fetch(outURL).then(res => res.json())
if (!video) throw new Error("The instance returned null.")
if (video.error) throw new InstanceError(video.error, video.identifier)
// video data additional processing
for (const format of video.formatStreams.concat(video.adaptiveFormats)) {
if (!format.second__height && format.resolution) format.second__height = +format.resolution.slice(0, -1)
if (!format.second__order) format.second__order = formatOrder(format)
}
2020-10-06 10:43:44 +00:00
for (const rec of video.recommendedVideos) {
if (!rec.second__lengthText && rec.lengthSeconds > 0) {
rec.second__lengthText = converters.lengthSecondsToLengthText(rec.lengthSeconds)
}
}
2020-08-31 13:22:16 +00:00
const subscribed = user.isSubscribed(video.authorId)
return render(200, "pug/video.pug", {video, subscribed, instanceOrigin})
2020-08-31 13:22:16 +00:00
} catch (e) {
let message = pug.render("pre= error", {error: e.stack || e.toString()})
if (e instanceof fetch.FetchError) {
const template = `
p The selected instance, #[code= instanceOrigin], did not respond correctly.
2020-08-31 13:22:16 +00:00
p Requested URL: #[a(href=url)= url]
`
message = pug.render(template, {instanceOrigin, url: outURL})
2020-08-31 13:22:16 +00:00
} else if (e instanceof InstanceError) {
2020-10-18 09:44:50 +00:00
if (e.identifier === "RATE_LIMITED_BY_YOUTUBE") {
const template = `
.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.
CloudTube needs to a working Second/Invidious instance in order to get data about videos.
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!
`
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]
p That error was generated by #[code= instanceOrigin].
2020-08-31 13:22:16 +00:00
`
2020-10-18 09:44:50 +00:00
message = pug.render(template, {instanceOrigin, error: e})
}
2020-08-31 13:22:16 +00:00
}
return render(500, "pug/video.pug", {video: {videoId: id}, error: true, message})
2020-08-30 13:54:59 +00:00
}
}
}
]