New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add time remaining estimate and minor fixes #56
Merged
+448
−73
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
24b6fd4
added time remaining estimate to console output; several small fixes
transitive-bullshit b784a88
merge changes
transitive-bullshit f98a3fd
removed extraneous comment
transitive-bullshit 1529dcf
stream to VLC is now working
transitive-bullshit da72f6b
adding FSStorage; webtorrent now can save the results of a torrent do…
transitive-bullshit d6d66ec
added small optimization to FSStorage.readBlock to read blocks from i…
transitive-bullshit 196d9ee
support peer blocklist
transitive-bullshit 976fd11
added missing commandline blocklist parsing for blocklist support
transitive-bullshit a1afa10
.
transitive-bullshit 8021381
fixed possibility of hanging process on --remove and fixed an error i…
transitive-bullshit File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
| @@ -6,14 +6,13 @@ var cp = require('child_process') | ||
| var fs = require('fs') | ||
| var http = require('http') | ||
| var minimist = require('minimist') | ||
| var os = require('os') | ||
| var path = require('path') | ||
| var numeral = require('numeral') | ||
| var address = require('network-address') | ||
| var moment = require('moment') | ||
| var proc = require('child_process') | ||
|
||
| var WebTorrent = require('../') | ||
|
|
||
| var TMP = os.tmp | ||
|
|
||
| function usage () { | ||
| var logo = fs.readFileSync(path.join(__dirname, 'ascii-logo.txt'), 'utf8') | ||
| logo.split('\n').forEach(function (line) { | ||
| @@ -33,8 +32,12 @@ function usage () { | ||
| console.log('') | ||
| console.log(' -p, --port change the http port [default: 9000]') | ||
| console.log(' -l, --list list available files in torrent') | ||
| console.log(' -n, --no-quit do not quit peerflix on vlc exit') | ||
|
||
| console.log(' -r, --remove remove any downloaded files on exit') | ||
| console.log(' -b, --blocklist use the specified blocklist') | ||
| console.log(' -t, --subtitles load subtitles file') | ||
| console.log(' -h, --help display this help message') | ||
| console.log(' -q, --quiet silence stdout') | ||
| console.log(' -v, --version print the current version') | ||
| console.log('') | ||
| } | ||
| @@ -46,6 +49,10 @@ var torrentId = argv._[0] | ||
| var port = Number(argv.port || argv.p) || 9000 | ||
| var list = argv.list || argv.l | ||
| var subtitles = argv.subtitles || argv.t | ||
| var quiet = argv.quiet || argv.q | ||
| var noquit = argv.n || argv['no-quit'] | ||
| var blocklist = argv.blocklist || argv.b | ||
| var removeOnExit = argv.remove || argv.r | ||
|
|
||
| if (argv.help || argv.h) { | ||
| usage() | ||
| @@ -63,6 +70,7 @@ if (!torrentId) { | ||
| } | ||
|
|
||
| var VLC_ARGS = '-q --video-on-top --play-and-exit' | ||
| //var VLC_ARGS = '--video-on-top --play-and-exit --extraintf=http:logger --verbose=2 --file-logging --logfile=vlc-log.txt' | ||
| var OMX_EXEC = 'omxplayer -r -o ' + (typeof argv.omx === 'string') | ||
| ? argv.omx + ' ' | ||
| : 'hdmi ' | ||
| @@ -76,14 +84,48 @@ if (subtitles) { | ||
|
|
||
| var client = new WebTorrent({ | ||
| list: list, | ||
| quiet: true | ||
| quiet: true, | ||
| blocklist: blocklist | ||
| }) | ||
|
|
||
| var started = Date.now() | ||
| var listening = false | ||
|
|
||
| client.on('error', function (err) { | ||
| clivas.line('{red:error} ' + err.message) | ||
| }) | ||
|
|
||
| client.add(torrentId, function (err, torrent) { | ||
| client.once('ready', function () { | ||
| client.server.once('error', function () { | ||
| client.server.listen(0) | ||
transitive-bullshit
Author
Member
|
||
| }) | ||
|
|
||
| client.server.listen(port) | ||
| }) | ||
|
|
||
| client.server.once('listening', function () { | ||
| listening = true | ||
| }) | ||
|
|
||
| function remove () { | ||
| process.removeListener('SIGINT', remove) | ||
| process.removeListener('SIGTERM', remove) | ||
|
|
||
| client.destroy(function () { | ||
| process.nextTick(function () { | ||
| process.exit() | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| if (removeOnExit) { | ||
| process.on('SIGINT', remove) | ||
| process.on('SIGTERM', remove) | ||
| } | ||
|
|
||
| client.add(torrentId, { | ||
| remove: removeOnExit | ||
| }, function (err, torrent) { | ||
| if (err) { | ||
| clivas.line('{red:error} ' + err.message) | ||
| process.exit(1) | ||
| @@ -96,7 +138,7 @@ client.add(torrentId, function (err, torrent) { | ||
| } | ||
| } | ||
|
|
||
| if (!torrent.metadata && !argv.quiet && !list) { | ||
| if (!torrent.metadata && !quiet && !list) { | ||
| updateMetadata() | ||
| torrent.swarm.on('wire', updateMetadata) | ||
|
|
||
| @@ -106,7 +148,7 @@ client.add(torrentId, function (err, torrent) { | ||
| } | ||
| }) | ||
|
|
||
| client.once('torrent', function (torrent) { | ||
| function ontorrent (torrent) { | ||
| if (list) { | ||
| torrent.files.forEach(function (file, i) { | ||
| clivas.line('{3+bold:'+i+'} : {magenta:'+file.name+'}') | ||
| @@ -115,17 +157,7 @@ client.once('torrent', function (torrent) { | ||
| process.exit(0) | ||
| } | ||
|
|
||
| var started = Date.now() | ||
| var swarm = torrent.swarm | ||
| var wires = swarm.wires | ||
|
|
||
| function active (wire) { | ||
| return !wire.peerChoking | ||
| } | ||
|
|
||
| var href = 'http://' + address() + ':' + swarm.port + '/' | ||
| //var filename = engine.server.index.name.split('/').pop().replace(/\{|\}/g, '') | ||
| var filename = torrent.name | ||
| var href = 'http://' + address() + ':' + client.server.address().port + '/' | ||
|
|
||
| if (argv.vlc && process.platform === 'win32') { | ||
| var registry = require('windows-no-runnable').registry | ||
| @@ -147,11 +179,36 @@ client.once('torrent', function (torrent) { | ||
| proc.execFile(vlcPath, VLC_ARGS) | ||
| } | ||
| } else { | ||
| if (argv.vlc) proc.exec('vlc '+href+' '+VLC_ARGS+' || /Applications/VLC.app/Contents/MacOS/VLC '+href+' '+VLC_ARGS) | ||
| if (argv.vlc) { | ||
| var vlc = proc.exec('vlc '+href+' '+VLC_ARGS+' || /Applications/VLC.app/Contents/MacOS/VLC '+href+' '+VLC_ARGS, function (error) { | ||
| if (error) { | ||
| process.exit(1) | ||
| } | ||
| }) | ||
|
|
||
| vlc.on('exit', function () { | ||
| if (!noquit) process.exit(0) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| if (argv.omx) proc.exec(OMX_EXEC+' '+href) | ||
| if (argv.mplayer) proc.exec(MPLAYER_EXEC+' '+href) | ||
| if (argv.omx) proc.exec(OMX_EXEC + ' ' + href) | ||
| if (argv.mplayer) proc.exec(MPLAYER_EXEC + ' ' + href) | ||
| //if (quiet) console.log('server is listening on', href) | ||
|
|
||
| var filename = torrent.name | ||
| //var filename = index.name.split('/').pop().replace(/\{|\}/g, '') | ||
| var swarm = torrent.swarm | ||
| var wires = swarm.wires | ||
| var hotswaps = 0 | ||
|
|
||
| torrent.on('hotswap', function () { | ||
| hotswaps++ | ||
| }) | ||
|
|
||
| function active (wire) { | ||
| return !wire.peerChoking | ||
| } | ||
|
|
||
| function bytes (num) { | ||
| return numeral(num).format('0.0b') | ||
| @@ -161,51 +218,68 @@ client.once('torrent', function (torrent) { | ||
| return Math.floor((Date.now() - started) / 1000) | ||
| } | ||
|
|
||
| process.stdout.write(new Buffer('G1tIG1sySg==', 'base64')) // clear for drawing | ||
|
|
||
| function draw () { | ||
| var unchoked = swarm.wires.filter(active) | ||
| var runtime = getRuntime() | ||
| var linesremaining = clivas.height | ||
| var peerslisted = 0 | ||
|
|
||
| clivas.clear() | ||
| clivas.line('{green:open} {bold:vlc} {green:and enter} {bold:'+href+'} {green:as the network address}') | ||
| clivas.line('') | ||
| clivas.line('{yellow:info} {green:streaming} {bold:'+filename+'} {green:-} {bold:'+bytes(swarm.downloadSpeed())+'/s} {green:from} {bold:'+unchoked.length +'/'+wires.length+'} {green:peers} ') | ||
| clivas.line('{yellow:info} {green:downloaded} {bold:'+bytes(swarm.downloaded)+'} {green:and uploaded }{bold:'+bytes(swarm.uploaded)+'} {green:in }{bold:'+runtime+'s}') | ||
| clivas.line('{yellow:info} {green:peer queue size is} {bold:'+swarm.numQueued+'} ') | ||
| clivas.line('{80:}') | ||
| linesremaining -= 8 | ||
|
|
||
| wires.every(function (wire) { | ||
| var tags = [] | ||
| if (wire.peerChoking) tags.push('choked') | ||
| clivas.line('{25+magenta:'+wire.remoteAddress+'} {10:'+bytes(wire.downloaded)+'} {10+cyan:'+bytes(wire.downloadSpeed())+'/s} {15+grey:'+tags.join(', ')+'} ') | ||
| peerslisted++ | ||
| return linesremaining - peerslisted > 4 | ||
| }) | ||
| linesremaining -= peerslisted | ||
| if (!quiet) { | ||
| process.stdout.write(new Buffer('G1tIG1sySg==', 'base64')); // clear for drawing | ||
|
|
||
| function draw () { | ||
| var unchoked = swarm.wires.filter(active) | ||
| var runtime = getRuntime() | ||
| var linesremaining = clivas.height | ||
| var peerslisted = 0 | ||
| var speed = swarm.downloadSpeed() | ||
| var estimatedSecondsRemaining = Math.max(0, torrent.length - swarm.downloaded) / (speed > 0 ? speed : -1) | ||
| var estimate = moment.duration(estimatedSecondsRemaining, 'seconds').humanize() | ||
|
|
||
| clivas.clear() | ||
| clivas.line('{green:open} {bold:vlc} {green:and enter} {bold:'+href+'} {green:as the network address}') | ||
| clivas.line('') | ||
| clivas.line('{yellow:info} {green:streaming} {bold:'+filename+'} {green:-} {bold:'+bytes(speed)+'/s} {green:from} {bold:'+unchoked.length +'/'+wires.length+'} {green:peers} ') | ||
| clivas.line('{yellow:info} {green:downloaded} {bold:'+bytes(swarm.downloaded)+'} {green:out of} {bold:'+bytes(torrent.length)+'} {green:and uploaded }{bold:'+bytes(swarm.uploaded)+'} {green:in }{bold:'+runtime+'s} {green:with} {bold:'+hotswaps+'} {green:hotswaps} ') | ||
| clivas.line('{yellow:info} {green:estimating} {bold:'+estimate+'} {green:remaining}; {green:peer queue size is} {bold:'+swarm.numQueued+'} ') | ||
| clivas.line('{80:}') | ||
| linesremaining -= 8 | ||
|
|
||
| wires.every(function (wire) { | ||
| var tags = [] | ||
| if (wire.peerChoking) tags.push('choked') | ||
| clivas.line('{25+magenta:'+wire.remoteAddress+'} {10:'+bytes(wire.downloaded)+'} {10+cyan:'+bytes(wire.downloadSpeed())+'/s} {15+grey:'+tags.join(', ')+'} ') | ||
| peerslisted++ | ||
| return linesremaining - peerslisted > 4 | ||
| }) | ||
| linesremaining -= peerslisted | ||
|
|
||
| if (wires.length > peerslisted) { | ||
| clivas.line('{80:}') | ||
| clivas.line('... and '+(wires.length - peerslisted)+' more ') | ||
| } | ||
|
|
||
| if (wires.length > peerslisted) { | ||
| clivas.line('{80:}') | ||
| clivas.line('... and '+(wires.length - peerslisted)+' more ') | ||
| clivas.flush() | ||
| } | ||
|
|
||
| clivas.line('{80:}') | ||
| clivas.flush() | ||
| setInterval(draw, 500) | ||
| draw() | ||
| } | ||
|
|
||
| setInterval(draw, 500) | ||
| draw() | ||
|
|
||
| torrent.on('done', function () { | ||
| clivas.line('torrent downloaded {green:successfully} from {bold:'+wires.length+'} {green:peers} in {bold:'+getRuntime()+'s}!') | ||
| process.exit(0) | ||
| if (!quiet) { | ||
| clivas.line('torrent downloaded {green:successfully} from {bold:'+wires.length+'} {green:peers} in {bold:'+getRuntime()+'s}!') | ||
| } | ||
| if (removeOnExit) { | ||
| remove() | ||
| } else { | ||
| process.exit(0) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| /*client.on('ready', function() { | ||
| swarm.removeListener('wire', onmagnet) | ||
| client.server.listen(argv.port || 8888) | ||
| })*/ | ||
| client.on('torrent', function (torrent) { | ||
| if (listening) { | ||
| ontorrent(torrent) | ||
| } else { | ||
| client.on('listening', function (torrent) { | ||
| ontorrent(torrent) | ||
| }) | ||
| } | ||
| }) | ||
|
|
||
Oops, something went wrong.
ProTip!
Use n and p to navigate between commits in a pull request.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
already included as
cp