@letsmoe/otter-sdk 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/README.md +184 -2
  2. package/package.json +40 -4
  3. package/src/aes-gcm.ts +189 -0
  4. package/src/client.ts +23 -0
  5. package/src/context.ts +74 -0
  6. package/src/definition.ts +81 -0
  7. package/src/errors.ts +61 -0
  8. package/src/extract.ts +134 -0
  9. package/src/generated/index.ts +79 -0
  10. package/src/generated/runtime/batcher.ts +275 -0
  11. package/src/generated/runtime/createClient.ts +68 -0
  12. package/src/generated/runtime/error.ts +29 -0
  13. package/src/generated/runtime/fetcher.ts +97 -0
  14. package/src/generated/runtime/generateGraphqlOperation.ts +225 -0
  15. package/src/generated/runtime/index.ts +13 -0
  16. package/src/generated/runtime/linkTypeMap.ts +156 -0
  17. package/src/generated/runtime/typeSelection.ts +95 -0
  18. package/src/generated/runtime/types.ts +69 -0
  19. package/src/generated/schema.graphql +657 -0
  20. package/src/generated/schema.ts +1338 -0
  21. package/src/generated/types.ts +1495 -0
  22. package/src/hosters/dead-link.ts +55 -0
  23. package/src/hosters/dood.ts +69 -0
  24. package/src/hosters/filemoon.ts +152 -0
  25. package/src/hosters/generic.ts +63 -0
  26. package/src/hosters/index.ts +14 -0
  27. package/src/hosters/recaptcha.ts +61 -0
  28. package/src/hosters/registry.ts +74 -0
  29. package/src/hosters/streamtape.ts +63 -0
  30. package/src/hosters/vidara.ts +48 -0
  31. package/src/hosters/vinovo.ts +55 -0
  32. package/src/hosters/voe-decoding.ts +131 -0
  33. package/src/hosters/voe-domains.ts +39 -0
  34. package/src/hosters/voe.ts +82 -0
  35. package/src/http-client.ts +295 -0
  36. package/src/http.ts +97 -0
  37. package/src/index.ts +9 -0
  38. package/src/languages.ts +73 -0
  39. package/src/match.ts +120 -0
  40. package/src/model.ts +110 -0
  41. package/src/packer.ts +106 -0
  42. package/src/plugin.ts +4 -0
  43. package/src/run.ts +74 -0
  44. package/src/runtime.ts +254 -0
  45. package/src/testing.ts +120 -0
  46. package/src/timing.ts +11 -0
  47. package/src/types.ts +122 -0
  48. package/src/url.ts +60 -0
