mirror of
https://github.com/movie-web/providers-api.git
synced 2025-09-13 02:53:26 +00:00
Add initial POC of uber proxy
This commit is contained in:
19
package.json
19
package.json
@@ -3,29 +3,24 @@
|
||||
"version": "1.0.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"prepare": "nitropack prepare",
|
||||
"dev": "nitropack dev",
|
||||
"build": "nitropack build",
|
||||
"build:cloudflare": "NITRO_PRESET=cloudflare npm run build",
|
||||
"build:aws": "NITRO_PRESET=aws_lambda npm run build",
|
||||
"build:node": "NITRO_PRESET=node-server npm run build",
|
||||
"start": "node .output/server/index.mjs",
|
||||
"lint": "eslint --ext .ts src/",
|
||||
"lint:fix": "eslint --fix --ext .ts src/",
|
||||
"dev": "wrangler dev src/index.ts",
|
||||
"deploy": "wrangler deploy --minify src/index.ts",
|
||||
"preinstall": "npx only-allow pnpm"
|
||||
},
|
||||
"dependencies": {
|
||||
"@movie-web/providers": "^1.1.5",
|
||||
"h3": "^1.9.0",
|
||||
"nitropack": "latest"
|
||||
"hono": "^3.11.8",
|
||||
"zod": "^3.22.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@cloudflare/workers-types": "^4.20231121.0",
|
||||
"@typescript-eslint/eslint-plugin": "^6.14.0",
|
||||
"@typescript-eslint/parser": "^6.14.0",
|
||||
"eslint": "^8.56.0",
|
||||
"eslint-config-airbnb-base": "^15.0.0",
|
||||
"eslint-config-prettier": "^9.1.0",
|
||||
"eslint-import-resolver-typescript": "^3.6.1",
|
||||
"eslint-plugin-prettier": "^5.0.1"
|
||||
"eslint-plugin-prettier": "^5.0.1",
|
||||
"wrangler": "^3.21.0"
|
||||
}
|
||||
}
|
||||
|
2459
pnpm-lock.yaml
generated
2459
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
149
src/index.ts
Normal file
149
src/index.ts
Normal file
@@ -0,0 +1,149 @@
|
||||
import { Context, Env, Hono } from 'hono';
|
||||
import { streamSSE } from 'hono/streaming';
|
||||
import {
|
||||
ScrapeMedia,
|
||||
makeProviders,
|
||||
makeStandardFetcher,
|
||||
targets,
|
||||
} from '@movie-web/providers';
|
||||
import { ZodError, z } from 'zod';
|
||||
|
||||
const app = new Hono();
|
||||
let id = 0;
|
||||
|
||||
const fetcher = makeStandardFetcher(fetch);
|
||||
|
||||
const providers = makeProviders({
|
||||
fetcher,
|
||||
target: targets.NATIVE,
|
||||
});
|
||||
|
||||
async function outputEvent(
|
||||
stream: Parameters<Parameters<typeof streamSSE>['1']>['0'],
|
||||
event: string,
|
||||
data: any,
|
||||
) {
|
||||
return await stream.writeSSE({
|
||||
event,
|
||||
data: JSON.stringify(data),
|
||||
id: String(id++),
|
||||
});
|
||||
}
|
||||
|
||||
const tmdbIdSchema = z.string().regex(/^\d+$/)
|
||||
|
||||
const mediaSchema = z
|
||||
.discriminatedUnion('type', [
|
||||
z.object({
|
||||
type: z.literal('movie'),
|
||||
title: z.string().min(1),
|
||||
releaseYear: z.coerce.number().int().gt(0),
|
||||
tmdbId: tmdbIdSchema,
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('show'),
|
||||
title: z.string().min(1),
|
||||
releaseYear: z.coerce.number().int().gt(0),
|
||||
tmdbId: tmdbIdSchema,
|
||||
episodeNumber: z.coerce.number().int(),
|
||||
episodeTmdbId: tmdbIdSchema,
|
||||
seasonNumber: z.coerce.number().int(),
|
||||
seasonTmdbId: tmdbIdSchema,
|
||||
}),
|
||||
])
|
||||
.transform((query) => {
|
||||
if (query.type == 'movie') return query;
|
||||
|
||||
return {
|
||||
type: query.type,
|
||||
title: query.title,
|
||||
releaseYear: query.releaseYear,
|
||||
tmdbId: query.tmdbId,
|
||||
episode: {
|
||||
number: query.episodeNumber,
|
||||
tmdbId: query.episodeTmdbId,
|
||||
},
|
||||
season: {
|
||||
number: query.seasonNumber,
|
||||
tmdbId: query.seasonTmdbId,
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
async function validateTurnstile(context: Context<Env>) {
|
||||
const turnstileSecret = context.env?.TURNSTILE_SECRET as string | undefined
|
||||
|
||||
const token = context.req.header("cf-turnstile-token") || ""
|
||||
|
||||
// TODO: Make this cross platform
|
||||
const ip = context.req.header('CF-Connecting-IP') || "";
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append('secret', turnstileSecret || "");
|
||||
formData.append('response', token);
|
||||
formData.append('remoteip', ip);
|
||||
|
||||
const url = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';
|
||||
const result = await fetch(url, {
|
||||
body: formData,
|
||||
method: 'POST',
|
||||
});
|
||||
|
||||
const outcome = await result.json<any>();
|
||||
return outcome.success
|
||||
}
|
||||
|
||||
app.get('/scrape', async (context) => {
|
||||
const queryParams = context.req.query();
|
||||
|
||||
const turnstileEnabled = Boolean(context.env?.TURNSTILE_ENABLED)
|
||||
|
||||
if (turnstileEnabled) {
|
||||
const success = await validateTurnstile(context)
|
||||
|
||||
if (!success) {
|
||||
context.status(401)
|
||||
return context.text("Turnstile invalid")
|
||||
}
|
||||
}
|
||||
|
||||
let media: ScrapeMedia;
|
||||
try {
|
||||
media = mediaSchema.parse(queryParams);
|
||||
} catch (e) {
|
||||
if (e instanceof ZodError) {
|
||||
context.status(400);
|
||||
return context.json(e.format());
|
||||
}
|
||||
context.status(500);
|
||||
return context.text('An error has occurred!');
|
||||
}
|
||||
|
||||
return streamSSE(context, async (stream) => {
|
||||
const output = await providers.runAll({
|
||||
media,
|
||||
events: {
|
||||
discoverEmbeds(evt) {
|
||||
outputEvent(stream, 'discoverEmbeds', evt);
|
||||
},
|
||||
init(evt) {
|
||||
outputEvent(stream, 'init', evt);
|
||||
},
|
||||
start(evt) {
|
||||
outputEvent(stream, 'start', evt);
|
||||
},
|
||||
update(evt) {
|
||||
outputEvent(stream, 'update', evt);
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (output) {
|
||||
return await outputEvent(stream, 'completed', output);
|
||||
}
|
||||
|
||||
stream.writeSSE({ event: 'noOutput', data: '', id: String(id++) });
|
||||
});
|
||||
});
|
||||
|
||||
export default app;
|
25
tsconfig.json
Normal file
25
tsconfig.json
Normal file
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ESNext",
|
||||
"lib": ["dom", "dom.iterable", "esnext"],
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true,
|
||||
"esModuleInterop": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"strict": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "node",
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"noEmit": true,
|
||||
"baseUrl": "./src",
|
||||
"paths": {
|
||||
"@/*": ["./*"]
|
||||
},
|
||||
"types": [
|
||||
"@cloudflare/workers-types"
|
||||
],
|
||||
},
|
||||
}
|
8
wrangler.toml
Normal file
8
wrangler.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
name = "providers-api"
|
||||
main = "./src/index.ts"
|
||||
workers_dev = true
|
||||
compatibility_date = "2023-12-17"
|
||||
|
||||
[vars]
|
||||
TURNSTILE_ENABLED = true
|
||||
TURNSTILE_SECRET = "1x0000000000000000000000000000000AA"
|
Reference in New Issue
Block a user