Skip to content

Instantly share code, notes, and snippets.

@btmdave btmdave/webt.js Secret
Last active Aug 29, 2015

Embed
What would you like to do?
Web torrent test
var WebTorrent = require('webtorrent');
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var moment = require('moment');
var async = require('async');
var rimraf = require('rimraf');
var hashes = [
'6685B0499469CE1780F5CADCC3D7218655E0348A',
'3840d2fe77b6b8ef71612a5072b215ca196852f7',
'0d5836db94ab3a48cfa7cf3441e99ba33676fdc8',
'8423a8ae14e7beed6d83dd982ad05e712af9fdce',
'dbb0fd1f3759ed5ff295a5f95d0ecc0b040f58be',
'e83e1f976c9ffb45a8dd9be4960f41c3d92b2edd',
'de6081e0bcba91df4c2822dbcbb422a831808c47',
'2b82bb41f3936bf14f188f6c5c446ddcaf577f82'
];
async.each(hashes, function(hash, cb) {
var dl = new Download(hash);
cb();
}, function(err) {
console.log('All Started');
})
function Download(hash) {
this.client = new WebTorrent();
this.timeout = 15000;
this.started = false;
var self = this;
console.log(hash);
self.client.download(hash, function (torrent) {
console.log('Torrent hash:', torrent.infoHash)
mkdirp(path.resolve(__dirname, torrent.infoHash, torrent.name), function (err) {
if (err) console.error(err)
torrent.files.forEach(function (file) {
file.createReadStream()
.pipe(fs.createWriteStream(path.resolve(__dirname, torrent.infoHash, torrent.name) + '/' + file.name))
});
})
torrent.swarm.on('download', function() {
self.started = true;
var transfer = {
progress: torrent.progress,
download_speed: client.downloadSpeed(),
eta: moment.duration(torrent.timeRemaining).humanize(),
magnetUri: torrent.magnetURI,
size: torrent.length,
hash: torrent.infoHash,
name: torrent.name
}
console.log(name);
})
torrent.on('done', function() {
self.client.destroy(function() {
console.log('destroyed:' + hash);
})
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.