mirror of
https://github.com/sussy-code/smov.git
synced 2024-12-29 16:07:40 +01:00
Version checking + preparing streams
This commit is contained in:
parent
f70d13f2c9
commit
421186cb54
6 changed files with 68 additions and 4 deletions
5
src/backend/extension/compatibility.ts
Normal file
5
src/backend/extension/compatibility.ts
Normal file
|
@ -0,0 +1,5 @@
|
|||
const allowedExtensionVersion = ["0.0.1"];
|
||||
|
||||
export function isAllowedExtensionVersion(version: string): boolean {
|
||||
return allowedExtensionVersion.includes(version);
|
||||
}
|
|
@ -3,6 +3,8 @@ import {
|
|||
sendToBackgroundViaRelay,
|
||||
} from "@plasmohq/messaging";
|
||||
|
||||
import { isAllowedExtensionVersion } from "@/backend/extension/compatibility";
|
||||
|
||||
let activeExtension = false;
|
||||
|
||||
export interface ExtensionHello {
|
||||
|
@ -35,7 +37,14 @@ export async function sendExtensionRequest(
|
|||
url: string,
|
||||
ops: any,
|
||||
): Promise<ExtensionHello | null> {
|
||||
return sendMessage("proxy-request", { url, ...ops });
|
||||
return sendMessage("make-request", { url, ...ops });
|
||||
}
|
||||
|
||||
export async function setDomainRule(
|
||||
domains: string[],
|
||||
headers: Record<string, string>,
|
||||
): Promise<ExtensionHello | null> {
|
||||
return sendMessage("prepare-stream", { domains, headers });
|
||||
}
|
||||
|
||||
export async function extensionInfo(): Promise<ExtensionHello | null> {
|
||||
|
@ -47,5 +56,9 @@ export function isExtensionActiveCached(): boolean {
|
|||
}
|
||||
|
||||
export async function isExtensionActive(): Promise<boolean> {
|
||||
return !!(await extensionInfo());
|
||||
const info = await extensionInfo();
|
||||
if (!info) return false;
|
||||
const allowedVersion = isAllowedExtensionVersion(info.version);
|
||||
if (!allowedVersion) return false;
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -20,11 +20,11 @@ export type ExtensionRequestReply =
|
|||
};
|
||||
|
||||
interface MmMetadata {
|
||||
"declarative-net-request": {
|
||||
"prepare-stream": {
|
||||
req: PlasmoRequestBody;
|
||||
res: ExtensionRequestReply;
|
||||
};
|
||||
"proxy-request": {
|
||||
"make-request": {
|
||||
req: PlasmoRequestBody;
|
||||
res: ExtensionRequestReply;
|
||||
};
|
||||
|
|
40
src/backend/extension/streams.ts
Normal file
40
src/backend/extension/streams.ts
Normal file
|
@ -0,0 +1,40 @@
|
|||
import { Stream } from "@movie-web/providers";
|
||||
|
||||
import { setDomainRule } from "@/backend/extension/messaging";
|
||||
|
||||
function extractDomain(url: string): string {
|
||||
try {
|
||||
const u = new URL(url);
|
||||
return u.hostname;
|
||||
} catch {
|
||||
return url;
|
||||
}
|
||||
}
|
||||
|
||||
function extractDomainsFromStream(stream: Stream): string[] {
|
||||
if (stream.type === "hls") {
|
||||
return [extractDomain(stream.playlist)];
|
||||
}
|
||||
if (stream.type === "file") {
|
||||
return Object.values(stream.qualities).map((v) => extractDomain(v.url));
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
function buildHeadersFromStream(stream: Stream): Record<string, string> {
|
||||
const headers: Record<string, string> = {};
|
||||
Object.entries(stream.headers ?? {}).forEach((entry) => {
|
||||
headers[entry[0]] = entry[1];
|
||||
});
|
||||
Object.entries(stream.preferredHeaders ?? {}).forEach((entry) => {
|
||||
headers[entry[0]] = entry[1];
|
||||
});
|
||||
return headers;
|
||||
}
|
||||
|
||||
export async function prepareStream(stream: Stream) {
|
||||
await setDomainRule(
|
||||
extractDomainsFromStream(stream),
|
||||
buildHeadersFromStream(stream),
|
||||
);
|
||||
}
|
|
@ -5,6 +5,7 @@ import {
|
|||
} from "@movie-web/providers";
|
||||
import { useAsyncFn } from "react-use";
|
||||
|
||||
import { prepareStream } from "@/backend/extension/streams";
|
||||
import {
|
||||
connectServerSideEvents,
|
||||
makeProviderUrl,
|
||||
|
@ -131,6 +132,7 @@ export function useSourceScraping(sourceId: string | null, routerId: string) {
|
|||
]);
|
||||
|
||||
if (result.stream) {
|
||||
await prepareStream(result.stream[0]);
|
||||
setCaption(null);
|
||||
setSource(
|
||||
convertRunoutputToSource({ stream: result.stream[0] }),
|
||||
|
@ -187,6 +189,7 @@ export function useSourceScraping(sourceId: string | null, routerId: string) {
|
|||
]);
|
||||
setSourceId(sourceId);
|
||||
setCaption(null);
|
||||
await prepareStream(embedResult.stream[0]);
|
||||
setSource(
|
||||
convertRunoutputToSource({ stream: embedResult.stream[0] }),
|
||||
convertProviderCaption(embedResult.stream[0].captions),
|
||||
|
|
|
@ -5,6 +5,7 @@ import {
|
|||
} from "@movie-web/providers";
|
||||
import { RefObject, useCallback, useEffect, useRef, useState } from "react";
|
||||
|
||||
import { prepareStream } from "@/backend/extension/streams";
|
||||
import {
|
||||
connectServerSideEvents,
|
||||
getCachedMetadata,
|
||||
|
@ -169,6 +170,7 @@ export function useScrape() {
|
|||
conn.on("update", updateEvent);
|
||||
conn.on("discoverEmbeds", discoverEmbedsEvent);
|
||||
const sseOutput = await conn.promise();
|
||||
if (sseOutput) await prepareStream(sseOutput.stream);
|
||||
|
||||
return getResult(sseOutput === "" ? null : sseOutput);
|
||||
}
|
||||
|
@ -184,6 +186,7 @@ export function useScrape() {
|
|||
discoverEmbeds: discoverEmbedsEvent,
|
||||
},
|
||||
});
|
||||
if (output) await prepareStream(output.stream);
|
||||
return getResult(output);
|
||||
},
|
||||
[
|
||||
|
|
Loading…
Reference in a new issue