mirror of
https://github.com/movie-web/providers.git
synced 2025-09-13 16:33:26 +00:00
dev-cli with browser based fetching
This commit is contained in:
@@ -1,18 +1,91 @@
|
||||
/* eslint import/no-extraneous-dependencies: ["error", {"devDependencies": true}] */
|
||||
|
||||
import Spinnies from 'spinnies';
|
||||
import { existsSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
|
||||
import puppeteer, { Browser } from 'puppeteer';
|
||||
import Spinnies from 'spinnies';
|
||||
import { PreviewServer, build, preview } from 'vite';
|
||||
|
||||
import { getConfig } from '@/dev-cli/config';
|
||||
import { logDeepObject } from '@/dev-cli/logging';
|
||||
import { getMovieMediaDetails, getShowMediaDetails } from '@/dev-cli/tmdb';
|
||||
import { CommandLineArguments } from '@/dev-cli/validate';
|
||||
|
||||
import { MetaOutput, ProviderMakerOptions, makeProviders } from '..';
|
||||
|
||||
async function runActualScraping(
|
||||
async function runBrowserScraping(
|
||||
providerOptions: ProviderMakerOptions,
|
||||
source: MetaOutput,
|
||||
options: CommandLineArguments,
|
||||
) {
|
||||
if (!existsSync(join(__dirname, '../../lib/index.mjs')))
|
||||
throw new Error('Please compile before running cli in browser mode');
|
||||
const config = getConfig();
|
||||
if (!config.proxyUrl)
|
||||
throw new Error('Simple proxy url must be set in the environment (MOVIE_WEB_PROXY_URL) for browser mode to work');
|
||||
|
||||
const root = join(__dirname, 'browser');
|
||||
let server: PreviewServer | undefined;
|
||||
let browser: Browser | undefined;
|
||||
try {
|
||||
// setup browser
|
||||
await build({
|
||||
root,
|
||||
});
|
||||
server = await preview({
|
||||
root,
|
||||
});
|
||||
browser = await puppeteer.launch({
|
||||
headless: 'new',
|
||||
args: ['--no-sandbox', '--disable-setuid-sandbox'],
|
||||
});
|
||||
const page = await browser.newPage();
|
||||
await page.goto(server.resolvedUrls.local[0]);
|
||||
await page.waitForFunction('!!window.scrape', { timeout: 5000 });
|
||||
|
||||
// get input media
|
||||
let input: any;
|
||||
if (source.type === 'embed') {
|
||||
input = {
|
||||
url: options.url,
|
||||
id: source.id,
|
||||
};
|
||||
} else if (source.type === 'source') {
|
||||
let media;
|
||||
if (options.type === 'movie') {
|
||||
media = await getMovieMediaDetails(options.tmdbId);
|
||||
} else {
|
||||
media = await getShowMediaDetails(options.tmdbId, options.season, options.episode);
|
||||
}
|
||||
input = {
|
||||
media,
|
||||
id: source.id,
|
||||
};
|
||||
} else {
|
||||
throw new Error('Wrong source input type');
|
||||
}
|
||||
|
||||
return await page.evaluate(
|
||||
async (proxy, type, inp) => {
|
||||
return (window as any).scrape(proxy, type, inp);
|
||||
},
|
||||
config.proxyUrl,
|
||||
source.type,
|
||||
input,
|
||||
);
|
||||
} finally {
|
||||
server?.httpServer.close();
|
||||
await browser?.close();
|
||||
}
|
||||
}
|
||||
|
||||
async function runActualScraping(
|
||||
providerOptions: ProviderMakerOptions,
|
||||
source: MetaOutput,
|
||||
options: CommandLineArguments,
|
||||
): Promise<any> {
|
||||
if (options.fetcher === 'browser') return runBrowserScraping(providerOptions, source, options);
|
||||
const providers = makeProviders(providerOptions);
|
||||
|
||||
if (source.type === 'embed') {
|
||||
|
Reference in New Issue
Block a user