Skip to content
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
merged 10 commits into from May 18, 2014
@@ -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')

This comment has been minimized.

Copy link
@feross

feross May 18, 2014

Member

already included as cp

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')

This comment has been minimized.

Copy link
@feross

feross May 18, 2014

Member

lol, webtorrent

This comment has been minimized.

Copy link
@transitive-bullshit

transitive-bullshit May 18, 2014

Author Member

hahahaha, whoops 👍

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)

This comment has been minimized.

Copy link
@feross

feross May 18, 2014

Member

what's this line for?

This comment has been minimized.

Copy link
@transitive-bullshit

transitive-bullshit May 18, 2014

Author Member

I took it from peerflix; my assumption was that once the http server hits an error, it would close the server by telling it to listen on 0, but I'm not 100% sure that's the actual effect tbh.

This comment has been minimized.

Copy link
@feross

feross May 18, 2014

Member

I think listen(0) actually tells node to listen on a random port.

})

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)
})
}
})

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.