<commit_before>// This is a port foor node.js of this great article's code:
// http://cgbystrom.com/articles/deconstructing-spotifys-builtin-http-server/

var request = require('request')
var qs = require('querystring')
var util = require('util');
var path = require('path');
var child_process = require('child_process');

// global variables, used when running on windows
var wintools;
var spotifyWebHelperWinProcRegex;

// Default port that Spotify Web Helper binds to.
var DEFAULT_PORT = 4370;
var DEFAULT_RETURN_ON = ['login', 'logout', 'play', 'pause', 'error', 'ap']
var DEFAULT_RETURN_AFTER = 1
var ORIGIN_HEADER = {
    'Origin': 'https://open.spotify.com',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.148 Safari/537.36 Vivaldi/1.4.589.38'
}

function getJson(url, params, headers, cb) {
    if (params instanceof Function) {
        cb = params;
        params = null;
        headers = null;
    }

    if (headers instanceof Function) {
        cb = headers;
        headers = null;
    }

    headers = headers || {}
    cb = cb || function () { };
    if (params)
        url += '?' + qs.stringify(params)

    console.log(url, headers)

    // rejectUnauthorized:false should be ok here since we are working with localhost
    // this fixes the UNABLE_TO_VERIFY_LEAF_SIGNATURE error
    request({ 'url': url, 'headers': headers, 'rejectUnauthorized' : false}, function (err, req, body) {
        if (err) {
            return cb(err);
        }

        var parsedBody;
        try {
            parsedBody = JSON.parse(body);
            console.log(parsedBody)
        }
        catch (e) {
            return cb(e);
        }

        return cb(null, parsedBody);
    });
}

var ASCII_LOWER_CASE = "abcdefghijklmnopqrstuvwxyz";
// http://stackoverflow.com/questions/1349404/generate-a-string-of-5-random-characters-in-javascript
function generateRandomString(length) {
    var text = "";

    for( var i=0; i < 10; i++ )
        text += ASCII_LOWER_CASE.charAt(Math.floor(Math.random() * ASCII_LOWER_CASE.length));

    return text;
}

function generateRandomLocalHostName() {
    // Generate a random hostname under the .spotilocal.com domain
    return generateRandomString(10) + '.spotilocal.com'
}

function getOauthToken(cb) {
    return getJson('http://open.spotify.com/token', null, ORIGIN_HEADER, function (err, res) {
        if (err) {
            return cb(err);
        }

        return cb(null, res['t']);
    });
}

function isSpotifyWebHelperRunning(cb) {
  cb = cb || function () { };
  // not doing anything for non windows, for now
  if (process.platform != 'win32')  {
    return cb(null, true);
  }

  wintools = wintools || require('wintools');
  wintools.ps(function (err, lst) {
    if (err) {
      return cb(err);
    }

    spotifyWebHelperWinProcRegex = spotifyWebHelperWinProcRegex || new RegExp('spotifywebhelper.exe', 'i');

    for (var k in lst) {
      if (spotifyWebHelperWinProcRegex.test(lst[k].desc)) {
        return cb(null, true);
      }
      spotifyWebHelperWinProcRegex.lastIndex = 0;
    };
    cb(null, false);
  });
}

function getWindowsSpotifyWebHelperPath() {
  if (!process.env.USERPROFILE) {
    return null;
  }

  return path.join(process.env.USERPROFILE, 'AppData\\Roaming\\Spotify\\Data\\SpotifyWebHelper.exe');
}

