Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Invidious's download endpoint #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { bearerAuth } from "hono/bearer-auth";
import youtubeApiPlayer from "./youtube_api_routes/player.ts";
import invidiousRouteLatestVersion from "./invidious_routes/latestVersion.ts";
import invidiousRouteDashManifest from "./invidious_routes/dashManifest.ts";
import {getDownloadHandler} from "./invidious_routes/download.ts";
import videoPlaybackProxy from "./videoPlaybackProxy.ts";
import health from "./health.ts";

Expand All @@ -21,6 +22,8 @@ export const routes = (app: Hono, konfigStore: Store<Record<string, unknown>>) =

app.route("/youtubei/v1", youtubeApiPlayer);
app.route("/latest_version", invidiousRouteLatestVersion);
// Needs app for app.request in order to call /latest_version endpoint
app.post("/download", getDownloadHandler(app));
app.route("/api/manifest/dash/id", invidiousRouteDashManifest);
app.route("/videoplayback", videoPlaybackProxy);
app.route("/healthz", health);
Expand Down
64 changes: 64 additions & 0 deletions src/routes/invidious_routes/download.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { HTTPException } from "hono/http-exception";
import { Store } from "@willsoto/node-konfig-core";
import {
verifyRequest
} from "../../lib/helpers/verifyRequest.ts";

export function getDownloadHandler(app) {
async function handler(c) {
const body = await c.req.formData();

const videoId = body.get("id");

const konfigStore = await c.get("konfigStore") as Store<
Record<string, unknown>
>;

const check = c.req.query("check");

if (konfigStore.get("server.verify_requests") && check == undefined) {
throw new HTTPException(400, {
res: new Response("No check ID."),
});
} else if (konfigStore.get("server.verify_requests") && check) {
if (verifyRequest(check, videoId, konfigStore) === false) {
throw new HTTPException(400, {
res: new Response("ID incorrect."),
});
}
}

const title = body.get("title");

let downloadWidgetData : { itag: number; ext: string; label: string};

try {
downloadWidgetData = JSON.parse(body.get("download_widget"));
} catch (error) {
throw new HTTPException(400, {res: new Response("Invalid download_widget json"), });
}

if (!(title || videoId || (downloadWidgetData.itag && downloadWidgetData.ext))) {
throw new HTTPException(400, {res: new Response("Invalid form data"), });
}
const itag = Number(downloadWidgetData.itag);
const {ext, label} = downloadWidgetData;

const filename = `${title}-${videoId}.${ext || ''}`;

if (label) {
} else if (itag) {
const urlQueriesForLatestVersion = new URLSearchParams();
urlQueriesForLatestVersion.set("id", videoId);
urlQueriesForLatestVersion.set("itag", itag.toString());
urlQueriesForLatestVersion.set("title", filename);
urlQueriesForLatestVersion.set("local", "true");

return await app.request(`/latest_version?${urlQueriesForLatestVersion.toString()}`);
} else {
throw new HTTPException(400, {res: new Response("Invalid label or itag"), });
}
}

return handler
}