diff --git a/package.json b/package.json index 10c3cb9..b396215 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "author": "Godzil", - "description": "Crunchy.js is a fork of Crunchyroll.js, capable of downloading anime episodes from the popular CrunchyRoll streaming service.", + "description": "Crunchy is a fork of Crunchyroll.js, capable of downloading anime episodes from the popular CrunchyRoll streaming service.", "license": "MIT", "keywords": [ "anime", diff --git a/src/episode.ts b/src/episode.ts index 9a86472..897cae9 100644 --- a/src/episode.ts +++ b/src/episode.ts @@ -7,6 +7,7 @@ import path = require('path'); import subtitle from './subtitle/index'; import video from './video/index'; import xml2js = require('xml2js'); +import log = require('./log'); /** * Streams the episode to disk. @@ -24,12 +25,12 @@ export default function(config: IConfig, address: string, done: (err: Error, ign /** * Completes a download and writes the message with an elapsed time. */ -function complete(message: string, begin: number, done: (err: Error, ign: boolean) => void) { +function complete(epName: string, message: string, begin: number, done: (err: Error, ign: boolean) => void) { var timeInMs = Date.now() - begin; var seconds = prefix(Math.floor(timeInMs / 1000) % 60, 2); var minutes = prefix(Math.floor(timeInMs / 1000 / 60) % 60, 2); var hours = prefix(Math.floor(timeInMs / 1000 / 60 / 60), 2); - console.log(message + ' (' + hours + ':' + minutes + ':' + seconds + ')'); + log.dispEpisode(epName, message + ' (' + hours + ':' + minutes + ':' + seconds + ')', true); done(null, false); } @@ -57,14 +58,14 @@ function download(config: IConfig, page: IEpisodePage, player: IEpisodePlayer, d if (fileExist(filePath + ".mkv")) { var count = 0; - console.info("File '"+fileName+"' already exist..."); + log.warn("File '"+fileName+"' already exist..."); do { count = count + 1; fileName = name(config, page, series, "-" + count).replace("/","_").replace("'","_").replace(":","_"); filePath = path.join(config.output || process.cwd(), series, fileName); } while(fileExist(filePath + ".mkv")) - console.info("Renaming to '"+fileName+"'..."); + log.warn("Renaming to '"+fileName+"'..."); } mkdirp(path.dirname(filePath), (err: Error) => { @@ -74,20 +75,20 @@ function download(config: IConfig, page: IEpisodePage, player: IEpisodePlayer, d var now = Date.now(); if (player.video.file != undefined) { - console.log('Fetching ' + fileName); + log.dispEpisode(fileName, 'Fetching...', false); downloadVideo(config, page, player, filePath, err => { if (err) return done(err, false); - if (config.merge) return complete('Finished ' + fileName, now, done); + if (config.merge) return complete(fileName, 'Finished!', now, done); var isSubtited = Boolean(player.subtitle); video.merge(config, isSubtited, player.video.file, filePath, player.video.mode, err => { if (err) return done(err, false); - complete('Finished ' + fileName, now, done); + complete(fileName, 'Finished!', now, done); }); }); } else { - console.log('Ignoring ' + fileName + ': not released yet'); + log.dispEpisode(fileName, 'Ignoring: not released yet', true); done(null, true); } }); @@ -166,8 +167,8 @@ function scrapePage(config: IConfig, address: string, done: (err: Error, page?: if (!swf || !data) { - console.info('Something wrong in the page at '+address+' (data are: '+look+')'); - console.info('Setting Season to 0 and episode to \’0\’...'); + log.warn('Something wrong in the page at '+address+' (data are: '+look+')'); + log.warn('Setting Season to 0 and episode to \’0\’...'); done(null, { id: id, episode: "0", diff --git a/src/log.ts b/src/log.ts new file mode 100644 index 0000000..13b68c5 --- /dev/null +++ b/src/log.ts @@ -0,0 +1,71 @@ +'use strict'; +import os = require('os'); + +export function error(str: string) +{ + if (os.platform() === 'win32') + { + console.log(' * ERROR: ' + str); + } + else + { + /* Do more fancy output */ + console.error(' \x1B[1;31m* ERROR\x1B[0m: ' + str); + } +} + +export function info(str: string) +{ + if (os.platform() === 'win32') + { + console.log(' * INFO : ' + str); + } + else + { + /* Do more fancy output */ + console.log(' \x1B[1;32m* INFO \x1B[0m: ' + str); + } +} + +export function debug(str: string) +{ + if (os.platform() === 'win32') + { + console.log(' * DEBUG: ' + str); + } + else + { + /* Do more fancy output */ + console.log(' \x1B[1;35m* DEBUG\x1B[0m: ' + str); + } +} + +export function warn(str: string) +{ + if (os.platform() === 'win32') + { + console.log(' * WARN : ' + str); + } + else + { + /* Do more fancy output */ + console.log(' \x1B[1;33m* WARN \x1B[0m: ' + str); + } +} + +export function dispEpisode(name: string, status: string, addNL: boolean) +{ + if (os.platform() === 'win32') + { + process.stdout.write(' > ' + name + ' : ' + status + '\x1B[0G'); + } + else + { + /* Do more fancy output */ + process.stdout.write(' \x1B[1;33m> \x1B[37m' + name + '\x1B[0m : \x1B[33m' + status + '\x1B[0m\x1B[0G'); + } + if (addNL) + { + console.log(''); + } +} \ No newline at end of file diff --git a/src/request.ts b/src/request.ts index 306ad98..df795a9 100644 --- a/src/request.ts +++ b/src/request.ts @@ -1,6 +1,7 @@ 'use strict'; import request = require('request'); import cheerio = require('cheerio'); +import log = require('./log'); var isAuthenticated = false; var isPremium = false; @@ -80,8 +81,8 @@ function authenticate(config: IConfig, done: (err: Error) => void) { var error = $('ul.message, li.error').text(); return done(new Error('Authentication failed: ' + error)); } - if (isPremium === false) { console.log('Do not use this app without a premium account.'); } - else { console.log('You have a premium account! Good!'); } + if (isPremium === false) { log.warn('Do not use this app without a premium account.'); } + else { log.info('You have a premium account! Good!'); } done(null); }); }); diff --git a/src/series.ts b/src/series.ts index 16fc584..3830cf9 100644 --- a/src/series.ts +++ b/src/series.ts @@ -5,6 +5,7 @@ import fs = require('fs'); import request = require('./request'); import path = require('path'); import url = require('url'); +import log = require('./log'); var persistent = '.crpersistent'; /** @@ -84,6 +85,7 @@ function page(config: IConfig, address: string, done: (err: Error, result?: ISer var $ = cheerio.load(result); var title = $('span[itemprop=name]').text(); if (!title) return done(new Error('Invalid page.(' + address + ')')); + log.info("Checking availability for " + title); var episodes: ISeriesEpisode[] = []; $('.episode').each((i, el) => { if ($(el).children('img[src*=coming_soon]').length) return; diff --git a/src/video/stream.ts b/src/video/stream.ts index 0bfc858..4cf9a15 100644 --- a/src/video/stream.ts +++ b/src/video/stream.ts @@ -2,6 +2,7 @@ import childProcess = require('child_process'); import path = require('path'); import os = require('os'); +import log = require('../log'); /** * Streams the video to disk. @@ -19,7 +20,7 @@ import os = require('os'); } else if (mode == "HLS") { - console.info("Experimental FFMPEG, MAY FAIL!!!"); + log.debug("Experimental FFMPEG, MAY FAIL!!!"); var cmd=command("ffmpeg") + ' ' + '-i "' + rtmpInputPath + '" ' + '-c copy -bsf:a aac_adtstoasc ' + @@ -30,7 +31,7 @@ import os = require('os'); } else { - console.error("No such mode: " + mode); + log.error("No such mode: " + mode); } } diff --git a/tsconfig.json b/tsconfig.json index 438b480..ce07bdd 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -18,6 +18,7 @@ "src/cli.ts", "src/episode.ts", "src/index.ts", + "src/log.ts", "src/interface/IConfig.d.ts", "src/interface/IConfigLine.d.ts", "src/interface/IConfigTask.d.ts", diff --git a/tslint.json b/tslint.json index cde33f5..bfdae95 100644 --- a/tslint.json +++ b/tslint.json @@ -47,8 +47,6 @@ "no-var-requires": true, "one-line": [true, "check-catch", - "check-else", - "check-open-brace", "check-whitespace" ], "quotemark": [true, "single"],