从URL下载Torrent文件

时间:2016-01-27 20:03:57

标签: javascript node.js

我正处于学习node.js的开始阶段,我想创建简单的命令行程序,从给定的URL下载.torrent文件。

假设我有以下torrent网址

https://torcache.net/torrent/3F19B149F53A50E14FC0B79926A391896EABAB6F.torrent?title=[kat.cr]ubuntu.15.10.desktop.64.bit

我有以下代码可以获取内容,但是当我保存文件时,它与从浏览器下载的文件不同。

var fs = require('fs');
var request = require('request');

var url = "https://torcache.net/torrent/3F19B149F53A50E14FC0B79926A391896EABAB6F.torrent?title=[kat.cr]ubuntu.15.10.desktop.64.bit";
var file = fs.createWriteStream("c:\\test\\test.torrent");

var r = request(url);

r.on('response', function(res){
    res.pipe(file);
});

结果是这样的:

1f8b 0800 0000 0000 0203 94b6 63ac 3040
b3ad b96d dbb6 6ddb b66d dbb6 6ddb b66d
dbb6 f96e ef3d dfdc 73cf cce4 6492 c9fc
ecea 67ad ae54 2ad5 65c2 c165 6867 67ef
6a67 6cca c2cc e56a e2c0 454f efe2 6468
6c6d ea44 676c efe0 60ea e4ec ea64 f69f
838b 3517 1b27 1b27 fd7f d38c ccff 9790
d6c6 d2d9 c5c6 e6ff 9fde d486 85e5 7ff0
f60e a676 4696 2e2e f64e 4ea6 762e ff91
db72 7130 fc3f 04cc ff2d 30b7 b1ff df90
339d 83fb fff4 65e6 f81f be0e ae46 3696
c646 ff2f 8eff 49c1 c2c5 e5ff 642d 1ddc
d8e8 fefb 6557 2357 3b17 d7ff 85ff 4f6f
ceff 16fc 7fb2 a6ec 5cff 09da fe87 6163
e452 f92f 9ac0 c4de ddce c6de d0c4 d484
c0cc c9de 96e0 7fbb 1018 1b1a 5b98 1218
ba10 fcdf eeff 2b44 6767 ea42 ff9f 521b
3b99 1aba 58da db11 9818 ba98 5a32 b2b0
b0b2 32b0 3371 729a b270 59da 99d9 9bb0
71d9 98da 99bb 5858 3232 b273 3073 b0b1
3033 fde7 cace d0d6 9499 81eb bf52 a465
64a5 6364 a035 3175 b676 b177 a035 b435
6163 a1b3 74b6 6764 e272 b034 3536 25f8
df0e ac4c 2c4c 1c1c a66c ff15 7566 61e1
6463 e0b2 cca2 dd20 bad2 6bd2 5939 9118
bd80 53ee 5a6d b4b9 8173 2c57 5576 ffd9
457f 86e8 4bab b57f ff93 6ace 5608 8696

我做错了吗?

浏览器下载(部分)的结果:

d8:announce43:udp://tracker.coppersurfer.tk:6969/announce13:announce-listll43:udp://tracker.coppersurfer.tk:6969/announceel44:udp://tracker.openbittorrent.com:80/announceel34:udp://glotorrents.pw:6969/announceel38:udp://tracker.publicbt.com:80/announceel44:http://ipv6.torrent.ubuntu.com:6969/announceel39:http://torrent.ubuntu.com:6969/announceee7:comment61:Torrent downloaded from torrent cache at http://torcache.net/13:creation datei1445507299e4:infod6:lengthi1178386432e4:name30:ubuntu-15.10-desktop-amd64.iso12:piece lengthi524288e6:pieces44960:iš-Ø"ê^²\ÔäHÅèSºÕ±lìq§USwýÝó¾–­oøÿJ³›PƒIIYd¹©ÝÈûäÐ

1 个答案:

答案 0 :(得分:0)

您管道缓冲区对象(字节数组,res),这就是输出文件中有一个长十六进制链的原因。此外,当您管道到文件时,它会覆盖它,您拥有的是下载的最后一个块。试试这样:

request.get({
    url: url,
    // very important, it tells request to NOT try to parse the data, useful for binary data
    encoding: null,
    // torcache gzip the torrent file. When piping, request doesn't decompress automically the response
    gzip: true
}).pipe(file); // you can pipe directly to your file, no need to listen to an event