function launchSpotifyWebhelperIfNeeded(cb) {
  cb = cb || function () { };
  // not doing anything for non windows, for now
  if (process.platform != 'win32') {
    return cb(null, true);<commit_msg>Update spotify web helper paths on Windows<commit_after>// This is a port foor node.js of this great article's code:
// http://cgbystrom.com/articles/deconstructing-spotifys-builtin-http-server/

var request = require('request')
var fs = require('fs');
var qs = require('querystring')
var util = require('util');
var path = require('path');
var child_process = require('child_process');

// global variables, used when running on windows
var wintools;
var spotifyWebHelperWinProcRegex;

// Default port that Spotify Web Helper binds to.
var DEFAULT_PORT = 4370;
var DEFAULT_RETURN_ON = ['login', 'logout', 'play', 'pause', 'error', 'ap']
var DEFAULT_RETURN_AFTER = 1
var ORIGIN_HEADER = {
    'Origin': 'https://open.spotify.com',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.148 Safari/537.36 Vivaldi/1.4.589.38'
}

function getJson(url, params, headers, cb) {
    if (params instanceof Function) {
        cb = params;
        params = null;
        headers = null;
    }

    if (headers instanceof Function) {
        cb = headers;
        headers = null;
    }

    headers = headers || {}
    cb = cb || function () { };
    if (params)
        url += '?' + qs.stringify(params)

    console.log(url, headers)

    // rejectUnauthorized:false should be ok here since we are working with localhost
    // this fixes the UNABLE_TO_VERIFY_LEAF_SIGNATURE error
    request({ 'url': url, 'headers': headers, 'rejectUnauthorized' : false}, function (err, req, body) {
        if (err) {
            return cb(err);
        }

        var parsedBody;
        try {
            parsedBody = JSON.parse(body);
            console.log(parsedBody)
        }
        catch (e) {
            return cb(e);
        }

        return cb(null, parsedBody);
    });
}

var ASCII_LOWER_CASE = "abcdefghijklmnopqrstuvwxyz";
// http://stackoverflow.com/questions/1349404/generate-a-string-of-5-random-characters-in-javascript
function generateRandomString(length) {
    var text = "";

    for( var i=0; i < 10; i++ )
        text += ASCII_LOWER_CASE.charAt(Math.floor(Math.random() * ASCII_LOWER_CASE.length));

    return text;
}

function generateRandomLocalHostName() {
    // Generate a random hostname under the .spotilocal.com domain
    return generateRandomString(10) + '.spotilocal.com'
}

function getOauthToken(cb) {
    return getJson('http://open.spotify.com/token', null, ORIGIN_HEADER, function (err, res) {
        if (err) {
            return cb(err);
        }

        return cb(null, res['t']);
    });
}

function isSpotifyWebHelperRunning(cb) {
  cb = cb || function () { };
  // not doing anything for non windows, for now
  if (process.platform != 'win32')  {
    return cb(null, true);
  }

  wintools = wintools || require('wintools');
  wintools.ps(function (err, lst) {
    if (err) {
      return cb(err);
    }

    spotifyWebHelperWinProcRegex = spotifyWebHelperWinProcRegex || new RegExp('spotifywebhelper.exe', 'i');

    for (var k in lst) {
      if (spotifyWebHelperWinProcRegex.test(lst[k].desc)) {
        return cb(null, true);
      }
      spotifyWebHelperWinProcRegex.lastIndex = 0;
    };
    cb(null, false);
  });
}

function getWindowsSpotifyWebHelperPath() {
  if (!process.env.USERPROFILE) {
    return null;
  }

  let paths = [
    'AppData\\Roaming\\Spotify\\Data\\SpotifyWebHelper.exe',
    'AppData\\Roaming\\Spotify\\SpotifyWebHelper.exe'
  ].map(x => path.join(process.env.USERPROFILE, x))
  .filter(x => fs.existsSync(x));

  return paths[0] || '';
}

function launchSpotifyWebhelperIfNeeded(cb) {
  cb = cb || function () { };
  // not doing anything for non windows, for now
  if (process.platform != 'win32') {
    return cb(null, true);
{'fraction_non_alphanumeric': 0.13724685276409415, 'fraction_numerical': 0.015736179529282978, 'mean_word_length': 2.9766050054406965, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '4905848', 'n_tokens_mistral': 2496, 'n_tokens_neox': 2361, 'n_words': 576}