Set user-agent for invidious.snopyta ID

This commit is contained in:
Cadence Ember 2021-01-12 23:49:00 +13:00
parent c419aa90da
commit 98ee77def4
No known key found for this signature in database
GPG Key ID: BC1C2C61CF521B17
5 changed files with 21 additions and 6 deletions

View File

@ -1,4 +1,4 @@
const fetch = require("node-fetch") const {request} = require("../utils/request")
const {render} = require("pinski/plugins") const {render} = require("pinski/plugins")
const {getUser} = require("../utils/getuser") const {getUser} = require("../utils/getuser")
const converters = require("../utils/converters") const converters = require("../utils/converters")
@ -10,7 +10,10 @@ module.exports = [
const instanceOrigin = getUser(req).getSettingsOrDefaults().instance const instanceOrigin = getUser(req).getSettingsOrDefaults().instance
const fetchURL = new URL(`${instanceOrigin}/api/v1/search`) const fetchURL = new URL(`${instanceOrigin}/api/v1/search`)
fetchURL.searchParams.set("q", query) fetchURL.searchParams.set("q", query)
const results = await fetch(fetchURL.toString()).then(res => res.json()) const results = await request(fetchURL.toString()).then(res => res.json())
const error = results.error || results.message || results.code
if (error) throw new Error(`Instance said: ${error}`)
for (const video of results) { for (const video of results) {
converters.normaliseVideoInfo(video) converters.normaliseVideoInfo(video)

View File

@ -1,3 +1,4 @@
const {request} = require("../utils/request")
const fetch = require("node-fetch") const fetch = require("node-fetch")
const {render} = require("pinski/plugins") const {render} = require("pinski/plugins")
const db = require("../utils/db") const db = require("../utils/db")
@ -127,7 +128,7 @@ module.exports = [
if (!settings.local) { if (!settings.local) {
const instanceOrigin = settings.instance const instanceOrigin = settings.instance
const outURL = `${instanceOrigin}/api/v1/videos/${id}` const outURL = `${instanceOrigin}/api/v1/videos/${id}`
const videoPromise = fetch(outURL).then(res => res.json()) const videoPromise = request(outURL).then(res => res.json())
return renderVideo(videoPromise, {user, id, instanceOrigin}) return renderVideo(videoPromise, {user, id, instanceOrigin})
} else { } else {
return render(200, "pug/local-video.pug", {id}) return render(200, "pug/local-video.pug", {id})

View File

@ -39,7 +39,7 @@ function lengthSecondsToLengthText(seconds) {
*/ */
function normaliseVideoInfo(video) { function normaliseVideoInfo(video) {
if (!video.second__lengthText && video.lengthSeconds > 0) { if (!video.second__lengthText && video.lengthSeconds > 0) {
video.second__lengthText = converters.lengthSecondsToLengthText(video.lengthSeconds) video.second__lengthText = lengthSecondsToLengthText(video.lengthSeconds)
} }
if (!video.second__lengthText && video.lengthSeconds === 0) { if (!video.second__lengthText && video.lengthSeconds === 0) {
video.second__lengthText = "LIVE" video.second__lengthText = "LIVE"

11
utils/request.js Normal file
View File

@ -0,0 +1,11 @@
const fetch = require("node-fetch")
function request(url, options = {}) {
if (!options.headers) options.headers = {}
options.headers = {
"user-agent": "CloudTubeBackend/1.0"
}
return fetch(url, options)
}
module.exports.request = request

View File

@ -1,10 +1,10 @@
const fetch = require("node-fetch") const {request} = require("./request")
const db = require("./db") const db = require("./db")
async function fetchChannel(ucid, instance) { async function fetchChannel(ucid, instance) {
if (!instance) throw new Error("No instance parameter provided") if (!instance) throw new Error("No instance parameter provided")
// fetch // fetch
const channel = await fetch(`${instance}/api/v1/channels/${ucid}`).then(res => res.json()) const channel = await request(`${instance}/api/v1/channels/${ucid}`).then(res => res.json())
// update database // update database
const bestIcon = channel.authorThumbnails.slice(-1)[0] const bestIcon = channel.authorThumbnails.slice(-1)[0]
const iconURL = bestIcon ? bestIcon.url : null const iconURL = bestIcon ? bestIcon.url : null