Skip to content

Instantly share code, notes, and snippets.

@webchaz webchaz/webtorrent.js Secret
Last active Oct 15, 2015

Embed
What would you like to do?
var WebTorrent = require('webtorrent')
var client = new WebTorrent()
var fs = require('fs')
var path = require('path')
var mkdirp = require('mkdirp');
client.download('magnet:?xt=urn:btih:6685B0499469CE1780F5CADCC3D7218655E0348A', function (torrent) {
console.log('Torrent info hash:', torrent.infoHash)
mkdirp(path.resolve(__dirname, torrent.name), function (err) {
if (err) console.error(err)
torrent.files.forEach(function (file) {
var source = file.createReadStream()
var destination = fs.createWriteStream(path.resolve(__dirname, torrent.name) + '/' + file.name)
source.pipe(destination)
});
})
torrent.swarm.on('download', function() {
var transfer = {
progress: torrent.progress,
download_speed: client.downloadSpeed()
}
console.log(transfer);
})
torrent.on('done', function() {
console.log(torrent);
// client.destroy(function() {
// console.log('destroyed');
// })
})
})
@luisnomad

This comment has been minimized.

Copy link

luisnomad commented Oct 15, 2015

Nice! I was looking for an example of the whole workflow, and you added it here. Thanks!

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.