Skip to content

Commit

Permalink
--but and --downloadArchive
Browse files Browse the repository at this point in the history
  • Loading branch information
izu-co authored Nov 21, 2021
2 parents 7beff58 + e133821 commit 0bfbeee
Show file tree
Hide file tree
Showing 9 changed files with 284 additions and 59 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ token.yml
lib
test.*
updates.json
cr_token.yml
cr_token.yml
archive.json
31 changes: 23 additions & 8 deletions crunchy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import { CrunchyEpMeta, CrunchyEpMetaMultiDub, ParseItem, SeriesSearch, SeriesSe
import { ObjectInfo } from './@types/objectInfo';
import parseFileName, { Variable } from './modules/module.filename';
import { PlaybackData } from './@types/playbackData';
import { downloaded } from './modules/module.downloadArchive';
const req = new reqModule.Req(domain, argv);

// select
Expand Down Expand Up @@ -84,7 +85,7 @@ export default (async () => {
else if(argv.series && argv.series.match(/^[0-9A-Z]{9}$/)){
await refreshToken();
await getSeriesById();
await downloadFromSeriesID();
return await downloadFromSeriesID();
}
else if(argv['movie-listing'] && argv['movie-listing'].match(/^[0-9A-Z]{9}$/)){
await refreshToken();
Expand All @@ -94,9 +95,9 @@ export default (async () => {
await refreshToken();
if (argv.dubLang.length > 1) {
console.log('[INFO] One show can only be downloaded with one dub. Use --srz instead.');
argv.dubLang = argv.dubLang[0];
}
await getSeasonById();
argv.dubLang = argv.dubLang[0];
return await getSeasonById();
}
else if(argv.e){
await refreshToken();
Expand Down Expand Up @@ -699,7 +700,7 @@ async function getSeasonById(){
? 'S' + epNumList.sp.toString().padStart(epNumLen['S'], '0')
: '' + parseInt(epNum, 10).toString().padStart(epNumLen['E'], '0')
);
if(selEps.indexOf(selEpId) > -1 && !item.isSelected && item.playback){
if((argv.but && item.playback && selEps.indexOf(selEpId) == -1) || (argv.all && item.playback) || (!argv.but && selEps.indexOf(selEpId) > -1 && !item.isSelected && item.playback)){
selectedMedia.push(epMeta);
item.isSelected = true;
}
Expand All @@ -719,10 +720,18 @@ async function getSeasonById(){
}

console.log();
let ok = true;
for(const media of selectedMedia){
await getMedia(media);
if (await getMedia(media) !== true) {
ok = false;
} else {
downloaded({
service: 'crunchy',
type: 's'
}, argv.s as string, [media.episodeNumber]);
}
}
return ok;
}

async function getObjectById(returnData?: boolean){
Expand Down Expand Up @@ -1164,7 +1173,8 @@ async function getMedia(mMeta: CrunchyEpMeta){
else{
console.log();
}


return !dlFailed;
}

async function muxStreams(options: MergerOptions){
Expand Down Expand Up @@ -1247,6 +1257,10 @@ const downloadFromSeriesID = async () => {
const res = await getMediaList(item);
if (!res)
return;
downloaded({
service: 'crunchy',
type: 'srz'
}, argv.series as string, [item.episodeNumber]);
muxStreams({
onlyAudio: [],
onlyVid: [],
Expand All @@ -1262,6 +1276,7 @@ const downloadFromSeriesID = async () => {
fonts: Merger.makeFontsList(cfg.dir.fonts, appstore.sxList)
});
}
return true;
};

const itemSelectMultiDub = (eps: Record<string, {
Expand Down Expand Up @@ -1315,7 +1330,7 @@ const itemSelectMultiDub = (eps: Record<string, {
? 'S' + epNumList.sp.toString().padStart(epNumLen['S'], '0')
: '' + parseInt(epNum, 10).toString().padStart(epNumLen['E'], '0')
);
if(selEps.indexOf(selEpId) > -1 && item.playback){
if((argv.but && selEps.indexOf(selEpId) == -1) || (argv.all || (selEps.indexOf(selEpId) > -1) && item.playback && !argv.but)){
if (Object.prototype.hasOwnProperty.call(ret, key)) {
const epMe = ret[key];
epMe.data.push({
Expand Down
62 changes: 42 additions & 20 deletions funi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const cfg = yamlCfg.loadCfg();
const token = yamlCfg.loadFuniToken();
// cli
const argv = appYargs.appArgv(cfg.cli);

// Import modules after argv has been exported
import getData from './modules/module.getdata.js';
import merger, { SubtitleInput } from './modules/module.merger';
Expand All @@ -36,6 +35,7 @@ import { Subtitle } from './@types/subtitleObject';
import { StreamData } from './@types/streamData';
import { DownloadedFile } from './@types/downloadedFile';
import parseFileName, { Variable } from './modules/module.filename';
import { downloaded } from './modules/module.downloadArchive';

// check page
argv.p = 1;
Expand All @@ -47,9 +47,9 @@ let title = '',
fnOutput: string[] = [],
season = 0,
tsDlPath: {
path: string,
lang: string,
}[] = [],
path: string,
lang: string
}[] = [],
stDlPath: Subtitle[] = [];

// main
Expand All @@ -71,7 +71,7 @@ export default (async () => {
searchShow();
}
else if(argv.s && !isNaN(parseInt(argv.s)) && parseInt(argv.s) > 0){
getShow();
return getShow();
}
else{
appYargs.showHelp();
Expand Down Expand Up @@ -134,7 +134,7 @@ async function searchShow(){

// get show
async function getShow(){
// show main data
let ok = true;
const showData = await getData({
baseUrl: api_host,
url: `/source/catalog/title/${argv.s}`,
Expand Down Expand Up @@ -209,11 +209,12 @@ async function getShow(){
return e;
});

const epSelList = parseSelect(argv.e as string);
const epSelList = parseSelect(argv.e as string, argv.but);

const fnSlug: {
title: string,
episode: string
episode: string,
episodeID: string
}[] = []; let is_selected = false;

const eps = epsDataArr;
Expand All @@ -233,7 +234,7 @@ async function getShow(){
// select
is_selected = false;
if (argv.all || epSelList.isSelected(epStrId)) {
fnSlug.push({title:eps[e].item.titleSlug,episode:eps[e].item.episodeSlug});
fnSlug.push({title:eps[e].item.titleSlug,episode:eps[e].item.episodeSlug, episodeID:epStrId});
epSelEpsTxt.push(epStrId);
is_selected = true;
}
Expand Down Expand Up @@ -265,15 +266,17 @@ async function getShow(){
else{
console.log('[INFO] Selected Episodes: %s\n',epSelEpsTxt.join(', '));
for(let fnEp=0;fnEp<fnSlug.length;fnEp++){
await getEpisode(fnSlug[fnEp]);
if (await getEpisode(fnSlug[fnEp]) !== true)
ok = false;
}
}
return ok;
}

async function getEpisode(fnSlug: {
title: string,
episode: string
title: string,
episode: string,
episodeID: string
}) {
const episodeData = await getData({
baseUrl: api_host,
Expand Down Expand Up @@ -321,7 +324,7 @@ async function getEpisode(fnSlug: {
language: m.language,
version: m.version,
type: m.experienceType,
subtitles: getSubsUrl(m.mediaChildren),
subtitles: getSubsUrl(m.mediaChildren)
};
}
else{
Expand Down Expand Up @@ -352,6 +355,7 @@ async function getEpisode(fnSlug: {
for (const curDub of (argv.dub as appYargs.possibleDubs)) {
if(dub_type == dubType[curDub] && selUncut){
streamIds.push({

id: m.id,
lang: merger.getLanguageCode(curDub, curDub.slice(0, -2))
});
Expand Down Expand Up @@ -413,7 +417,7 @@ async function getEpisode(fnSlug: {
return;
}
else{
await downloadStreams();
return await downloadStreams(fnSlug.episodeID);
}
}
}
Expand Down Expand Up @@ -457,7 +461,7 @@ function getSubsUrl(m: MediaChild[]) : Subtitle[] {
return found;
}

async function downloadStreams(){
async function downloadStreams(epsiodeID: string){

// req playlist

Expand Down Expand Up @@ -713,14 +717,18 @@ async function downloadStreams(){
if (addSubs)
console.log('[INFO] Subtitles downloaded!');
}

if((puraudio.length < 1 && audioAndVideo.length < 1) || (purvideo.length < 1 && audioAndVideo.length < 1)){
console.log('\n[INFO] Unable to locate a video AND audio file\n');
return;
}

if(argv.skipmux){
console.log('[INFO] Skipping muxing...');
downloaded({
service: 'funi',
type: 's'
}, argv.s as string, [epsiodeID]);
return;
}

Expand Down Expand Up @@ -759,14 +767,28 @@ async function downloadStreams(){
}
else{
console.log('\n[INFO] Done!\n');
return;
downloaded({
service: 'funi',
type: 's'
}, argv.s as string, [epsiodeID]);
return true;
}
if (argv.nocleanup) {
downloaded({
service: 'funi',
type: 's'
}, argv.s as string, [epsiodeID]);
return true;
}
if (argv.nocleanup)
return;

audioAndVideo.concat(puraudio).concat(purvideo).forEach(a => fs.unlinkSync(a.path));
stDlPath.forEach(subObject => subObject.file && fs.unlinkSync(subObject.file));
console.log('\n[INFO] Done!\n');
downloaded({
service: 'funi',
type: 's'
}, argv.s as string, [epsiodeID]);
return true;
}

async function downloadFile(filename: string, chunkList: {
Expand Down
48 changes: 43 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { appArgv } from './modules/module.app-args';
import { appArgv, overrideArguments } from './modules/module.app-args';
import * as yamlCfg from './modules/module.cfg-loader';
import { makeCommand, addToArchive } from './modules/module.downloadArchive';

import update from './modules/module.updater';

Expand All @@ -10,10 +11,47 @@ import update from './modules/module.updater';

await update(argv.update);

if (argv.service === 'funi') {
(await import('./funi')).default();
} else if (argv.service === 'crunchy') {
(await import('./crunchy')).default();
if (argv.all && argv.but) {
console.log('[ERROR] --all and --but exclude each other!');
return;
}

if (argv.addArchive) {
if (argv.service === 'funi') {
if (argv.s === undefined)
return console.log('[ERROR] `-s` not found');
addToArchive({
service: 'funi',
type: 's'
}, argv.s);
console.log('[INFO] Added %s to the downloadArchive list', argv.s);
} else if (argv.service === 'crunchy') {
if (argv.s === undefined && argv.series === undefined)
return console.log('[ERROR] `-s` or `--srz` not found');
if (argv.s && argv.series)
return console.log('[ERROR] Both `-s` and `--srz` found');
addToArchive({
service: 'crunchy',
type: argv.s === undefined ? 'srz' : 's'
}, (argv.s === undefined ? argv.series : argv.s) as string);
console.log('[INFO] Added %s to the downloadArchive list', (argv.s === undefined ? argv.series : argv.s));
}
} else if (argv.downloadArchive) {
const ids = makeCommand(argv.service);
for (const id of ids) {
overrideArguments(cfg.cli, id);
/* Reimport module to override appArgv */
Object.keys(require.cache).forEach(key => {
if (key.endsWith('crunchy.js') || key.endsWith('funi.js'))
delete require.cache[key];
});
await (argv.service === 'funi' ? await import('./funi') : await import('./crunchy')).default();
}
} else {
if (argv.service === 'funi') {
(await import('./funi')).default();
} else if (argv.service === 'crunchy') {
(await import('./crunchy')).default();
}
}
})();
Loading

0 comments on commit 0bfbeee

Please sign in to comment.