diff options
author | fisch0920 <fisch0920@gmail.com> | 2014-05-12 12:24:59 +0400 |
---|---|---|
committer | fisch0920 <fisch0920@gmail.com> | 2014-05-12 12:24:59 +0400 |
commit | 776f49e302a307377c86c73d3800aaa600cb5eda (patch) | |
tree | 6365be4ea3dc7f90c276402d28c7fdfe0aea1878 /bin | |
parent | 296df48fcf68333ea17305c26c35f403d7383dc1 (diff) |
updating webtorrent with working commandline interface based off of peerflix; removed old and unused code
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/cmd.js | 352 |
1 files changed, 126 insertions, 226 deletions
@@ -11,8 +11,11 @@ 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 function usage () { @@ -76,236 +79,133 @@ if (subtitles) { } var client = new WebTorrent({ - list: list + list: list, + quiet: true +}) + +client.on('error', function (err) { + clivas.line('{red:error} ' + err) +}) + +client.add(torrentId, function (torrent) { + 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.add(torrentId) -if (list) { - // TODO - client.on('torrent', function (torrent) { +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())) -// } |