Added Cookie.ts. Changed request changes.

This commit is contained in:
MemeCornucopia 2024-01-29 13:32:21 -05:00
parent 46253bad0c
commit c8ad3387c5
3 changed files with 37 additions and 11 deletions

View File

@ -0,0 +1,22 @@
export interface Cookie {
name: string;
value: string;
}
export function makeCookieHeader(cookies: Record<string, string>): string {
return Object.entries(cookies)
.map(([name, value]) => `${name}=${value}`)
.join('; ');
}
export function parseSetCookie(headerValue: string): Record<string, Cookie> {
const cookies: Record<string, Cookie> = {};
const parts = headerValue.split(/; */);
for (const part of parts) {
const [name, value] = part.split('=');
if (name && value) {
cookies[name] = { name, value };
}
}
return cookies;
}

View File

@ -1,5 +1,6 @@
import { load } from 'cheerio'; import { load } from 'cheerio';
import { makeCookieHeader, parseSetCookie } from '@/entrypoint/utils/cookie';
import { flags } from '@/entrypoint/utils/targets'; import { flags } from '@/entrypoint/utils/targets';
import { makeEmbed } from '@/providers/base'; import { makeEmbed } from '@/providers/base';
@ -15,7 +16,8 @@ export const wootlyScraper = makeEmbed({
readHeaders: ['Set-Cookie'], readHeaders: ['Set-Cookie'],
}); });
const wootssesCookie = wootlyData.headers.get('Set-Cookie')?.match(/wootsses=([^;]+)/)?.[1]; const cookies = parseSetCookie(wootlyData.headers.get('Set-Cookie') || '');
const wootssesCookie = cookies.wootsses.value;
let $ = load(wootlyData.body); // load the html data let $ = load(wootlyData.body); // load the html data
const iframeSrc = $('iframe').attr('src') ?? ''; const iframeSrc = $('iframe').attr('src') ?? '';
@ -24,17 +26,18 @@ export const wootlyScraper = makeEmbed({
method: 'GET', method: 'GET',
readHeaders: ['Set-Cookie'], readHeaders: ['Set-Cookie'],
headers: { headers: {
cookie: `wootsses=${wootssesCookie};`, cookie: makeCookieHeader({ wootsses: wootssesCookie }),
}, },
}); });
const woozCookie = woozCookieRequest.headers.get('Set-Cookie')?.match(/wooz=([^;]+)/)?.[1]; const woozCookies = parseSetCookie(woozCookieRequest.headers.get('Set-Cookie') || '');
const woozCookie = woozCookies.wooz.value;
const iframeData = await ctx.proxiedFetcher<string>(iframeSrc, { const iframeData = await ctx.proxiedFetcher<string>(iframeSrc, {
method: 'POST', method: 'POST',
body: new URLSearchParams({ qdf: '1' }), body: new URLSearchParams({ qdf: '1' }),
headers: { headers: {
cookie: `wooz=${woozCookie}`, cookie: makeCookieHeader({ wooz: woozCookie }),
Referer: iframeSrc, Referer: iframeSrc,
}, },
}); });
@ -44,7 +47,6 @@ export const wootlyScraper = makeEmbed({
const scriptText = $('script').html() ?? ''; const scriptText = $('script').html() ?? '';
// Regular expressions to match the variables // Regular expressions to match the variables
const tk = scriptText.match(/tk=([^;]+)/)?.[0].replace(/tk=|["\s]/g, ''); const tk = scriptText.match(/tk=([^;]+)/)?.[0].replace(/tk=|["\s]/g, '');
const vd = scriptText.match(/vd=([^,]+)/)?.[0].replace(/vd=|["\s]/g, ''); const vd = scriptText.match(/vd=([^,]+)/)?.[0].replace(/vd=|["\s]/g, '');
@ -55,7 +57,7 @@ export const wootlyScraper = makeEmbed({
query: { t: tk, id: vd }, query: { t: tk, id: vd },
method: 'GET', method: 'GET',
headers: { headers: {
cookie: `wooz=${woozCookie}; wootsses=${wootssesCookie};`, cookie: makeCookieHeader({ wooz: woozCookie, wootsses: wootssesCookie }),
}, },
}); });

View File

@ -1,5 +1,6 @@
import { load } from 'cheerio'; import { load } from 'cheerio';
import { makeCookieHeader, parseSetCookie } from '@/entrypoint/utils/cookie';
import { ScrapeContext } from '@/utils/context'; import { ScrapeContext } from '@/utils/context';
import { EmbedsResult, baseUrl, baseUrl2 } from './type'; import { EmbedsResult, baseUrl, baseUrl2 } from './type';
@ -14,7 +15,8 @@ export async function getEmbeds(ctx: ScrapeContext, id: string): Promise<EmbedsR
method: 'GET', method: 'GET',
}); });
const aGoozCookie = data.headers.get('Set-Cookie')?.match(/aGooz=([^;]+)/)?.[1]; const cookies = parseSetCookie(data.headers.get('Set-Cookie') || '');
const aGoozCookie = cookies.aGooz.value;
const $ = load(data.body); const $ = load(data.body);
const RandomCookieName = data.body.split(`_3chk('`)[1].split(`'`)[0]; const RandomCookieName = data.body.split(`_3chk('`)[1].split(`'`)[0];
@ -31,7 +33,10 @@ export async function getEmbeds(ctx: ScrapeContext, id: string): Promise<EmbedsR
ctx.fetcher ctx.fetcher
.full(url, { .full(url, {
headers: { headers: {
cookie: `aGooz=${aGoozCookie}; ${RandomCookieName}=${RandomCookieValue};`, cookie: makeCookieHeader({
aGooz: aGoozCookie,
[RandomCookieName]: RandomCookieValue,
}),
Referer: baseUrl2, Referer: baseUrl2,
}, },
method: 'GET', method: 'GET',
@ -45,11 +50,8 @@ export async function getEmbeds(ctx: ScrapeContext, id: string): Promise<EmbedsR
// Process each page result // Process each page result
for (const result of embedPages) { for (const result of embedPages) {
// Ensure there's a result to process
if (result) { if (result) {
// Attempt to find a matching embed ID
const embedId = ['wootly', 'upstream', 'mixdrop', 'dood'].find((a) => result.finalUrl.includes(a)); const embedId = ['wootly', 'upstream', 'mixdrop', 'dood'].find((a) => result.finalUrl.includes(a));
// If an embed ID is found, push the result to the results array
if (embedId) { if (embedId) {
results.push({ embedId, url: result.finalUrl }); results.push({ embedId, url: result.finalUrl });
} }