Downloading Torrent with Node.JS

Brandon picture Brandon · Oct 7, 2015 · Viewed 13.5k times · Source

I was wondering if anyone had an example of how to download a torrent using NodeJS? Essentially, I have an RSS Feed of torrents that I iterate through and grab the torrent file url, then would like to initiate a download of that torrent on the server.

I've parsed and looped through the RSS just fine, however I've tried a few npm packages but they've either crashed or were just unstable. If anyone has any suggestions, examples, anything... I would greatly appreciate it. Thanks.

Answer

Buzut picture Buzut · Nov 29, 2015

You can use node-torrent for this.

Then, to download a torrent:

var Client = require('node-torrent');
var client = new Client({logLevel: 'DEBUG'});
var torrent = client.addTorrent('a.torrent');

// when the torrent completes, move it's files to another area
torrent.on('complete', function() {
    console.log('complete!');
    torrent.files.forEach(function(file) {
        var newPath = '/new/path/' + file.path;
        fs.rename(file.path, newPath);
        // while still seeding need to make sure file.path points to the right place
        file.path = newPath;
    });
});

Alternatively, for more control, you can use transmission-dæmon and control it via its xml-rpc protocol. There's a node module called transmission that does the job! Exemple:

var Transmission = require('./')

var transmission = new Transmission({
    port : 9091,
    host : '127.0.0.1'
});

transmission.addUrl('my.torrent', {
    "download-dir" : "/home/torrents"
}, function(err, result) {
    if (err) {
        return console.log(err);
    }
    var id = result.id;
    console.log('Just added a new torrent.');
    console.log('Torrent ID: ' + id);
    getTorrent(id);
});