All the Changes Requested Complete

This commit is contained in:
MemeCornucopia
2023-12-07 20:53:49 -05:00
parent 283b569b7c
commit 38cdb1313f
5 changed files with 1306 additions and 6690 deletions

View File

@@ -5,11 +5,11 @@ import { scrape, searchAndFindMedia } from './util';
import { MovieContext, ShowContext } from '../zoechip/common';
async function universalScraper(ctx: ShowContext | MovieContext): Promise<SourcererOutput> {
const lookmovieData = await searchAndFindMedia(ctx.media);
const lookmovieData = await searchAndFindMedia(ctx, ctx.media);
if (!lookmovieData) throw new NotFoundError('Media not found');
ctx.progress(30);
const videoUrl = await scrape(ctx.media, lookmovieData);
const videoUrl = await scrape(ctx, ctx.media, lookmovieData);
if (!videoUrl) throw new NotFoundError('No video found');
ctx.progress(60);

View File

@@ -1,52 +1,56 @@
import json5 from 'json5';
import { MovieMedia, ShowMedia } from '@/main/media';
import { compareMedia } from '@/utils/compare';
import { ScrapeContext } from '@/utils/context';
import { NotFoundError } from '@/utils/errors';
import { Result } from './type';
import { getVideoUrl } from './video';
export async function searchAndFindMedia(media: MovieMedia | ShowMedia): Promise<Result | undefined> {
if (media.type === "show") {
const searchRes = await fetch(`https://lmscript.xyz/v1/shows?filters[q]=${encodeURIComponent(media.title)}`).then((d) => d.json());
export async function searchAndFindMedia(
ctx: ScrapeContext,
media: MovieMedia | ShowMedia,
): Promise<Result | undefined> {
if (media.type === 'show') {
const searchRes = await ctx.fetcher<any>(`/v1/shows?filters[q]=${media.title}`, {
baseUrl: 'https://lmscript.xyz',
});
const results = searchRes.items;
const result = results.find((res: Result) => compareMedia(media, res.title, Number(res.year)));
return result;
} else if (media.type === "movie") {
const searchRes = await fetch(`https://lmscript.xyz/v1/movies?filters[q]=${encodeURIComponent(media.title)}`).then((d) => d.json());
}
if (media.type === 'movie') {
const searchRes = await ctx.fetcher<any>(`/v1/movies?filters[q]=${media.title}`, {
baseUrl: 'https://lmscript.xyz',
});
const results = searchRes.items;
const result = results.find((res: Result) => compareMedia(media, res.title, Number(res.year)));
return result;
}
}
export async function scrape(media: MovieMedia | ShowMedia, result: Result) {
export async function scrape(ctx: ScrapeContext, media: MovieMedia | ShowMedia, result: Result) {
// Find the relevant id
let id = null;
if (media.type === 'movie') {
id = result.id_movie;
} else if (media.type === 'show') {
const data = await fetch(`https://lmscript.xyz/v1/shows?expand=episodes&id=${result.id_show}`).then((d) => d.json());
const data: any = await ctx
.fetcher<any>(`https://lmscript.xyz/v1/shows?expand=episodes&id=${result.id_show}`)
.then((d) => d);
const episodeObj = data.episodes?.find((v: any) => {
return Number(v.season) === Number(media.season.number) && Number(v.episode) === Number(media.episode.number);
});
if (episodeObj) id = episodeObj.id;
}
// Check ID
if (id === null) throw new NotFoundError('Not found');
const videoUrl = await getVideoUrl(id,media);
const videoUrl = await getVideoUrl(ctx, id, media);
return videoUrl;
}

View File

@@ -1,7 +1,7 @@
import { Config } from './type';
import { MovieMedia, ShowMedia } from '@/main/media';
import { ScrapeContext } from '@/utils/context';
export async function getVideoSources(id:any,media: MovieMedia | ShowMedia): Promise<any> {
export async function getVideoSources(ctx: ScrapeContext, id: any, media: MovieMedia | ShowMedia): Promise<any> {
// Fetch video sources
let url = '';
@@ -10,17 +10,17 @@ export async function getVideoSources(id:any,media: MovieMedia | ShowMedia): Pro
} else if (media.type === 'movie') {
url = `https://lmscript.xyz/v1/movies/view?expand=streams&id=${id}`;
}
const data = await fetch(url).then((d) => d.json());
const data = await ctx.fetcher<any>(url).then((d) => d);
return data;
}
export async function getVideoUrl(id:any,media: MovieMedia | ShowMedia): Promise<string | null> {
export async function getVideoUrl(ctx: ScrapeContext, id: any, media: MovieMedia | ShowMedia): Promise<string | null> {
// Get sources
const data = await getVideoSources(id,media);
const data = await getVideoSources(ctx, id, media);
const videoSources = data.streams;
// Find video URL and return it
const opts = ['1080p', '1080', '720p', '720', '480p', '480','240p','240','360p','360',"144","144p", 'auto'];
const opts = ['auto', '1080p', '1080', '720p', '720', '480p', '480', '240p', '240', '360p', '360', '144', '144p'];
let videoUrl: string | null = null;
for (const res of opts) {