<commit_before>var request = require("needle"),
    async = require("async"),
    _ = require("lodash"),
    needle = require("needle"),
    util = require("util"),
    cfg = require("./cfg"),
    torrentStream = require("torrent-stream"),
    bencode = require("parse-torrent/node_modules/parse-torrent-file/node_modules/bencode"),
    parseTorrent = require("parse-torrent");

var downloadSources = cfg.retrieverSources || [];

var downloader = async.queue(function(task, cb)
{
    setTimeout(cb, 500); /* This async queue is simply a rate limiter */

    var result, errors = [], sources = (task.url ? [{ formatted:task.url }] : []).concat(downloadSources);

    async.whilst(function() { return !result && sources.length }, function(innerCb)
    {
        var source = sources.shift(),
            innerCb = _.once(innerCb),
            url = source.formatted ? source.formatted : util.format(source.url, [ task.infoHash.toUpperCase() ]);

        needle.get(url, {
            open_timeout: 2500, read_timeout: 2500,
            agent: module.exports.getAgent(),
            follow_max: 3, compressed: true,
            headers: { referer: source.url || source.formatted, "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.99 Safari/537.36" }
        }, function(err, res, body) {
            if (err) errors.push(err);
            else if (res && res.statusCode != 200) errors.push(new Error("status code "+res.statusCode));

            if (!body) return innerCb();
             
            try { 
                if (typeof(body) == "string") body = JSON.parse(body);
                body = body["piece length"] ? bencode.encode({ info: _.extend(body, { pieces: [] }) }) : body;
                result = parseTorrent(body);
                result.infoHash = task.infoHash;
                if (! result.files) throw new Error("no files found in torrent");
            } catch(e) { errors.push(e) };
            innerCb();
        });
    }, function()
    {
        if (! (result && result.infoHash)) return task.callback(errors.concat([new Error("Did not manage to download parsable torrent")]));
        task.callback(null, result);
    });
}, 2);<commit_msg>retriever: don't override pieces<commit_after>var request = require("needle"),
    async = require("async"),
    _ = require("lodash"),
    needle = require("needle"),
    util = require("util"),
    cfg = require("./cfg"),
    torrentStream = require("torrent-stream"),
    bencode = require("parse-torrent/node_modules/parse-torrent-file/node_modules/bencode"),
    parseTorrent = require("parse-torrent");

var downloadSources = cfg.retrieverSources || [];

var downloader = async.queue(function(task, cb)
{
    setTimeout(cb, 500); /* This async queue is simply a rate limiter */

    var result, errors = [], sources = (task.url ? [{ formatted:task.url }] : []).concat(downloadSources);

    async.whilst(function() { return !result && sources.length }, function(innerCb)
    {
        var source = sources.shift(),
            innerCb = _.once(innerCb),
            url = source.formatted ? source.formatted : util.format(source.url, [ task.infoHash.toUpperCase() ]);

        needle.get(url, {
            open_timeout: 2500, read_timeout: 2500,
            agent: module.exports.getAgent(),
            follow_max: 3, compressed: true,
            headers: { referer: source.url || source.formatted, "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.99 Safari/537.36" }
        }, function(err, res, body) {
            if (err) errors.push(err);
            else if (res && res.statusCode != 200) errors.push(new Error("status code "+res.statusCode));

            if (!body) return innerCb();
             
            try { 
                if (typeof(body) == "string") body = JSON.parse(body);
                body = body["piece length"] ? bencode.encode({ info: _.extend({ pieces: [] }, body) }) : body;
                result = parseTorrent(body);
                result.infoHash = task.infoHash;
                if (! result.files) throw new Error("no files found in torrent");
            } catch(e) { errors.push(e) };
            innerCb();
        });
    }, function()
    {
        if (! (result && result.infoHash)) return task.callback(errors.concat([new Error("Did not manage to download parsable torrent")]));
        task.callback(null, result);
    });
}, 2);
{'fraction_non_alphanumeric': 0.1544624972178945, 'fraction_numerical': 0.018695748942799913, 'mean_word_length': 2.566666666666667, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2079918', 'n_tokens_mistral': 1396, 'n_tokens_neox': 1306, 'n_words': 305}