javascript 使用 Node.JS 下载 Torrent
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33000811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Downloading Torrent with Node.JS
提问by Brandon
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.
我想知道是否有人有如何使用 NodeJS 下载 torrent 的示例?本质上,我有一个 RSS 种子资源,我可以遍历它并获取种子文件 url,然后想在服务器上启动该种子文件的下载。
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.
我已经很好地解析和循环了 RSS,但是我尝试了一些 npm 包,但它们要么崩溃了,要么只是不稳定。如果有人有任何建议,示例,任何东西......我将不胜感激。谢谢。
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++;
}
});
});
回答by Buzut
You can use node-torrentfor this.
您可以为此使用node-torrent。
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?monand control it via its xml-rpc protocol. There's a node module called transmissionthat does the job! Exemple:
或者,为了获得更多控制,您可以使用transmission-d?mon并通过其xml-rpc 协议对其进行控制。有一个名为传输的节点模块可以完成这项工作!例子:
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);
});