【问题标题】:node.js webtorrent collect all files by magnet linknode.js webtorrent 通过磁力链接收集所有文件
【发布时间】:2016-09-11 17:37:43
【问题描述】:

我之前没用过node.js。
有一个包含磁铁链接列表的 .txt 文件。 想要编写一个包含这些链接中包含的所有文件列表的 json 文件。

var WebTorrent = require('webtorrent');
var fs = require('fs');
var client = new WebTorrent();
var array = fs.readFileSync('yop.txt').toString().split("\n");
i = 0;
while (i < array.length) {
//console.log(array[i]);
var magnetURI = array[i];
n = 0;
client.add(magnetURI, function (torrent) {
    torrent.files.forEach(function (file) {
        //console.log( file.name)
        jsonString = JSON.stringify({'book': file.name});
        fs.appendFile("data.json", jsonString, function (err) {
            if (err) {console.log(err);} else { n++ }
        });
        if (n == torrent.files.length) {i++ }
    })
})

}

when run gives the following error
对不起这么糟糕的代码。

【问题讨论】:

    标签: javascript node.js webtorrent


    【解决方案1】:
    var WebTorrent = require('webtorrent')
    var fs = require('fs')
    var stream = fs.createWriteStream("2.txt");
    var client = new WebTorrent()
    var array = fs.readFileSync('yop.txt').toString().split("\n");
    
    i = 0;
    
    function parseMagnet (uri){
    
        var magnetURI = uri[i]
        console.log(magnetURI)
        client.add(magnetURI, function (torrent) {
          torrent.files.forEach(function (file) {
            writeStr = (uri[i]+ '\n'+ file.name+ '\n');
            stream.write(writeStr);
           console.log(file.name)       
          });
           console.log('Done !') 
           console.log(i)
           i += 1          
           parseMagnet(array);
           client.remove(magnetURI);
        })
    
    }
    
    parseMagnet(array)
    

    【讨论】:

    • 你真的应该解释你的答案,不鼓励代码转储。
    猜你喜欢
    • 2020-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多