From 0f6266afa43096de3649b8a81a638b8c773acf07 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Tue, 24 Dec 2024 11:36:57 -0500 Subject: [PATCH] Remove youtube.js from web app --- CHANGELOG.md | 4 +- .../Middleware/ProxyRouter.bs | 41 ----- .../Web/PlayletWebServer/PlayletWebServer.bs | 2 - playlet-web/package-lock.json | 58 +------ playlet-web/package.json | 3 - playlet-web/src/lib/Api/PlayletApi.ts | 5 + playlet-web/src/lib/Api/YoutubeJs.ts | 156 ------------------ playlet-web/src/lib/LinkDragDrop.svelte | 4 +- .../lib/VideoFeed/PlaylistCastDialog.svelte | 2 +- .../lib/VideoFeed/PlaylistThumbnail.svelte | 2 +- 10 files changed, 15 insertions(+), 262 deletions(-) delete mode 100644 playlet-lib/src/components/Web/PlayletWebServer/Middleware/ProxyRouter.bs delete mode 100644 playlet-web/src/lib/Api/YoutubeJs.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 0054fd8d..ea59cf11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -- Nothing worth mentioning yet. +### Removed + +- [YouTube.js](https://github.com/LuanRT/YouTube.js) from web app. It is no longer needed, so this reduces app size. ## [0.35.0] - 2024-12-24 diff --git a/playlet-lib/src/components/Web/PlayletWebServer/Middleware/ProxyRouter.bs b/playlet-lib/src/components/Web/PlayletWebServer/Middleware/ProxyRouter.bs deleted file mode 100644 index 04943595..00000000 --- a/playlet-lib/src/components/Web/PlayletWebServer/Middleware/ProxyRouter.bs +++ /dev/null @@ -1,41 +0,0 @@ -import "pkg:/source/services/HttpClient.bs" - -namespace Http - - class ProxyRouter extends HttpRouter - - function new(server as object) - super() - - task = server.task - m.invidiousNode = task.invidious - m.invidiousService = new Invidious.InvidiousService(m.invidiousNode) - end function - - @post("/api/proxy") - function ProxyRequest(context as object) as boolean - request = context.request - response = context.response - - requestArgs = request.Json() - if requestArgs = invalid - response.Default(400, "Invalid request body") - return true - end if - - httpReq = HttpClient.FromObject(requestArgs) - httpRes = httpReq.Await() - - result = { - "status": httpRes.StatusCode() - "headers": httpRes.Headers() - "body": httpRes.Text() - } - - response.Json(result) - return true - end function - - end class - -end namespace diff --git a/playlet-lib/src/components/Web/PlayletWebServer/PlayletWebServer.bs b/playlet-lib/src/components/Web/PlayletWebServer/PlayletWebServer.bs index 06cdd720..af220c2a 100644 --- a/playlet-lib/src/components/Web/PlayletWebServer/PlayletWebServer.bs +++ b/playlet-lib/src/components/Web/PlayletWebServer/PlayletWebServer.bs @@ -11,7 +11,6 @@ import "pkg:/components/Web/PlayletWebServer/Middleware/PlayletInvidiousBackendR import "pkg:/components/Web/PlayletWebServer/Middleware/PlayletLibUrlsRouter.bs" import "pkg:/components/Web/PlayletWebServer/Middleware/PreferencesRouter.bs" import "pkg:/components/Web/PlayletWebServer/Middleware/ProfilesRouter.bs" -import "pkg:/components/Web/PlayletWebServer/Middleware/ProxyRouter.bs" import "pkg:/components/Web/PlayletWebServer/Middleware/RegistryRouter.bs" import "pkg:/components/Web/PlayletWebServer/Middleware/SearchHistoryRouter.bs" import "pkg:/components/Web/PlayletWebServer/Middleware/StateApiRouter.bs" @@ -56,7 +55,6 @@ function SetupRoutes(server as object) server.UseRouter(new Http.PlayletLibUrlsRouter(server)) server.UseRouter(new Http.DialRouter(server)) server.UseRouter(new Http.CacheRouter()) - server.UseRouter(new Http.ProxyRouter(server)) server.UseRouter(new Http.RegistryRouter(server)) server.UseRouter(new Http.PlayletInvidiousBackendRouter()) diff --git a/playlet-web/package-lock.json b/playlet-web/package-lock.json index 6b396da7..1295a086 100644 --- a/playlet-web/package-lock.json +++ b/playlet-web/package-lock.json @@ -7,9 +7,6 @@ "": { "name": "playlet-web", "version": "0.35.0", - "dependencies": { - "youtubei.js": "^12.2.0" - }, "devDependencies": { "@sveltejs/vite-plugin-svelte": "3.1.2", "@tailwindcss/typography": "^0.5.15", @@ -1494,11 +1491,6 @@ "node": ">=6.9.0" } }, - "node_modules/@bufbuild/protobuf": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/@bufbuild/protobuf/-/protobuf-2.1.0.tgz", - "integrity": "sha512-+2Mx67Y3skJ4NCD/qNSdBJNWtu6x6Qr53jeNg+QcwiL6mt0wK+3jwHH2x1p7xaYH6Ve2JKOVn0OxU35WsmqI9A==" - }, "node_modules/@esbuild/aix-ppc64": { "version": "0.21.5", "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.21.5.tgz", @@ -1867,14 +1859,6 @@ "node": ">=12" } }, - "node_modules/@fastify/busboy": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/@fastify/busboy/-/busboy-2.1.1.tgz", - "integrity": "sha512-vBZP4NlzfOlerQTnba4aqZoMhE/a9HY7HRqoOPaETQcSQuWEIyZMHGfVu6w9wGtGK5fED5qRs2DteVCjOH60sA==", - "engines": { - "node": ">=14" - } - }, "node_modules/@isaacs/cliui": { "version": "8.0.2", "resolved": "https://registry.npmjs.org/@isaacs/cliui/-/cliui-8.0.2.tgz", @@ -2318,6 +2302,7 @@ "version": "8.11.2", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.11.2.tgz", "integrity": "sha512-nc0Axzp/0FILLEVsm4fNwLCwMttvhEI263QtVPQcbpfZZ3ts0hLsZGOpE6czNlid7CJ9MlyH8reXkpsf3YUY4w==", + "dev": true, "bin": { "acorn": "bin/acorn" }, @@ -3229,18 +3214,6 @@ "@pkgjs/parseargs": "^0.11.0" } }, - "node_modules/jintr": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/jintr/-/jintr-3.2.0.tgz", - "integrity": "sha512-psD1yf05kMKDNsUdW1l5YhO59pHScQ6OIHHb8W5SKSM2dCOFPsqolmIuSHgVA8+3Dc47NJR181CXZ4alCAPTkA==", - "funding": [ - "https://github.com/sponsors/LuanRT" - ], - "license": "MIT", - "dependencies": { - "acorn": "^8.8.0" - } - }, "node_modules/jiti": { "version": "1.21.6", "resolved": "https://registry.npmjs.org/jiti/-/jiti-1.21.6.tgz", @@ -4516,7 +4489,8 @@ "node_modules/tslib": { "version": "2.8.1", "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz", - "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==" + "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==", + "dev": true }, "node_modules/typescript": { "version": "5.7.2", @@ -4531,17 +4505,6 @@ "node": ">=14.17" } }, - "node_modules/undici": { - "version": "5.28.4", - "resolved": "https://registry.npmjs.org/undici/-/undici-5.28.4.tgz", - "integrity": "sha512-72RFADWFqKmUb2hmmvNODKL3p9hcB6Gt2DOQMis1SEBaV6a4MH8soBvzg+95CYhCKPFedut2JY9bMfrDl9D23g==", - "dependencies": { - "@fastify/busboy": "^2.0.0" - }, - "engines": { - "node": ">=14.0" - } - }, "node_modules/unicode-canonical-property-names-ecmascript": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/unicode-canonical-property-names-ecmascript/-/unicode-canonical-property-names-ecmascript-2.0.1.tgz", @@ -4853,21 +4816,6 @@ "engines": { "node": ">= 14" } - }, - "node_modules/youtubei.js": { - "version": "12.2.0", - "resolved": "https://registry.npmjs.org/youtubei.js/-/youtubei.js-12.2.0.tgz", - "integrity": "sha512-G+50qrbJCToMYhu8jbaHiS3Vf+RRul+CcDbz3hEGwHkGPh+zLiWwD6SS+YhYF+2/op4ZU5zDYQJrGqJ+wKh7Gw==", - "funding": [ - "https://github.com/sponsors/LuanRT" - ], - "license": "MIT", - "dependencies": { - "@bufbuild/protobuf": "^2.0.0", - "jintr": "^3.2.0", - "tslib": "^2.5.0", - "undici": "^5.19.1" - } } } } diff --git a/playlet-web/package.json b/playlet-web/package.json index 99401966..7523b1a0 100644 --- a/playlet-web/package.json +++ b/playlet-web/package.json @@ -28,8 +28,5 @@ "vite": "^5.4.11", "vite-plugin-compression": "^0.5.1", "vite-tsconfig-paths": "^5.1.4" - }, - "dependencies": { - "youtubei.js": "^12.2.0" } } diff --git a/playlet-web/src/lib/Api/PlayletApi.ts b/playlet-web/src/lib/Api/PlayletApi.ts index db2fe06a..bdad4dc7 100644 --- a/playlet-web/src/lib/Api/PlayletApi.ts +++ b/playlet-web/src/lib/Api/PlayletApi.ts @@ -3,6 +3,11 @@ import { getHost } from "lib/Api/Host"; export class PlayletApi { static host = () => `http://${getHost()}` + static async getVideoInfo(videoId) { + const response = await fetch(`${PlayletApi.host()}/playlet-invidious-backend/api/v1/videos/${videoId}`); + return await response.json(); + } + static async getState() { const response = await fetch(`${PlayletApi.host()}/api/state`); return await response.json(); diff --git a/playlet-web/src/lib/Api/YoutubeJs.ts b/playlet-web/src/lib/Api/YoutubeJs.ts deleted file mode 100644 index c41a233a..00000000 --- a/playlet-web/src/lib/Api/YoutubeJs.ts +++ /dev/null @@ -1,156 +0,0 @@ -import { Innertube } from 'youtubei.js/web'; -import { getHost } from "lib/Api/Host"; - -export class YoutubeJs { - static host = () => `http://${getHost()}` - - static innerTube: Innertube; - static initPromise: Promise; - - static async init() { - if (YoutubeJs.innerTube) { - return; - } - - if (YoutubeJs.initPromise) { - return YoutubeJs.initPromise; - } - - YoutubeJs.initPromise = new Promise(async (resolve, reject) => { - try { - YoutubeJs.innerTube = await Innertube.create({ - // @ts-ignore - fetch: async (input: RequestInfo | URL, init?: RequestInit) => { - - const method = init?.method - ? init.method - : input instanceof Request - ? input.method - : 'GET'; - - const url = typeof input === 'string' - ? new URL(input) - : input instanceof URL - ? input - : new URL(input.url); - - const headers = init?.headers - ? new Headers(init.headers) - : input instanceof Request - ? input.headers - : new Headers(); - - const headersObject = {}; - headers.forEach((value, key) => { - headersObject[key] = value; - }); - - const body = init?.body; - - const args = { - Method: method, - Url: url, - Headers: headersObject, - Body: body, - CacheSeconds: -1, - }; - - const response = await fetch(`http://${getHost()}/api/proxy`, { - headers: { - 'Content-Type': 'application/json' - }, - method: "POST", - body: JSON.stringify(args) - }) - - const responseData = await response.json(); - - return new Response(responseData.body, { - status: responseData.status, - statusText: `${responseData.status}`, - headers: responseData.headers, - }); - } - }); - resolve(); - } catch (error) { - console.error(error); - YoutubeJs.innerTube = null; - reject(error); - } - YoutubeJs.initPromise = null; - }); - await YoutubeJs.initPromise; - } - - static async getVideoInfo(videoId: string) { - await this.init(); - const info = await YoutubeJs.innerTube.getBasicInfo(videoId, 'IOS'); - - console.log('[YTJS] info:', info); - - if (info.playability_status.status !== 'OK') { - let errorMessage = ""; - if (info.playability_status.reason) { - errorMessage = info.playability_status.reason; - } - if (info.playability_status.error_screen) { - if (info.playability_status.error_screen.hasKey('subreason')) { - const subreason = info.playability_status.error_screen.subreason; - if (subreason?.text && typeof subreason.text === 'string') { - errorMessage += `\n` + subreason.text; - } - } - } - - throw new Error(errorMessage); - } - - // Populate a video object that is similar to Invidious format. - // Mostly populate only fields we care about, enough to make it work. - return { - type: "video", - title: info.basic_info.title, - videoId: info.basic_info.id, - videoThumbnails: [{ - quality: "medium", - url: `https://i.ytimg.com/vi/${info.basic_info.id}/mqdefault.jpg`, - width: 320, - height: 180 - }], - storyboards: [], - description: "", - published: 0, - publishedText: "", - keywords: [], - viewCount: 0, - likeCount: 0, - dislikeCount: 0, - paid: false, - premium: false, - isFamilyFriendly: true, - allowedRegions: [], - genre: "", - genreUrl: null, - author: info.basic_info.author, - authorId: info.basic_info.channel_id, - authorUrl: "", - authorVerified: false, - authorThumbnails: [], - subCountText: "", - lengthSeconds: info.basic_info.duration, - allowRatings: true, - rating: 0, - isListed: true, - liveNow: info.basic_info.is_live, - isPostLiveDvr: info.basic_info.is_post_live_dvr, - isUpcoming: info.basic_info.is_upcoming, - dashUrl: "", - hlsUrl: info.streaming_data.hls_manifest_url, - adaptiveFormats: [], - formatStreams: [], - captions: [], - recommendedVideos: [], - } - } -} diff --git a/playlet-web/src/lib/LinkDragDrop.svelte b/playlet-web/src/lib/LinkDragDrop.svelte index e008c8ad..27ae9065 100644 --- a/playlet-web/src/lib/LinkDragDrop.svelte +++ b/playlet-web/src/lib/LinkDragDrop.svelte @@ -2,7 +2,7 @@ import { onDestroy, onMount } from "svelte"; import { playletStateStore, tr } from "lib/Stores"; import { InvidiousApi } from "lib/Api/InvidiousApi"; - import { YoutubeJs } from "./Api/YoutubeJs"; + import { PlayletApi } from "./Api/PlayletApi"; import VideoCastDialog from "./VideoFeed/VideoCastDialog.svelte"; import ChannelCastDialog from "./VideoFeed/ChannelCastDialog.svelte"; import PlaylistCastDialog from "./VideoFeed/PlaylistCastDialog.svelte"; @@ -109,7 +109,7 @@ async function searchForVideoById(videoId, timestamp) { try { isLoading = true; - videoMetadata = await YoutubeJs.getVideoInfo(videoId); + videoMetadata = await PlayletApi.getVideoInfo(videoId); videoStartAtChecked = timestamp !== undefined; if (videoStartAtChecked) { videoStartAtTimestamp = timestamp; diff --git a/playlet-web/src/lib/VideoFeed/PlaylistCastDialog.svelte b/playlet-web/src/lib/VideoFeed/PlaylistCastDialog.svelte index 2643ed15..e89bcd74 100644 --- a/playlet-web/src/lib/VideoFeed/PlaylistCastDialog.svelte +++ b/playlet-web/src/lib/VideoFeed/PlaylistCastDialog.svelte @@ -77,7 +77,7 @@ } } if (url === "") { - url = `${invidiousInstance}/vi/-----------/mqdefault.jpg`; + url = `https://i.ytimg.com/vi/-----------/mqdefault.jpg`; } return url; } diff --git a/playlet-web/src/lib/VideoFeed/PlaylistThumbnail.svelte b/playlet-web/src/lib/VideoFeed/PlaylistThumbnail.svelte index 1c12b4ab..6eecf121 100644 --- a/playlet-web/src/lib/VideoFeed/PlaylistThumbnail.svelte +++ b/playlet-web/src/lib/VideoFeed/PlaylistThumbnail.svelte @@ -32,7 +32,7 @@ } } if (url === "") { - url = `${invidiousInstance}/vi/-----------/mqdefault.jpg`; + url = `https://i.ytimg.com/vi/-----------/mqdefault.jpg`; } thumbnailUrl = url; }