@@ -0,0 +1,55 @@
1
+ import { HttpError, PluginDeadLinkError } from "../errors.ts";
2
+ import type { PluginContext } from "../definition.ts";
3
+ import { errorForStatus, type HttpResponse, type RequestOptions } from "../http-client.ts";
4
+
5
+ const GONE_STATUSES = [404, 410];
6
+ const API_MISSING_MESSAGE = /not found|missing|deleted|removed|does not exist/i;
7
+
8
+ function isGoneStatus(status: number): boolean {
9
+ return GONE_STATUSES.includes(status);
10
+ }
11
+
12
+ /**
13
+ * Fetches the page of the link itself. A 404 or 410 there means the hoster
14
+ * dropped the file; every other failure (challenge, 403, 429, 5xx) keeps its
15
+ * own error. Do not use it for secondary requests such as assets or APIs.
16
+ */
17
+ export async function getLinkPage(url: string, ctx: PluginContext, options: RequestOptions = {}): Promise<HttpResponse> {
18
+ try {
19
+ return await ctx.http.get(url, options);
20
+ } catch (error) {
21
+ if (error instanceof HttpError && error.status !== undefined && isGoneStatus(error.status)) {
22
+ throw new PluginDeadLinkError(`${url} no longer exists (HTTP ${error.status})`, error.status);
23
+ }
24
+ throw error;
25
+ }
26
+ }
27
+
28
+ /** For hosters that answer a missing file with a normal 200 page: throws when the page matches `missingPage`. */
29
+ export function assertFilePresent(html: string, missingPage: RegExp, url: string): void {
30
+ if (missingPage.test(html)) throw new PluginDeadLinkError(`${url} says the file does not exist`);
31
+ }
32
+
33
+ function apiErrorMessage(body: string): string {
34
+ try {
35
+ const parsed = JSON.parse(body);
36
+ if (typeof parsed.error === "string") return parsed.error;
37
+ return "";
38
+ } catch {
39
+ return "";
40
+ }
41
+ }
42
+
43
+ /** Whether a hoster API answered "no such video" for the link: a 404/410 whose JSON error names the missing video. */
44
+ export function apiReportsMissingFile(status: number, body: string): boolean {
45
+ if (!isGoneStatus(status)) return false;
46
+ return API_MISSING_MESSAGE.test(apiErrorMessage(body));
47
+ }
48
+
49
+ /** Throws the dead link error when a hoster API reported a missing file, otherwise the error the status stands for. */
50
+ export function throwForApiFailure(url: string, status: number, body: string): never {
51
+ if (apiReportsMissingFile(status, body)) {
52
+ throw new PluginDeadLinkError(`${url} reports the video as missing (HTTP ${status})`, status);
53
+ }
54
+ throw errorForStatus(url, status);
55
+ }
@@ -0,0 +1,69 @@
1
+ import { PluginSourceError } from "../errors.ts";
2
+ import type { PluginContext } from "../definition.ts";
3
+ import type { SourceInput } from "../model.ts";
4
+ import { originOf, resolveAgainst } from "../url.ts";
5
+ import { assertFilePresent, getLinkPage } from "./dead-link.ts";
6
+ import { defineHoster } from "./registry.ts";
7
+
8
+ const TOKEN_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
9
+ const TOKEN_LENGTH = 10;
10
+
11
+ // The player script: dsplayer.hotkeys[...]'<pass_md5 path>' ... function makePlay ... return ...?token=<token>&expiry=
12
+ const PLAYER_SCRIPT = /dsplayer\.hotkeys[^']+'([^']+)[\s\S]+?function\s*makePlay[\s\S]+?return[^?]+([^"]+)/;
13
+
14
+ // The player of a missing video is a 200 page titled "Video not found" (or "File not found" on /w/ links).
15
+ const MISSING_PLAYER_PAGE = /<title>\s*(Video|File) not found/i;
16
+
17
+ function randomSuffix(): string {
18
+ let suffix = "";
19
+ for (let index = 0; index < TOKEN_LENGTH; index++) {
20
+ suffix += TOKEN_CHARACTERS[Math.floor(Math.random() * TOKEN_CHARACTERS.length)];
21
+ }
22
+ return suffix;
23
+ }
24
+
25
+ /** The /d/ landing page either embeds the player in an iframe or has a twin at /e/. */
26
+ async function loadPlayerHtml(landingUrl: string, ctx: PluginContext): Promise<string> {
27
+ const landing = await getLinkPage(landingUrl, ctx, { referer: originOf(landingUrl) + "/" });
28
+ const finalLandingUrl = landing.url || landingUrl;
29
+ const landingHtml = await landing.text();
30
+ const frame = landingHtml.match(/<iframe\s*src="([^"]+)/);
31
+ let playerUrl = finalLandingUrl.replace("/d/", "/e/");
32
+ if (frame) playerUrl = resolveAgainst(finalLandingUrl, frame[1]);
33
+ const player = await getLinkPage(playerUrl, ctx, { referer: finalLandingUrl });
34
+ const playerHtml = await player.text();
35
+ assertFilePresent(playerHtml, MISSING_PLAYER_PAGE, playerUrl);
36
+ return playerHtml;
37
+ }
38
+
39
+ async function retrieveDood(url: string, ctx: PluginContext): Promise<SourceInput> {
40
+ const landingUrl = url.replace("/e/", "/d/");
41
+ const html = await loadPlayerHtml(landingUrl, ctx);
42
+ const script = html.match(PLAYER_SCRIPT);
43
+ if (!script) {
44
+ throw new PluginSourceError("extraction_failed", `Dood: no player script on ${url}`);
45
+ }
46
+
47
+ const origin = originOf(landingUrl);
48
+ const passResponse = await ctx.http.get(resolveAgainst(origin + "/", script[1]), { referer: origin + "/" });
49
+ const passBody = (await passResponse.text()).trim();
50
+ const headers = { Referer: origin + "/" };
51
+ if (passBody.includes("cloudflarestorage.")) {
52
+ return { url: passBody, mime_type: "video/mp4", headers };
53
+ }
54
+ return { url: passBody + randomSuffix() + script[2] + Date.now(), mime_type: "video/mp4", headers };
55
+ }
56
+
57
+ // Mirrors of DoodStream, from the ResolveURL project's list; they come and go.
58
+ export const dood = defineHoster({
59
+ name: "Doodstream",
60
+ domains: [
61
+ "dood.watch", "doodstream.com", "dood.to", "dood.so", "dood.cx", "dood.la", "dood.ws",
62
+ "dood.sh", "doodstream.co", "dood.pm", "dood.wf", "dood.re", "dood.yt", "dooood.com",
63
+ "dood.stream", "ds2play.com", "doods.pro", "ds2video.com", "d0o0d.com", "do0od.com",
64
+ "d0000d.com", "d000d.com", "dood.li", "dood.work", "dooodster.com", "vidply.com",
65
+ "all3do.com", "do7go.com", "doodcdn.io", "doply.net", "vide0.net", "vvide0.com",
66
+ "d-s.io", "dsvplay.com", "myvidplay.com", "playmogo.com",
67
+ ],
68
+ retrieve: retrieveDood,
69
+ });
@@ -0,0 +1,152 @@
1
+ import { PluginDeadLinkError, PluginSourceError } from "../errors.ts";
2
+ import type { HttpResponse } from "../http-client.ts";
3
+ import type { PluginContext } from "../definition.ts";
4
+ import type { SourceInput } from "../model.ts";
5
+ import { originOf } from "../url.ts";
6
+ import { decryptAesGcm } from "../aes-gcm.ts";
7
+ import { apiReportsMissingFile, getLinkPage } from "./dead-link.ts";
8
+ import { defineHoster } from "./registry.ts";
9
+
10
+ const MAX_FRAME_DEPTH = 2;
11
+
12
+ interface PlayerPage {
13
+ url: string;
14
+ html: string;
15
+ }
16
+
17
+ function sourceFor(streamUrl: string, page: PlayerPage, ctx: PluginContext): SourceInput {
18
+ return {
19
+ url: streamUrl,
20
+ mime_type: ctx.extract.mimeType(streamUrl, "application/x-mpegURL"),
21
+ headers: { Referer: originOf(page.url) + "/" },
22
+ };
23
+ }
24
+
25
+ /** The embed page names the playlist in a data attribute, or packs it into its player script. */
26
+ function streamUrlOf(html: string, ctx: PluginContext): string | undefined {
27
+ const attribute = html.match(/data-hls-url="([^"]+)/);
28
+ if (attribute) return attribute[1].replace(/&amp;/g, "&");
29
+ const media = ctx.extract.mediaUrls(html);
30
+ const playlist = media.find((candidate) => candidate.mime_type === "application/x-mpegURL");
31
+ if (playlist) return playlist.url;
32
+ if (media.length > 0) return media[0].url;
33
+ return undefined;
34
+ }
35
+
36
+ /** Only the embed page of the link itself says whether the file exists; frames inside it are secondary. */
37
+ async function fetchPage(pageUrl: string, referer: string, depth: number, ctx: PluginContext): Promise<HttpResponse> {
38
+ if (depth === 0) return getLinkPage(pageUrl, ctx, { referer });
39
+ return ctx.http.get(pageUrl, { referer });
40
+ }
41
+
42
+ async function findStream(pageUrl: string, referer: string, depth: number, ctx: PluginContext): Promise<SourceInput | undefined> {
43
+ const response = await fetchPage(pageUrl, referer, depth, ctx);
44
+ const page = { url: response.url || pageUrl, html: await response.text() };
45
+ const streamUrl = streamUrlOf(page.html, ctx);
46
+ if (streamUrl) return sourceFor(streamUrl, page, ctx);
47
+ if (depth >= MAX_FRAME_DEPTH) return undefined;
48
+
49
+ // Newer embeds only hold an iframe to the actual player on another domain.
50
+ const frames = ctx.extract.iframes(page.html, page.url);
51
+ if (frames.length === 0) return undefined;
52
+ return findStream(frames[0], page.url, depth + 1, ctx);
53
+ }
54
+
55
+ interface EncryptedPlayback {
56
+ iv: string;
57
+ payload: string;
58
+ key_parts: string[];
59
+ version: string;
60
+ }
61
+
62
+ interface VideoSource {
63
+ url: string;
64
+ height?: number;
65
+ }
66
+
67
+ const KEY_PART_VERSIONS = 20;
68
+
69
+ function bytesOfBase64Url(text: string): Uint8Array {
70
+ const standard = text.replace(/-/g, "+").replace(/_/g, "/");
71
+ const padding = (4 - (standard.length % 4)) % 4;
72
+ return Uint8Array.from(Buffer.from(standard + "=".repeat(padding), "base64"));
73
+ }
74
+
75
+ /** The playback key is two of the 1-based key parts, picked by the playback version n (1..20): parts n and 31 - n. */
76
+ function keyOf(playback: EncryptedPlayback): Uint8Array {
77
+ const version = parseInt(playback.version, 10);
78
+ if (!(version >= 1 && version <= KEY_PART_VERSIONS)) {
79
+ throw new PluginSourceError("extraction_failed", `Filemoon: unknown playback version ${playback.version}`);
80
+ }
81
+ const firstPart = bytesOfBase64Url(playback.key_parts[version - 1]);
82
+ const secondPart = bytesOfBase64Url(playback.key_parts[31 - version - 1]);
83
+ const key = new Uint8Array(firstPart.length + secondPart.length);
84
+ key.set(firstPart);
85
+ key.set(secondPart, firstPart.length);
86
+ return key;
87
+ }
88
+
89
+ function decryptSources(playback: EncryptedPlayback): VideoSource[] {
90
+ const plaintext = decryptAesGcm(keyOf(playback), bytesOfBase64Url(playback.iv), bytesOfBase64Url(playback.payload));
91
+ const decoded = JSON.parse(Buffer.from(plaintext).toString("utf8"));
92
+ return decoded.sources || [];
93
+ }
94
+
95
+ function bestSource(sources: VideoSource[]): VideoSource | undefined {
96
+ let best: VideoSource | undefined;
97
+ for (const source of sources) {
98
+ if (!best || (source.height || 0) > (best.height || 0)) best = source;
99
+ }
100
+ return best;
101
+ }
102
+
103
+ function videoCodeOf(url: string): string {
104
+ const segments = new URL(url).pathname.split("/").filter(Boolean);
105
+ return segments[segments.length - 1] || "";
106
+ }
107
+
108
+ /** Domains without the video API answer with the SPA's HTML. */
109
+ function parseVideo(body: string): { playback?: EncryptedPlayback } {
110
+ try {
111
+ return JSON.parse(body);
112
+ } catch {
113
+ return {};
114
+ }
115
+ }
116
+
117
+ /** The current embed is a single-page app; its video API serves the sources AES-GCM encrypted. */
118
+ async function findStreamViaApi(url: string, ctx: PluginContext): Promise<SourceInput | undefined> {
119
+ const code = videoCodeOf(url);
120
+ if (code === "") return undefined;
121
+ const origin = originOf(url);
122
+ const response = await ctx.http.get(`${origin}/api/videos/${code}`, {
123
+ referer: `${origin}/e/${code}`,
124
+ allow_error_status: true,
125
+ });
126
+ const body = await response.text();
127
+ if (apiReportsMissingFile(response.status, body)) {
128
+ throw new PluginDeadLinkError(`Filemoon: ${url} reports the video as missing`, response.status);
129
+ }
130
+ if (response.status >= 400) return undefined;
131
+ const video = parseVideo(body);
132
+ if (!video.playback) return undefined;
133
+ const best = bestSource(decryptSources(video.playback));
134
+ if (!best) return undefined;
135
+ return sourceFor(best.url, { url: origin + "/", html: "" }, ctx);
136
+ }
137
+
138
+ async function retrieveFilemoon(url: string, ctx: PluginContext): Promise<SourceInput> {
139
+ const apiSource = await findStreamViaApi(url, ctx);
140
+ if (apiSource) return apiSource;
141
+ const source = await findStream(url, originOf(url) + "/", 0, ctx);
142
+ if (!source) {
143
+ throw new PluginSourceError("extraction_failed", `Filemoon: no stream found on ${url}`);
144
+ }
145
+ return source;
146
+ }
147
+
148
+ export const filemoon = defineHoster({
149
+ name: "Filemoon",
150
+ domains: ["filemoon.sx", "filemoon.to", "filemoon.in", "filemoon.link", "filemoon.nl", "filemoon.org"],
151
+ retrieve: retrieveFilemoon,
152
+ });
@@ -0,0 +1,63 @@
1
+ import { PluginSourceError } from "../errors.ts";
2
+ import type { PluginContext } from "../definition.ts";
3
+ import type { ExtractedMedia } from "../extract.ts";
4
+ import type { SourceInput } from "../model.ts";
5
+ import { parse } from "node-html-parser";
6
+ import { getLinkPage } from "./dead-link.ts";
7
+ import { defineHoster } from "./registry.ts";
8
+
9
+ const MIME_PREFERENCE = ["application/x-mpegURL", "application/dash+xml", "video/mp4", "video/webm"];
10
+
11
+ function preferred(candidates: ExtractedMedia[]): ExtractedMedia | undefined {
12
+ for (const mimeType of MIME_PREFERENCE) {
13
+ const found = candidates.find((candidate) => candidate.mime_type === mimeType);
14
+ if (found) return found;
15
+ }
16
+ return undefined;
17
+ }
18
+
19
+ function videoTagSource(html: string, ctx: PluginContext): ExtractedMedia | undefined {
20
+ const root = parse(html);
21
+ for (const element of root.querySelectorAll("video[src], source[src]")) {
22
+ const source = element.getAttribute("src") || "";
23
+ if (source.startsWith("http") || source.startsWith("//")) {
24
+ const url = ctx.http.absolute(source);
25
+ return { url, mime_type: ctx.extract.mimeType(url) };
26
+ }
27
+ }
28
+ return undefined;
29
+ }
30
+
31
+ function mediaOnPage(html: string, ctx: PluginContext): ExtractedMedia | undefined {
32
+ const fromVideoTag = videoTagSource(html, ctx);
33
+ if (fromVideoTag) return fromVideoTag;
34
+ return preferred(ctx.extract.mediaUrls(html));
35
+ }
36
+
37
+ async function mediaInFrames(html: string, pageUrl: string, ctx: PluginContext): Promise<ExtractedMedia | undefined> {
38
+ for (const frameUrl of ctx.extract.iframes(html, pageUrl)) {
39
+ try {
40
+ const frame = await ctx.http.get(frameUrl, { referer: pageUrl });
41
+ const found = mediaOnPage(await frame.text(), ctx);
42
+ if (found) return found;
43
+ } catch {
44
+ ctx.log(`generic: skipping unreachable frame ${frameUrl}`);
45
+ }
46
+ }
47
+ return undefined;
48
+ }
49
+
50
+ /** For hosters nobody wrote an extractor for: takes the first media URL the page or its iframes show. */
51
+ export const genericHoster = defineHoster({
52
+ name: "Generic",
53
+ domains: [],
54
+ async retrieve(url, ctx): Promise<SourceInput> {
55
+ const page = await getLinkPage(url, ctx);
56
+ const html = await page.text();
57
+ const found = mediaOnPage(html, ctx) || (await mediaInFrames(html, url, ctx));
58
+ if (!found) {
59
+ throw new PluginSourceError("extraction_failed", `no stream URL found at ${url}`);
60
+ }
61
+ return { url: found.url, mime_type: found.mime_type };
62
+ },
63
+ });
@@ -0,0 +1,14 @@
1
+ import { dood } from "./dood.ts";
2
+ import { filemoon } from "./filemoon.ts";
3
+ import { genericHoster } from "./generic.ts";
4
+ import { HosterRegistry } from "./registry.ts";
5
+ import { streamtape } from "./streamtape.ts";
6
+ import { vidara } from "./vidara.ts";
7
+ import { vinovo } from "./vinovo.ts";
8
+ import { voe } from "./voe.ts";
9
+
10
+ export { defineHoster, HosterRegistry, type HosterDefinition } from "./registry.ts";
11
+ export { dood, filemoon, genericHoster, streamtape, vidara, vinovo, voe };
12
+
13
+ /** The built-in hosters. Links no hoster serves go through the generic page scanner. */
14
+ export const hosters = new HosterRegistry([voe, vidara, dood, filemoon, streamtape, vinovo], genericHoster);
@@ -0,0 +1,61 @@
1
+ import type { PluginContext } from "../definition.ts";
2
+ import { originOf } from "../url.ts";
3
+
4
+ const API_SCRIPT_URL = "https://www.google.com/recaptcha/api.js";
5
+ const ANCHOR_URL = "https://www.google.com/recaptcha/api2/anchor";
6
+ const RELOAD_URL = "https://www.google.com/recaptcha/api2/reload";
7
+
8
+ function siteKeyOf(html: string): string | undefined {
9
+ const found = html.match(/(?:src="https:\/\/www\.google\.com\/recaptcha\/api\.js\?.*?render|data-sitekey)="?([^"&]+)/);
10
+ if (!found) return undefined;
11
+ return found[1];
12
+ }
13
+
14
+ function encodeOrigin(origin: string): string {
15
+ return Buffer.from(`${origin}:443`).toString("base64").replace(/=+$/, "");
16
+ }
17
+
18
+ async function fetchReleaseVersion(siteKey: string, ctx: PluginContext, referer: string): Promise<string | undefined> {
19
+ const script = await ctx.http.get(`${API_SCRIPT_URL}?render=${siteKey}`, { referer });
20
+ const found = (await script.text()).match(/releases\/([^/]+)/);
21
+ if (!found) return undefined;
22
+ return found[1];
23
+ }
24
+
25
+ async function fetchAnchorToken(siteKey: string, origin: string, version: string, ctx: PluginContext, referer: string) {
26
+ const query = new URLSearchParams({
27
+ ar: "1",
28
+ k: siteKey,
29
+ co: encodeOrigin(origin),
30
+ hl: "en",
31
+ v: version,
32
+ size: "invisible",
33
+ cb: "123456789",
34
+ });
35
+ const anchor = await ctx.http.get(`${ANCHOR_URL}?${query.toString()}`, { referer });
36
+ const found = (await anchor.text()).match(/recaptcha-token.+?="([^"]+)/);
37
+ if (!found) return undefined;
38
+ return found[1];
39
+ }
40
+
41
+ /**
42
+ * Obtains an invisible reCAPTCHA v3 token for a page, the way the page's own
43
+ * script does. Returns an empty string when the page has no reCAPTCHA or Google refuses.
44
+ */
45
+ export async function recaptchaToken(html: string, pageUrl: string, ctx: PluginContext): Promise<string> {
46
+ const siteKey = siteKeyOf(html);
47
+ if (!siteKey) return "";
48
+ const origin = originOf(pageUrl);
49
+ const version = await fetchReleaseVersion(siteKey, ctx, pageUrl);
50
+ if (!version) return "";
51
+ const anchorToken = await fetchAnchorToken(siteKey, origin, version, ctx, pageUrl);
52
+ if (!anchorToken) return "";
53
+
54
+ const reload = await ctx.http.post(`${RELOAD_URL}?k=${siteKey}`, {
55
+ body: { v: version, reason: "q", k: siteKey, c: anchorToken, sa: "", co: encodeOrigin(origin) },
56
+ referer: ANCHOR_URL,
57
+ });
58
+ const found = (await reload.text()).match(/rresp","([^"]+)/);
59
+ if (!found) return "";
60
+ return found[1];
61
+ }
@@ -0,0 +1,74 @@
1
+ import type { PluginContext } from "../definition.ts";
2
+ import { domainOf } from "../http.ts";
3
+ import type { SourceInput } from "../model.ts";
4
+
5
+ export interface HosterDefinition {
6
+ /** Shown as the label of streams that point at this hoster. */
7
+ name: string;
8
+ domains: string[];
9
+ /** Turns a link on one of the domains into a playable media URL. */
10
+ retrieve(url: string, ctx: PluginContext): Promise<SourceInput>;
11
+ }
12
+
13
+ export function defineHoster(hoster: HosterDefinition): HosterDefinition {
14
+ return hoster;
15
+ }
16
+
17
+ function servesHost(hoster: HosterDefinition, host: string): boolean {
18
+ return hoster.domains.some((domain) => host === domain || host.endsWith("." + domain));
19
+ }
20
+
21
+ /** The hosters a plugin can turn links into media URLs with. */
22
+ export class HosterRegistry {
23
+ private readonly hosters: HosterDefinition[];
24
+
25
+ /** `fallback` handles links no registered hoster serves. */
26
+ constructor(
27
+ hosters: HosterDefinition[] = [],
28
+ private readonly fallback?: HosterDefinition,
29
+ ) {
30
+ this.hosters = [...hosters];
31
+ }
32
+
33
+ register(hoster: HosterDefinition): void {
34
+ this.hosters.push(hoster);
35
+ }
36
+
37
+ find(url: string): HosterDefinition | undefined {
38
+ const host = domainOf(url);
39
+ if (host === "") return undefined;
40
+ return this.hosters.find((hoster) => servesHost(hoster, host));
41
+ }
42
+
43
+ supports(url: string): boolean {
44
+ return this.find(url) !== undefined;
45
+ }
46
+
47
+ nameOf(url: string): string | undefined {
48
+ const hoster = this.find(url);
49
+ if (!hoster) return undefined;
50
+ return hoster.name;
51
+ }
52
+
53
+ /** Every domain a registered hoster serves. */
54
+ domains(): string[] {
55
+ return this.hosters.flatMap((hoster) => hoster.domains);
56
+ }
57
+
58
+ /**
59
+ * Retrieves the source with the hoster serving the link. Site links like
60
+ * aniworld.to/redirect/<id> redirect to a hoster, so an unknown link is
61
+ * followed first; only then does the fallback get it.
62
+ */
63
+ async retrieve(url: string, ctx: PluginContext): Promise<SourceInput> {
64
+ const hoster = this.find(url);
65
+ if (hoster) return hoster.retrieve(url, ctx);
66
+
67
+ const destination = await ctx.http.finalUrl(url);
68
+ const destinationHoster = this.find(destination);
69
+ if (destinationHoster) return destinationHoster.retrieve(destination, ctx);
70
+
71
+ if (!this.fallback) throw new Error(`no hoster serves ${url}`);
72
+ return this.fallback.retrieve(destination, ctx);
73
+ }
74
+ }
@@ -0,0 +1,63 @@
1
+ import { PluginSourceError } from "../errors.ts";
2
+ import type { PluginContext } from "../definition.ts";
3
+ import type { SourceInput } from "../model.ts";
4
+ import { originOf, resolveAgainst } from "../url.ts";
5
+ import { getLinkPage } from "./dead-link.ts";
6
+ import { defineHoster } from "./registry.ts";
7
+
8
+ // The page assigns the link in pieces to a hidden element:
9
+ // document.getElementById('x').innerHTML = "//host/get_video?id=" + ('abcxyz').substring(2) + ...
10
+ const LINK_ASSIGNMENT = /ById\('.+?=\s*(["']\/\/[^;<]+)/g;
11
+
12
+ /** Joins the quoted pieces of the assignment, applying each `.substring(n)` to its piece. */
13
+ export function assembleLink(assignment: string): string {
14
+ let link = "";
15
+ for (const piece of assignment.replace(/'/g, '"').split("+")) {
16
+ const text = piece.match(/"([^"]*)/);
17
+ if (!text) continue;
18
+ let skipped = 0;
19
+ for (const substring of piece.matchAll(/substring\((\d+)/g)) {
20
+ skipped += parseInt(substring[1], 10);
21
+ }
22
+ link += text[1].slice(skipped);
23
+ }
24
+ return link;
25
+ }
26
+
27
+ function lastAssignment(html: string): string | undefined {
28
+ const assignments = Array.from(html.matchAll(LINK_ASSIGNMENT));
29
+ if (assignments.length === 0) return undefined;
30
+ return assignments[assignments.length - 1][1];
31
+ }
32
+
33
+ async function retrieveStreamtape(url: string, ctx: PluginContext): Promise<SourceInput> {
34
+ const origin = originOf(url);
35
+ const playerUrl = url.replace("/v/", "/e/");
36
+ const page = await getLinkPage(playerUrl, ctx, { referer: origin + "/" });
37
+ const assignment = lastAssignment(await page.text());
38
+ if (!assignment) {
39
+ throw new PluginSourceError("extraction_failed", `Streamtape: no video link on ${url}`);
40
+ }
41
+
42
+ const redirectingLink = resolveAgainst(playerUrl, assembleLink(assignment) + "&stream=1");
43
+ const headers = { Referer: origin + "/" };
44
+ const redirect = await ctx.http.get(redirectingLink, { headers, follow_redirects: false });
45
+ const target = redirect.response().headers.get("location");
46
+ return { url: target || redirectingLink, mime_type: "video/mp4", headers };
47
+ }
48
+
49
+ // Mirrors of Streamtape, from the ResolveURL project's list.
50
+ export const streamtape = defineHoster({
51
+ name: "Streamtape",
52
+ domains: [
53
+ "streamtape.com", "strtape.cloud", "streamtape.net", "streamta.pe", "streamtape.site",
54
+ "strcloud.link", "strcloud.club", "strtpe.link", "streamtape.cc", "scloud.online", "stape.fun",
55
+ "streamadblockplus.com", "shavetape.cash", "streamtape.to", "streamta.site",
56
+ "streamadblocker.xyz", "tapewithadblock.org", "adblocktape.wiki", "antiadtape.com",
57
+ "streamtape.xyz", "tapeblocker.com", "streamnoads.com", "tapeadvertisement.com",
58
+ "tapeadsenjoyer.com", "watchadsontape.com", "tpead.net", "advertape.net",
59
+ "strtape.site", "strtapeadblock.me", "gettapeads.com",
60
+ "streamtapeadblock.art", "streamtapeadblockuser.xyz", "advtpe.com",
61
+ ],
62
+ retrieve: retrieveStreamtape,
63
+ });
@@ -0,0 +1,48 @@
1
+ import { PluginSourceError } from "../errors.ts";
2
+ import type { PluginContext } from "../definition.ts";
3
+ import type { SourceInput } from "../model.ts";
4
+ import { originOf } from "../url.ts";
5
+ import { throwForApiFailure } from "./dead-link.ts";
6
+ import { defineHoster } from "./registry.ts";
7
+
8
+ interface StreamResponse {
9
+ streaming_url?: string;
10
+ }
11
+
12
+ function filecodeOf(url: string): string {
13
+ const segments = new URL(url).pathname.split("/").filter(Boolean);
14
+ const filecode = segments[segments.length - 1];
15
+ if (!filecode) {
16
+ throw new PluginSourceError("extraction_failed", `Vidara: no file code in ${url}`);
17
+ }
18
+ return filecode;
19
+ }
20
+
21
+ async function retrieveVidara(url: string, ctx: PluginContext): Promise<SourceInput> {
22
+ const filecode = filecodeOf(url);
23
+ const origin = originOf(url);
24
+ const playerUrl = `${origin}/e/${filecode}`;
25
+
26
+ const response = await ctx.http.post(`${origin}/api/stream`, {
27
+ json: { filecode, device: "web" },
28
+ headers: { Referer: playerUrl, Origin: origin },
29
+ allow_error_status: true,
30
+ });
31
+ const body = await response.text();
32
+ if (response.status >= 400) throwForApiFailure(response.url || `${origin}/api/stream`, response.status, body);
33
+ const data = JSON.parse(body) as StreamResponse;
34
+ if (!data.streaming_url) {
35
+ throw new PluginSourceError("extraction_failed", `Vidara: no streaming_url for ${url}`);
36
+ }
37
+ return {
38
+ url: data.streaming_url,
39
+ mime_type: ctx.extract.mimeType(data.streaming_url, "application/x-mpegURL"),
40
+ headers: { Referer: playerUrl, Origin: origin },
41
+ };
42
+ }
43
+
44
+ export const vidara = defineHoster({
45
+ name: "Vidara",
46
+ domains: ["vidara.to", "vidara.so", "vidaraa.cc", "odysseusa.cc"],
47
+ retrieve: retrieveVidara,
48
+ });
@@ -0,0 +1,55 @@
1
+ import { PluginSourceError } from "../errors.ts";
2
+ import type { PluginContext } from "../definition.ts";
3
+ import type { SourceInput } from "../model.ts";
4
+ import { originOf } from "../url.ts";
5
+ import { assertFilePresent, getLinkPage } from "./dead-link.ts";
6
+ import { recaptchaToken } from "./recaptcha.ts";
7
+ import { defineHoster } from "./registry.ts";
8
+
9
+ // A missing file still gets the player page (200), with an error box instead of the video.
10
+ const MISSING_PLAYER_PAGE = /class="container error"[\s\S]{0,400}?Video not found/i;
11
+
12
+ interface FileUrlResponse {
13
+ status?: string;
14
+ token?: string;
15
+ }
16
+
17
+ function playerUrlOf(url: string): string {
18
+ const mediaId = new URL(url).pathname.split("/").filter(Boolean).pop();
19
+ return `${originOf(url)}/e/${mediaId}`;
20
+ }
21
+
22
+ async function retrieveVinovo(url: string, ctx: PluginContext): Promise<SourceInput> {
23
+ const playerUrl = playerUrlOf(url);
24
+ const origin = originOf(playerUrl);
25
+ const mediaId = playerUrl.split("/").pop();
26
+
27
+ const page = await getLinkPage(playerUrl, ctx, { referer: playerUrl });
28
+ const html = await page.text();
29
+ assertFilePresent(html, MISSING_PLAYER_PAGE, playerUrl);
30
+ const pageToken = html.match(/name="token"\s*content="([^"]+)/);
31
+ const streamBase = html.match(/<video[\s\S]+?data-base="([^"]+)/);
32
+ if (!pageToken || !streamBase) {
33
+ throw new PluginSourceError("extraction_failed", `Vinovo: no player data on ${playerUrl}`);
34
+ }
35
+
36
+ const response = await ctx.http.post(`${origin}/api/file/url/${mediaId}`, {
37
+ body: { token: pageToken[1], recaptcha: await recaptchaToken(html, playerUrl, ctx) },
38
+ headers: { Origin: origin, Referer: origin + "/", "X-Requested-With": "XMLHttpRequest" },
39
+ });
40
+ const data = await response.json<FileUrlResponse>();
41
+ if (data.status !== "ok" || !data.token) {
42
+ throw new PluginSourceError("extraction_failed", `Vinovo: file url refused for ${playerUrl}`);
43
+ }
44
+ return {
45
+ url: `${streamBase[1]}/stream/${data.token}`,
46
+ mime_type: "video/mp4",
47
+ headers: { Referer: origin + "/" },
48
+ };
49
+ }
50
+
51
+ export const vinovo = defineHoster({
52
+ name: "Vinovo",
53
+ domains: ["vinovo.to", "vinovo.si"],
54
+ retrieve: retrieveVinovo,
55
+ });