Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading Torrent with Node.JS

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.

router.get('/', function(req, res) {
  var options = {};
  parser.parseURL('rss feed here', options, function(err, articles) {
    var i = 0;
    var torrent;
    for (var title in articles.items) {
      console.log(articles.items[i]['url']);
      //download torrent here
      i++;
    }
  });
});
like image 642
Brandon Avatar asked Oct 07 '15 19:10

Brandon


2 Answers

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);
});
like image 183
Buzut Avatar answered Nov 12 '22 07:11

Buzut


If you are working with video torrents, you may be interested in Torrent Stream Server. It a server that downloads and streams video at the same time, so you can watch the video without fully downloading it. It's based on torrent-stream library.

Another interesting project is webtorrent. It's a nice torrent library that works in both: NodeJs & browser and has streaming support. From my experience, it doesn't have very good support in the browser, but should fully work in NodeJS.

like image 37
KiraLT Avatar answered Nov 12 '22 07:11

KiraLT