From e85f9893ac00381e3065d0587b6128be73fa6a39 Mon Sep 17 00:00:00 2001 From: Durgesh Kumar Dwivedi Date: Sat, 21 Sep 2024 19:13:17 +0530 Subject: [PATCH] feat(streamwish): Refactor StreamWish decoding to get proper hls urls --- src/extractors/streamwish.ts | 38 ++++++++++++++++++++++++++++---- src/providers/anime/gogoanime.ts | 2 +- test/anime/gogoanime.test.ts | 3 ++- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/extractors/streamwish.ts b/src/extractors/streamwish.ts index 0674c81d8..be713d665 100644 --- a/src/extractors/streamwish.ts +++ b/src/extractors/streamwish.ts @@ -24,22 +24,52 @@ class StreamWish extends VideoExtractor { 'User-Agent': USER_AGENT, }, }; + // console.log(videoUrl.href,"videoUrl") const { data } = await this.client.get(videoUrl.href, options); - const links = data.match(/file:\s*"([^"]+)"/); - let lastLink = null; + + // Code adapted from Zenda-Cross (https://github.com/Zenda-Cross/vega-app/blob/main/src/lib/providers/multi/multiGetStream.ts) + // Thank you to Zenda-Cross for the original implementation. + + var functionRegex = + /eval\(function\((.*?)\)\{.*?return p\}.*?\('(.*?)'\.split/; + var match = functionRegex.exec(data); + let p = ''; + if (match) { + var params = match[1].split(',').map(param => param.trim()); + var encodedString = match[2]; + + p = encodedString.split("',36,")?.[0].trim(); + let a = 36; + let c = encodedString.split("',36,")[1].slice(2).split('|').length; + let k = encodedString.split("',36,")[1].slice(2).split('|'); + + while (c--) { + if (k[c]) { + var regex = new RegExp('\\b' + c.toString(a) + '\\b', 'g'); + p = p.replace(regex, k[c]); + } + } + + // console.log('Decoded String:', p); + } else { + console.log('No match found'); + } + const links = p.match(/file:\s*"([^"]+)"/) ?? []; + // console.log(links, "links"); + let lastLink: string | null = null; links.forEach((link: string) => { if (link.includes('file:"')) { link = link.replace('file:"', '').replace(new RegExp('"', 'g'), ''); } this.sources.push({ quality: lastLink! ? 'backup' : 'default', - url: link, + url: link.replace(/&i=\d+,'\.4&/, '&i=0.4&'), isM3U8: link.includes('.m3u8'), }); lastLink = link; }); - const m3u8Content = await this.client.get(links[1], options); + const m3u8Content = await this.client.get(links[1].replace(/&i=\d+,'\.4&/, '&i=0.4&'), options); if (m3u8Content.data.includes('EXTM3U')) { const videoList = m3u8Content.data.split('#EXT-X-STREAM-INF:'); diff --git a/src/providers/anime/gogoanime.ts b/src/providers/anime/gogoanime.ts index a5c85a2de..567304e23 100644 --- a/src/providers/anime/gogoanime.ts +++ b/src/providers/anime/gogoanime.ts @@ -583,7 +583,7 @@ class Gogoanime extends AnimeParser { // (async () => { // const gogo = new Gogoanime(); -// const search = await gogo.fetchEpisodeSources('jigokuraku-dub-episode-1'); +// const search = await gogo.fetchEpisodeSources('jigokuraku-dub-episode-1',StreamingServers.StreamWish); // console.log(search); // })(); diff --git a/test/anime/gogoanime.test.ts b/test/anime/gogoanime.test.ts index aba7315e2..ab37c25da 100644 --- a/test/anime/gogoanime.test.ts +++ b/test/anime/gogoanime.test.ts @@ -1,5 +1,6 @@ import { info } from 'console'; import { ANIME } from '../../src/providers'; +import { StreamingServers } from '../../src/models'; jest.setTimeout(120000); @@ -26,7 +27,7 @@ test('returns a filled array of servers', async () => { }); test('returns a filled object of episode sources', async () => { - const data = await gogoanime.fetchEpisodeSources('spy-x-family-episode-9'); + const data = await gogoanime.fetchEpisodeSources('spy-x-family-episode-9',StreamingServers.StreamWish); expect(data.sources).not.toEqual([]); });