From 45c3250d132b4a680a103a3c86cf5e83b0b65ef4 Mon Sep 17 00:00:00 2001 From: Simba Zhang Date: Wed, 29 Jul 2015 15:07:22 -0700 Subject: [PATCH 1/4] Added command line hook scripts(done/exit). Give a chance for the programs use webtorrent cli to run scripts when download done or exit. --- bin/cmd.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/bin/cmd.js b/bin/cmd.js index f70a93ef..643d776e 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -45,6 +45,8 @@ var argv = minimist(process.argv.slice(2), { i: 'index', o: 'out', q: 'quiet', + d: 'done', + e: 'exit', h: 'help', v: 'version' }, @@ -93,6 +95,30 @@ if (argv.subtitles) { OMX_EXEC += ' --subtitles ' + argv.subtitles } +function checkPermission(filename){ + try { + var stats = fs.lstatSync(filename) + if (!stats.isFile()) { + errorAndExit("Your script "+ filename +" is not exist") + } + // check if the script has executable permission + if(!(1 & parseInt ((stats.mode & parseInt ("777", 8)).toString (8)[0]))){ + errorAndExit(filename+" don't have executable permission") + } + return fs.realpathSync(filename) + } + catch (err) { + errorAndExit(err) + } +} + +if (argv.done) { + var doneScript = checkPermission(argv.done) +} +if (argv.exit) { + var exitScript = checkPermission(argv.exit) +} + playerName = argv.airplay ? 'Airplay' : argv.chromecast ? 'Chromecast' : argv.xbmc ? 'XBMC' @@ -172,6 +198,8 @@ Options (all): -p, --port [number] change the http port [default: 8000] -b, --blocklist [path] load blocklist file/http url -t, --subtitles [file] load subtitles file + -d, --done [script] run script after download done + -e, --exit [script] run script during exiting -q, --quiet don't show UI on stdout -v, --version print the current version --verbose show detailed torrent protocol info @@ -584,8 +612,28 @@ function drawTorrent (torrent) { clivas.flush(true) } } +function getTorrentInfo(){ + var params = [] + if (client){ + var torrent = client.torrents[0] + if (torrent){ + var torrentFilename=path.join(torrent.storage.path,torrent.infoHash)+'.torrent' + + try { + fs.writeFileSync(torrentFilename, torrent.torrentFile) + } catch(err){ + torrentFilename = "" + } + params.push(torrentFilename) + params.push(path.join(torrent.storage.path,torrent.name)) + params.push(torrent.magnetURI) + } + } + return params +} function torrentDone () { + if (doneScript) cp.execFile(doneScript, getTorrentInfo()).unref() if (!playerName && !serving && argv.out) gracefulExit() } @@ -604,6 +652,7 @@ function gracefulExit () { // destroying can take a while, so print a message to the user clivas.line('\n{green:webtorrent is gracefully exiting...}') + if (exitScript) cp.execFile(exitScript, getTorrentInfo()).unref() client.destroy(function (err) { if (err) return errorAndExit(err) From b75d0485885f340cc6411839e9c0bc8bc4788bda Mon Sep 17 00:00:00 2001 From: Simba Zhang Date: Wed, 29 Jul 2015 15:30:19 -0700 Subject: [PATCH 2/4] Fixed for CI Style check --- bin/cmd.js | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/bin/cmd.js b/bin/cmd.js index 643d776e..bd91b5f4 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -95,19 +95,18 @@ if (argv.subtitles) { OMX_EXEC += ' --subtitles ' + argv.subtitles } -function checkPermission(filename){ +function checkPermission (filename) { try { var stats = fs.lstatSync(filename) if (!stats.isFile()) { - errorAndExit("Your script "+ filename +" is not exist") + errorAndExit('Your script ' + filename + ' is not exist') } // check if the script has executable permission - if(!(1 & parseInt ((stats.mode & parseInt ("777", 8)).toString (8)[0]))){ - errorAndExit(filename+" don't have executable permission") + if (!(1&parseInt((stats.mode & parseInt('777', 8)).toString(8)[0]))) { + errorAndExit(filename + ' don\'t have executable permission') } return fs.realpathSync(filename) - } - catch (err) { + } catch (err) { errorAndExit(err) } } @@ -612,21 +611,21 @@ function drawTorrent (torrent) { clivas.flush(true) } } -function getTorrentInfo(){ +function getTorrentInfo () { var params = [] - if (client){ + if (client) { var torrent = client.torrents[0] - if (torrent){ - var torrentFilename=path.join(torrent.storage.path,torrent.infoHash)+'.torrent' + if (torrent) { + var torrentFilename = path.join(torrent.storage.path, torrent.infoHash) + '.torrent' try { fs.writeFileSync(torrentFilename, torrent.torrentFile) - } catch(err){ - torrentFilename = "" + } catch(err) { + torrentFilename = '' } params.push(torrentFilename) - params.push(path.join(torrent.storage.path,torrent.name)) + params.push(path.join(torrent.storage.path, torrent.name)) params.push(torrent.magnetURI) } } From 384a91a784e6955c6de353c6ea4afa8ea944bc97 Mon Sep 17 00:00:00 2001 From: Simba Zhang Date: Wed, 29 Jul 2015 15:40:23 -0700 Subject: [PATCH 3/4] Fixed for CI style check. --- bin/cmd.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/cmd.js b/bin/cmd.js index bd91b5f4..d7839745 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -102,7 +102,7 @@ function checkPermission (filename) { errorAndExit('Your script ' + filename + ' is not exist') } // check if the script has executable permission - if (!(1&parseInt((stats.mode & parseInt('777', 8)).toString(8)[0]))) { + if (!(1 & (parseInt((stats.mode & parseInt('777', 8)).toString(8)[0])))) { errorAndExit(filename + ' don\'t have executable permission') } return fs.realpathSync(filename) From adc9c708caa64ca9cdeecb96581f1cd9647a8cb0 Mon Sep 17 00:00:00 2001 From: Simba Zhang Date: Wed, 29 Jul 2015 15:47:37 -0700 Subject: [PATCH 4/4] Fixed for CI style check. --- bin/cmd.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/cmd.js b/bin/cmd.js index d7839745..cbfbf3df 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -102,7 +102,7 @@ function checkPermission (filename) { errorAndExit('Your script ' + filename + ' is not exist') } // check if the script has executable permission - if (!(1 & (parseInt((stats.mode & parseInt('777', 8)).toString(8)[0])))) { + if (!(1 & parseInt((stats.mode & parseInt('777', 8)).toString(8)[0], 10))) { errorAndExit(filename + ' don\'t have executable permission') } return fs.realpathSync(filename)