mirror of
https://github.com/movie-web/providers.git
synced 2025-09-13 14:53:24 +00:00
Seperate scraper code from index cli file
This commit is contained in:
@@ -6,7 +6,11 @@ export function getConfig() {
|
|||||||
throw new Error('Missing MOVIE_WEB_TMDB_API_KEY environment variable');
|
throw new Error('Missing MOVIE_WEB_TMDB_API_KEY environment variable');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let proxyUrl: undefined | string = process.env.MOVIE_WEB_PROXY_URL;
|
||||||
|
proxyUrl = !proxyUrl ? undefined : proxyUrl;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
tmdbApiKey,
|
tmdbApiKey,
|
||||||
|
proxyUrl,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@@ -3,13 +3,11 @@
|
|||||||
import { program } from 'commander';
|
import { program } from 'commander';
|
||||||
import dotenv from 'dotenv';
|
import dotenv from 'dotenv';
|
||||||
import { prompt } from 'enquirer';
|
import { prompt } from 'enquirer';
|
||||||
import Spinnies from 'spinnies';
|
|
||||||
|
|
||||||
import { logDeepObject } from '@/dev-cli/logging';
|
import { runScraper } from '@/dev-cli/scraper';
|
||||||
import { getMovieMediaDetails, getShowMediaDetails } from '@/dev-cli/tmdb';
|
import { processOptions } from '@/dev-cli/validate';
|
||||||
import { CommandLineArguments, processOptions } from '@/dev-cli/validate';
|
|
||||||
|
|
||||||
import { MetaOutput, ProviderMakerOptions, getBuiltinEmbeds, getBuiltinSources, makeProviders } from '..';
|
import { getBuiltinEmbeds, getBuiltinSources } from '..';
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
@@ -49,55 +47,6 @@ function joinMediaTypes(mediaTypes: string[] | undefined) {
|
|||||||
return ''; // * Embed sources pass through here too
|
return ''; // * Embed sources pass through here too
|
||||||
}
|
}
|
||||||
|
|
||||||
async function runScraper(providerOptions: ProviderMakerOptions, source: MetaOutput, options: CommandLineArguments) {
|
|
||||||
const spinnies = new Spinnies();
|
|
||||||
const providers = makeProviders(providerOptions);
|
|
||||||
|
|
||||||
if (source.type === 'embed') {
|
|
||||||
spinnies.add('scrape', { text: `Running ${source.name} scraper on ${options.url}` });
|
|
||||||
try {
|
|
||||||
const result = await providers.runEmbedScraper({
|
|
||||||
url: options.url,
|
|
||||||
id: source.id,
|
|
||||||
});
|
|
||||||
spinnies.succeed('scrape', { text: 'Done!' });
|
|
||||||
logDeepObject(result);
|
|
||||||
} catch (error) {
|
|
||||||
let message = 'Unknown error';
|
|
||||||
if (error instanceof Error) {
|
|
||||||
message = error.message;
|
|
||||||
}
|
|
||||||
|
|
||||||
spinnies.fail('scrape', { text: `ERROR: ${message}` });
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
let media;
|
|
||||||
|
|
||||||
if (options.type === 'movie') {
|
|
||||||
media = await getMovieMediaDetails(options.tmdbId);
|
|
||||||
} else {
|
|
||||||
media = await getShowMediaDetails(options.tmdbId, options.season, options.episode);
|
|
||||||
}
|
|
||||||
|
|
||||||
spinnies.add('scrape', { text: `Running ${source.name} scraper on ${media.title}` });
|
|
||||||
try {
|
|
||||||
const result = await providers.runSourceScraper({
|
|
||||||
media,
|
|
||||||
id: source.id,
|
|
||||||
});
|
|
||||||
spinnies.succeed('scrape', { text: 'Done!' });
|
|
||||||
logDeepObject(result);
|
|
||||||
} catch (error) {
|
|
||||||
let message = 'Unknown error';
|
|
||||||
if (error instanceof Error) {
|
|
||||||
message = error.message;
|
|
||||||
}
|
|
||||||
|
|
||||||
spinnies.fail('scrape', { text: `ERROR: ${message}` });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function runQuestions() {
|
async function runQuestions() {
|
||||||
const options = {
|
const options = {
|
||||||
fetcher: 'node-fetch',
|
fetcher: 'node-fetch',
|
||||||
|
63
src/dev-cli/scraper.ts
Normal file
63
src/dev-cli/scraper.ts
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
/* eslint import/no-extraneous-dependencies: ["error", {"devDependencies": true}] */
|
||||||
|
|
||||||
|
import Spinnies from 'spinnies';
|
||||||
|
|
||||||
|
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(
|
||||||
|
providerOptions: ProviderMakerOptions,
|
||||||
|
source: MetaOutput,
|
||||||
|
options: CommandLineArguments,
|
||||||
|
) {
|
||||||
|
const providers = makeProviders(providerOptions);
|
||||||
|
|
||||||
|
if (source.type === 'embed') {
|
||||||
|
return providers.runEmbedScraper({
|
||||||
|
url: options.url,
|
||||||
|
id: source.id,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
return providers.runSourceScraper({
|
||||||
|
media,
|
||||||
|
id: source.id,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error('Invalid source type');
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function runScraper(
|
||||||
|
providerOptions: ProviderMakerOptions,
|
||||||
|
source: MetaOutput,
|
||||||
|
options: CommandLineArguments,
|
||||||
|
) {
|
||||||
|
const spinnies = new Spinnies();
|
||||||
|
|
||||||
|
spinnies.add('scrape', { text: `Running ${source.name} scraper` });
|
||||||
|
try {
|
||||||
|
const result = await runActualScraping(providerOptions, source, options);
|
||||||
|
spinnies.succeed('scrape', { text: 'Done!' });
|
||||||
|
logDeepObject(result);
|
||||||
|
} catch (error) {
|
||||||
|
let message = 'Unknown error';
|
||||||
|
if (error instanceof Error) {
|
||||||
|
message = error.message;
|
||||||
|
}
|
||||||
|
spinnies.fail('scrape', { text: `ERROR: ${message}` });
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user