Fix catflix

This commit is contained in:
Ivan Evans 2025-01-02 11:09:16 -07:00
parent 30560e46a3
commit 4021fe8aa6

View file

@ -1,56 +1,64 @@
import { load } from 'cheerio';
import { SourcererOutput, makeSourcerer } from '@/providers/base';
import { compareMedia } from '@/utils/compare';
import { MovieScrapeContext, ShowScrapeContext } from '@/utils/context';
import { NotFoundError } from '@/utils/errors';
const baseUrl = 'https://catflix.su';
function decodeBase64(encodedString: string): string {
const decodedString = atob(encodedString);
return decodedString;
}
async function comboScraper(ctx: ShowScrapeContext | MovieScrapeContext): Promise<SourcererOutput> {
const searchPage = await ctx.proxiedFetcher('/', {
baseUrl,
query: {
s: ctx.media.title,
},
});
const movieId = ctx.media.tmdbId;
const mediaTitle = ctx.media.title.replace(/ /g, '-').replace(/[():]/g, '').toLowerCase();
let watchPageUrl: string | undefined;
ctx.progress(40);
if (ctx.media.type === 'movie') {
watchPageUrl = `${baseUrl}/movie/${mediaTitle}-${movieId}`;
} else if (ctx.media.type === 'show') {
const seasonNumber = ctx.media.season.number;
const episodeNumber = ctx.media.episode.number;
const episodeId = ctx.media.episode.tmdbId;
const $search = load(searchPage);
const searchResults: { title: string; year?: number | undefined; url: string }[] = [];
if (!episodeId) {
throw new Error('Missing episode ID for show');
}
$search('li').each((_, element) => {
const title = $search(element).find('h2').first().text().trim();
// the year is always present, but I sitll decided to make it nullable since the impl isn't as future-proof
const year = Number($search(element).find('.text-xs > span').eq(1).text().trim()) || undefined;
const url = $search(element).find('a').attr('href');
if (!title || !url) return;
searchResults.push({ title, year, url });
});
let watchPageUrl = searchResults.find((x) => x && compareMedia(ctx.media, x.title, x.year))?.url;
if (!watchPageUrl) throw new NotFoundError('No watchable item found');
watchPageUrl = `${baseUrl}/episode/${mediaTitle}-season-${seasonNumber}-episode-${episodeNumber}/eid-${episodeId}`;
}
if (!watchPageUrl) {
throw new Error('Failed to generate watch page URL');
}
ctx.progress(60);
if (ctx.media.type === 'show') {
const match = watchPageUrl.match(/\/series\/([^/]+)\/?/);
if (!match) throw new Error('Failed to parse watch page url');
watchPageUrl = watchPageUrl.replace(
`/series/${match[1]}`,
`/episode/${match[1]}-${ctx.media.season.number}x${ctx.media.episode.number}`,
);
const WatchPage = await ctx.proxiedFetcher(watchPageUrl);
const $WatchPage = load(WatchPage);
const scriptContent = $WatchPage('script')
.toArray()
.find((script) => {
return (
script.children[0] && script.children[0].type === 'text' && script.children[0].data.includes('main_origin =')
);
});
if (!scriptContent) {
throw new Error('Script containing main_origin not found');
}
const watchPage = load(await ctx.proxiedFetcher(watchPageUrl));
const mainOriginScript = scriptContent.children[0].type === 'text' ? scriptContent.children[0].data : '';
const mainOriginMatch = mainOriginScript.match(/main_origin = "(.*?)";/);
ctx.progress(80);
if (!mainOriginMatch) {
throw new Error('Unable to extract main_origin value');
}
const url = watchPage('iframe').first().attr('src'); // I couldn't think of a better way
if (!url) throw new Error('Failed to find embed url');
const Catflix1 = mainOriginMatch[1];
const decodedUrl = decodeBase64(Catflix1);
ctx.progress(90);
@ -58,7 +66,7 @@ async function comboScraper(ctx: ShowScrapeContext | MovieScrapeContext): Promis
embeds: [
{
embedId: 'turbovid',
url,
url: decodedUrl,
},
],
};
@ -69,6 +77,7 @@ export const catflixScraper = makeSourcerer({
name: 'Catflix',
rank: 122,
flags: [],
disabled: false,
scrapeMovie: comboScraper,
scrapeShow: comboScraper,
});