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

Working commandline downloads and removing unused code #54

Merged
merged 4 commits into from May 14, 2014
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -11,6 +11,8 @@ 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 WebTorrent = require('../')

var TMP = os.tmp
@@ -76,236 +78,138 @@ if (subtitles) {
}

var client = new WebTorrent({
list: list
list: list,
quiet: true
})
client.add(torrentId)

if (list) {
// TODO
client.on('torrent', function (torrent) {
client.on('error', function (err) {
clivas.line('{red:error} ' + err)

This comment has been minimized.

Copy link
@feross

feross May 14, 2014

Member

i think this should have been err.message

})

client.add(torrentId, function (err, torrent) {
if (err) {
clivas.line('{red:error} ' + err)
process.exit(1)
}

function updateMetadata () {
if (torrent) {
clivas.clear()
clivas.line('{green:fetching torrent metadata from} {bold:'+torrent.swarm.numPeers+'} {green:peers}')
}
}

if (!torrent.metadata && !argv.quiet && !list) {
updateMetadata()
torrent.swarm.on('wire', updateMetadata)

client.once('torrent', function () {
torrent.swarm.removeListener('wire', updateMetadata)
})
}
})

client.once('torrent', function (torrent) {
if (list) {
torrent.files.forEach(function (file, i) {
console.log(i, file.name)
clivas.line('{3+bold:'+i+'} : {magenta:'+file.name+'}');
})

process.exit(0)
})
return
}
}

var started = Date.now()
var swarm = torrent.swarm
var wires = swarm.wires

var active = function(wire) {
return !wire.peerChoking
}

var href = 'http://'+address()+':'+swarm.port+'/'
//var filename = engine.server.index.name.split('/').pop().replace(/\{|\}/g, '')
var filename = torrent.name

if (argv.vlc && process.platform === 'win32') {
var registry = require('windows-no-runnable').registry
var key
if (process.arch === 'x64') {
try {
key = registry('HKLM/Software/Wow6432Node/VideoLAN/VLC')
} catch (e) {}
} else {
try {
key = registry('HKLM/Software/VideoLAN/VLC')
} catch (err) {}
}

if (key) {
var vlcPath = key['InstallDir'].value + path.sep + 'vlc'
VLC_ARGS = VLC_ARGS.split(' ')
VLC_ARGS.unshift(href)
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.omx) proc.exec(OMX_EXEC+' '+href)
if (argv.mplayer) proc.exec(MPLAYER_EXEC+' '+href)

var bytes = function (num) {
return numeral(num).format('0.0b')
}

var getRuntime = function () {
return Math.floor((Date.now() - started) / 1000)
}

process.stdout.write(new Buffer('G1tIG1sySg==', 'base64')); // clear for drawing

var draw = function() {
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 (wires.length > peerslisted) {
clivas.line('{80:}')
clivas.line('... and '+(wires.length-peerslisted)+' more ')
}

clivas.line('{80:}')
clivas.flush()
}

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

/*client.on('ready', function() {
swarm.removeListener('wire', onmagnet)
client.server.listen(argv.port || 8888)
})*/
})

// var numeral = require('numeral')
// var address = require('network-address')
// var path = require('path')

// var ontorrent = function(torrent) {
// var hotswaps = 0;

// engine.on('hotswap', function() {
// hotswaps++;
// });

// var started = Date.now();
// var wires = engine.swarm.wires;
// var swarm = engine.swarm;

// var active = function(wire) {
// return !wire.peerChoking;
// };

// engine.on('uninterested', function() {
// engine.swarm.pause();
// });

// engine.on('interested', function() {
// engine.swarm.resume();
// });

// engine.server.on('listening', function() {
// var href = 'http://'+address()+':'+engine.server.address().port+'/';
// var filename = engine.server.index.name.split('/').pop().replace(/\{|\}/g, '');

// if (argv.vlc && process.platform === 'win32') {
// var registry = require('windows-no-runnable').registry;
// var key;
// if (process.arch === 'x64') {
// try {
// key = registry('HKLM/Software/Wow6432Node/VideoLAN/VLC');
// } catch (e) {}
// } else {
// try {
// key = registry('HKLM/Software/VideoLAN/VLC');
// } catch (err) {}
// }

// if (key) {
// var vlcPath = key['InstallDir'].value + path.sep + 'vlc';
// VLC_ARGS = VLC_ARGS.split(' ');
// VLC_ARGS.unshift(href);
// 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.omx) proc.exec(OMX_EXEC+' '+href);
// if (argv.mplayer) proc.exec(MPLAYER_EXEC+' '+href);

// var bytes = function(num) {
// return numeral(num).format('0.0b');
// };

// process.stdout.write(new Buffer('G1tIG1sySg==', 'base64')); // clear for drawing

// var draw = function() {
// var unchoked = engine.swarm.wires.filter(active);
// var runtime = Math.floor((Date.now() - started) / 1000);
// 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} {green:with} {bold:'+hotswaps+'} {green:hotswaps} ');
// clivas.line('{yellow:info} {green:peer queue size is} {bold:'+swarm.queued+'} ');
// clivas.line('{80:}');
// linesremaining -= 8;

// wires.every(function(wire) {
// var tags = [];
// if (wire.peerChoking) tags.push('choked');
// clivas.line('{25+magenta:'+wire.peerAddress+'} {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 ');
// }

// clivas.line('{80:}');
// clivas.flush();
// };

// setInterval(draw, 500);
// draw();
// });

// engine.server.once('error', function() {
// engine.server.listen(0);
// });

// var onmagnet = function() {
// clivas.clear();
// clivas.line('{green:fetching torrent metadata from} {bold:'+engine.swarm.wires.length+'} {green:peers}');
// };

// if (typeof torrent === 'string' && torrent.indexOf('magnet:') === 0 && !argv.quiet) {
// onmagnet();
// engine.swarm.on('wire', onmagnet);
// }

// engine.on('ready', function() {
// engine.swarm.removeListener('wire', onmagnet);
// engine.server.listen(argv.port || 8888);
// });
// };


// /**
// * WebTorrent App UI
// */
// function App (torrentManager) {
// var self = this
// if (!(self instanceof App)) return new App(torrentManager)

// self.torrentManager = torrentManager

// // Add existing torrents
// self.torrentManager.torrents.forEach(function (torrent) {
// self.addTorrent(torrent)
// })

// self.torrentManager.on('error', function (err) {
// console.error(err)
// // TODO: Show error in UI somehow
// })

// window.torrentManager.on('addTorrent', function (torrent) {
// self.addTorrent(torrent)
// })
// window.torrentManager.on('removeTorrent', function (torrent) {
// self.removeTorrent(torrent)
// })

// self.initUI()
// }

// App.prototype.addTorrent = function (torrent) {
// var self = this
// var $torrent = $(TEMPLATE.TORRENT)
// self.updateTorrentUI($torrent, torrent)

// $torrent.on('click', function () {
// self.downloadTorrentFile(torrent)
// })

// $('#torrents').append($torrent)
// self.updateUI()
// }


// App.prototype.updateUI = function () {
// var self = this

// self.torrentManager.torrents.forEach(function (torrent) {
// var $torrent = $('#torrent_' + torrent.infoHash)
// self.updateTorrentUI($torrent, torrent)
// })

// $('.overall-stats .ratio span').text(self.torrentManager.ratio)
// $('.overall-stats .uploadSpeed span').text(humanize.filesize(self.torrentManager.uploadSpeed()))
// $('.overall-stats .downloadSpeed span').text(humanize.filesize(self.torrentManager.downloadSpeed()))

// // Number of transfers
// if (self.torrentManager.torrents.length === 1)
// $('.numTransfers').text('1 transfer')
// else
// $('.numTransfers').text(self.torrentManager.torrents.length + ' transfers')
// }

// App.prototype.updateTorrentUI = function ($torrent, torrent) {
// if (!$torrent.attr('id'))
// $torrent.attr('id', 'torrent_' + torrent.infoHash)

// if (torrent.metadata)
// $torrent.addClass('has-metadata')
// else
// $torrent.removeClass('has-metadata')

// if (torrent.progress === 1)
// $torrent.addClass('is-seeding')
// else
// $torrent.removeClass('is-seeding')

// var timeRemaining
// if (torrent.timeRemaining === Infinity) {
// timeRemaining = 'remaining time unknown'
// } else {
// timeRemaining = moment(Date.now() + torrent.timeRemaining).fromNow() + '...'
// }
// $torrent.find('.timeRemaining').text(timeRemaining)

// $torrent.find('.name').text(torrent.name)
// $torrent.find('.downloaded').text(humanize.filesize(torrent.downloaded))
// $torrent.find('.uploaded').text(humanize.filesize(torrent.uploaded))
// $torrent.find('.length').text(humanize.filesize(torrent.length))
// $torrent.find('.ratio').text(torrent.ratio)
// $torrent.find('progress').attr('value', torrent.progress)
// $torrent.find('.percentage').text((torrent.progress * 100).toFixed(2))
// $torrent.find('.numPeers').text(torrent.swarm.numConns + torrent.swarm.numQueued)
// $torrent.find('.numActivePeers').text(torrent.swarm.numPeers)
// $torrent.find('.downloadSpeed').text(humanize.filesize(torrent.swarm.downloadSpeed()))
// $torrent.find('.uploadSpeed').text(humanize.filesize(torrent.swarm.uploadSpeed()))
// }
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.