<commit_before>var exports = module.exports = {};
var https = require('https');

var defaultOptions = {
    host : 'api.spotify.com',
    port : 443,
    path : '/v1'
};

exports.getPlaylist = function(word, callback){
    //clone default options
    var options = JSON.parse(JSON.stringify(defaultOptions));
    options.method = 'GET';
    options.path += '/search?type=playlist&limit=1&q=';
    options.path += word;
    makeRequest(options, callback);
};

// given an options JSON, make a request
function makeRequest(options, callback) {
    var response;
    var req = https.request(options, function(res) {
        res.on('data', function(d) {
            callback(d); // execute call back to serve the data as the response
        });
    });
    req.end();
    req.on('error', function(e) {
        console.error(e);
    });
}<commit_msg>addressed merge conflicts, added authorization (client_credentials) and getTrackList<commit_after>var exports = module.exports = {};
var https = require('https');

function encode(string){
    return new Buffer(string).toString('base64');
}

function authorize(callback){
    var body = 'grant_type=client_credentials';
    var headers = {
        'Authorization' : 'Basic ' + encode(process.env.CLIENT_ID + ':' + process.env.CLIENT_SECRET),
        'Content-Type' : 'application/x-www-form-urlencoded',
        'Content-Length' : Buffer.byteLength(body, 'utf8')
    };
    var options = {
        host : 'accounts.spotify.com',
        method: 'POST',
        path : '/api/token',
        headers : headers
    };
    makeRequest(options, function(data){
        var access_token = data.access_token;
        callback(access_token);
    }, body);
}

// given a JSON playlist = { ownerID: "...", playlistID: "..." }
exports.getTrackList = function(playlist, callback){
    authorize(function(access_token){
        var headers = {
            'Authorization' : 'Bearer ' + access_token,
            'Content-Type' : 'application/json'
        };
        var options = {
            host : 'api.spotify.com',
            method : 'GET',
            path : '/v1/users/' + playlist.ownerID + '/playlists/' + playlist.playlistID + '/tracks?market=ES',
            headers : headers
        };
        makeRequest(options, function(data){
            console.log(data);
            callback(data);
        });
    });
}

exports.getPlaylist = function(word, callback){
    var options = {
        host : 'api.spotify.com',
        method : 'GET',
        path : '/v1//search?type=playlist&limit=1&q=' + word,
        headers : headers
    };
    makeRequest(options, function(data){
        console.log(data);
        callback(data);
    });
};

// given an options JSON, make a request
function makeRequest(options, callback, reqBody) {
    var req = https.request(options, function(res) {
        var resBody = '';

        // add each data chunk
        res.on('data', function(data) {
            data = data.toString("utf-8"); // read the Buffer
            resBody += data
        });

        res.on('end', function() {
            resBody = JSON.parse(resBody);
            callback(resBody);
        });
    });

    // send request body if it exists
    if(reqBody !== undefined){
        req.write(reqBody);
    }

    req.end();
    req.on('error', function(e) {
        console.error(e);
    });
}
{'fraction_non_alphanumeric': 0.14920919128618323, 'fraction_numerical': 0.0035810205908683975, 'mean_word_length': 2.3927125506072873, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3972451', 'n_tokens_mistral': 993, 'n_tokens_neox': 982, 'n_words': 216}