Skip to content

Commit

Permalink
[CR] Add crunchy play streams selector
Browse files Browse the repository at this point in the history
  • Loading branch information
AnimeDL committed Jun 20, 2024
1 parent 16dbc4f commit c294cdc
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 23 deletions.
2 changes: 2 additions & 0 deletions @types/crunchyTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { HLSCallback } from 'hls-download';
import { sxItem } from '../crunchy';
import { LanguageItem } from '../modules/module.langsData';
import { DownloadInfo } from './messageHandler';
import { CrunchyPlayStreams } from './enums';

export type CrunchyDownloadOptions = {
hslang: string,
kstream: number,
cpstream: keyof typeof CrunchyPlayStreams | 'none',
novids?: boolean,
noaudio?: boolean,
x: number,
Expand Down
16 changes: 16 additions & 0 deletions @types/enums.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export enum CrunchyPlayStreams {
'chrome' = 'web/chrome',
'firefox' = 'web/firefox',
'safari' = 'web/safari',
'edge' = 'web/edge',
'fallback' = 'web/fallback',
'ps4' = 'console/ps4',
'ps5' = 'console/ps5',
'switch' = 'console/switch',
'samsungtv' = 'tv/samsung',
'lgtv' = 'tv/lg',
'rokutv' = 'tv/roku',
'android' = 'android/phone',
'iphone' = 'ios/iphone',
'ipad' = 'ios/ipad',
}
46 changes: 25 additions & 21 deletions crunchy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import { CrunchyAndroidObject } from './@types/crunchyAndroidObject';
import { CrunchyChapters, CrunchyChapter, CrunchyOldChapter } from './@types/crunchyChapters';
import vtt2ass from './modules/module.vtt2ass';
import { CrunchyPlayStream } from './@types/crunchyPlayStreams';
import { CrunchyPlayStreams } from './@types/enums';

export type sxItem = {
language: langsData.LanguageItem,
Expand Down Expand Up @@ -1414,27 +1415,30 @@ export default class Crunchy implements ServiceClass {
pbData = await playbackReq.res.json() as PlaybackData;
}

let switchStream: CrunchyPlayStream | null = null;
const playbackReq = await this.req.getData(`https://cr-play-service.prd.crunchyrollsvc.com/v1/${currentVersion ? currentVersion.guid : currentMediaId}/console/switch/play`, AuthHeaders);
if(!playbackReq.ok || !playbackReq.res) {
console.error('Non-DRM Request Stream URLs FAILED!');
} else {
switchStream = await playbackReq.res.json() as CrunchyPlayStream;
const derivedPlaystreams = {} as CrunchyStreams;
for (const hardsub in switchStream.hardSubs) {
const stream = switchStream.hardSubs[hardsub];
derivedPlaystreams[hardsub] = {
url: stream.url,
'hardsub_locale': stream.hlang

let playStream: CrunchyPlayStream | null = null;
if (options.cpstream !== 'none') {
const playbackReq = await this.req.getData(`https://cr-play-service.prd.crunchyrollsvc.com/v1/${currentVersion ? currentVersion.guid : currentMediaId}/${CrunchyPlayStreams[options.cpstream]}/play`, AuthHeaders);
if (!playbackReq.ok || !playbackReq.res) {
console.error('Non-DRM Request Stream URLs FAILED!');
} else {
playStream = await playbackReq.res.json() as CrunchyPlayStream;
const derivedPlaystreams = {} as CrunchyStreams;
for (const hardsub in playStream.hardSubs) {
const stream = playStream.hardSubs[hardsub];
derivedPlaystreams[hardsub] = {
url: stream.url,
'hardsub_locale': stream.hlang
};
}
derivedPlaystreams[''] = {
url: playStream.url,
hardsub_locale: ''
};
pbData.data[0][`adaptive_${options.cpstream}_${playStream.url.includes('m3u8') ? 'hls' : 'dash'}_drm`] = {
...derivedPlaystreams
};
}
derivedPlaystreams[''] = {
url: switchStream.url,
hardsub_locale: ''
};
pbData.data[0]['adaptive_switch_dash'] = {
...derivedPlaystreams
};
}

variables.push(...([
Expand Down Expand Up @@ -2131,9 +2135,9 @@ export default class Crunchy implements ServiceClass {
console.info('Subtitles downloading skipped!');
}

if (switchStream) {
if (playStream) {
await this.refreshToken(true, true);
await this.req.getData(`https://cr-play-service.prd.crunchyrollsvc.com/v1/token/${currentVersion ? currentVersion.guid : currentMediaId}/${switchStream.token}`, {...{method: 'DELETE'}, ...AuthHeaders});
await this.req.getData(`https://cr-play-service.prd.crunchyrollsvc.com/v1/token/${currentVersion ? currentVersion.guid : currentMediaId}/${playStream.token}`, {...{method: 'DELETE'}, ...AuthHeaders});
}

await this.sleep(options.waittime);
Expand Down
4 changes: 3 additions & 1 deletion modules/module.app-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { DownloadInfo } from '../@types/messageHandler';
import { HLSCallback } from './hls-download';
import leven from 'leven';
import { console } from './log';
import { CrunchyPlayStreams } from '../@types/enums';

let argvC: {
[x: string]: unknown;
Expand Down Expand Up @@ -42,7 +43,8 @@ let argvC: {
extid: string | undefined;
q: number;
x: number;
kstream: number;
kstream: number;
cpstream: keyof typeof CrunchyPlayStreams | 'none';
partsize: number;
hslang: string;
dlsubs: string[];
Expand Down
15 changes: 15 additions & 0 deletions modules/module.args.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { aoSearchLocales, dubLanguageCodes, languages, searchLocales, subtitleLanguagesFilter } from './module.langsData';
import { CrunchyPlayStreams } from '../@types/enums';

const groups = {
'auth': 'Authentication:',
Expand Down Expand Up @@ -283,6 +284,20 @@ const args: TAppArg<boolean|number|string|unknown[]>[] = [
type: 'number',
usage: '${stream}'
},
{
name: 'cpstream',
group: 'dl',
alias: 'cps',
service: ['crunchy'],
type: 'string',
describe: 'Select specific crunchy play stream by device, or disable stream with "none"',
choices: [...Object.keys(CrunchyPlayStreams), 'none'],
default: {
default: 'chrome'
},
docDescribe: true,
usage: '${device}'
},
{
name: 'hslang',
group: 'dl',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "multi-downloader-nx",
"short_name": "aniDL",
"version": "5.0.4b1",
"version": "5.0.4b2",
"description": "Downloader for Crunchyroll, Hidive, AnimeOnegai, and AnimationDigitalNetwork with CLI and GUI",
"keywords": [
"download",
Expand Down

0 comments on commit c294cdc

Please sign in to comment.