<commit_before>(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        define(['superagent'], factory);
    } else if (typeof exports === 'object') {
        module.exports = factory(require('superagent'));
    } else {
        root.downloadTrack = factory(root.superagent);
    }
}(this, function (http) {
    var songs = [
        'http://freedownloads.last.fm/download/550191337/Alive.mp3',
        'http://freedownloads.last.fm/download/582135818/Together.mp3',
        'http://freedownloads.last.fm/download/508242732/Gu.mp3',
        'http://freedownloads.last.fm/download/653755146/Zonnestraal.mp3'
    ];

    function tryNext(cb) {
        songs.shift();

        if (songs.length){
            downloadTrack(cb);
        }
        else{
            cb(new Error('No more remaining tracks'));
        }
    }

    var downloadTrack = function(cb) {
        if (typeof cb !== 'function'){
            throw new TypeError('cb must be a function');
        }
        else{
            var data = [];

            console.log('Attempting to download ' + songs[0]);

            http.get(songs[0])
                .end(function(err, res){
                    if (err) { tryNext(cb); }
                    else{
                        res.on('data', function (chunk) {
                            data.push(chunk);
                        }).on('end', function () {
                            cb(null, Buffer.concat(data));
                        });
                    }
                });
        }
    };

    return downloadTrack;
}));<commit_msg>Check content disposition when downloading tracks<commit_after>(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        define(['superagent'], factory);
    } else if (typeof exports === 'object') {
        module.exports = factory(require('superagent'));
    } else {
        root.downloadTrack = factory(root.superagent);
    }
}(this, function (http) {
    var songs = [
        'http://freedownloads.last.fm/download/550191337/Alive.mp3',
        'http://freedownloads.last.fm/download/582135818/Together.mp3',
        'http://freedownloads.last.fm/download/508242732/Gu.mp3',
        'http://freedownloads.last.fm/download/653755146/Zonnestraal.mp3'
    ];

    function tryNext(cb) {
        songs.shift();

        if (songs.length){
            downloadTrack(cb);
        }
        else{
            cb(new Error('No more remaining tracks'));
        }
    }

    var downloadTrack = function(cb) {
        if (typeof cb !== 'function'){
            throw new TypeError('cb must be a function');
        }
        else{
            var data = [];

            console.log('Attempting to download ' + songs[0]);

            http.get(songs[0])
                .end(function(err, res){
                    var cd = res.header['content-disposition'];

                    if (err || cd.substr(cd.length - 3, cd.length) !== 'mp3') {
                        tryNext(cb);
                    }
                    else{
                        res.on('data', function (chunk) {
                            data.push(chunk);
                        }).on('end', function () {
                            cb(null, Buffer.concat(data));
                        });
                    }
                });
        }
    };

    return downloadTrack;
}));
{'fraction_non_alphanumeric': 0.14319526627218934, 'fraction_numerical': 0.02544378698224852, 'mean_word_length': 1.5007396449704142, 'pattern_counts': {'":': 0, '<': 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': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4869957', 'n_tokens_mistral': 1008, 'n_tokens_neox': 942, 'n_words': 170}