<commit_before>/*
 * grunt-crowdin-request
 * https://github.com/cloakedninjas/grunt-crowdin-request
 *
 * Copyright (c) 2014 <NAME>
 * Licensed under the MIT license.
 */

'use strict';

module.exports = function(grunt) {
  var fs = require('fs');
  var request = require('request');

  var config = {
    crowdinUrl: 'https://api.crowdin.com/api'
  };

  grunt.registerMultiTask('crowdin-request', 'Upload a .pot file to crowdin.net', function() {

    var done = this.async();

    switch (this.target) {
      case 'upload':
        initUpload(done, this.options(), this.data);
        break;

      case 'download':
        // TODO
        done();
        break;

      default:
        grunt.fail.warn('Unknown job: ' + this.target);
        done(false);
    }
  });

  /**
   * Prep the upload process, gather all input variables
   *
   * @param done
   * @param options
   * @param uploadOptions
   */
  function initUpload (done, options, uploadOptions) {
    var gitPattern = '#GIT_BRANCH#';
    if (uploadOptions.uploadFilename.indexOf(gitPattern) !== -1) {
      var git = require('git-rev');

      // get the current branch name

      git.branch(function (branchName) {
        grunt.verbose.writeln('Detected git branch: ' + branchName);
        uploadToCrowdin(done, uploadOptions.uploadFilename.replace(gitPattern, branchName), uploadOptions.srcFile, options);
      })
    }
    else {
      uploadToCrowdin(done, uploadOptions.uploadFilename, uploadOptions.srcFile, options);
    }
  }

  /**
   * Find out if file exists at Crowdin, then pick the correct API method to upload the file
   *
   * @param done
   * @param uploadFilename
   * @param uploadFile
   * @param options
   */
  function uploadToCrowdin(done, uploadFilename, uploadFile, options) {
    getStatus(options, function (err, response) {
      if (err) {
        done(false);
      }
      else {
        try {
          response = JSON.parse(response);
          var apiMethod = 'add-file';

          for (var i = 0, len = response.files.length; i < len; i++) {
            if (response.files[i].name === uploadFilename) {
              apiMethod = 'update-file';
              break;
            }
          }

          updateFileAtCrowdin(apiMethod, uploadFilename, uploadFile, options, done);
        }
        catch (e) {
          grunt.fail.warn('Unable to parse JSON');
          done(false);
        }
      }
    });
  }

  /**
   * Get the project current status
   *
   * @param options
   * @param callback
   */
  function getStatus(options, callback) {
    var url = buildUrl('info', options['project-identifier'], options['api-key']);

    grunt.verbose.writeln('Making POST request to: ' + url);

    request.post({
      url: url
    }, function (err, httpResponse, body) {
      if (err) {
        grunt.fail.warn('Get status failed: ' + err);
        callback(false, err);
      }
      else {
        grunt.verbose.writeln('Successfully got project info');

        callback(null, body);
      }
    });
  }

  /**
   *
   * @param method
   * @param uploadFilename
   * @param uploadFile
   * @param options
   * @param done
   */
  function updateFileAtCrowdin (method, uploadFilename, uploadFile, options, done) {
    var url = buildUrl(method, options['project-identifier'], options['api-key']);
    var formData = {};

    formData['files[' + uploadFilename + ']'] = fs.createReadStream(uploadFile);

    grunt.verbose.writeln('Making POST request to: ' + url);

    request.post({
      url: url,
      formData: formData
    }, function (err, httpResponse, body) {
      if (err) {
        grunt.fail.warn('Upload failed: ' + err);
        done(false);
      }
      else {
        grunt.verbose.writeln('Upload succeeded');
        done();
      }
    });
  }


  /**
   * Construct a URL to access the Crowdin API
   *
   * @param task
   * @param project
   * @param apiKey
   * @returns {string}
   */
  function buildUrl(task, project, apiKey) {
    var url = config.crowdinUrl + '/project/' + project + '/';

    switch (task) {
      case 'info':
        url += 'info';
        break;

      case 'add-file':
        url += 'add-file';
        break;

      case 'update-file':
        url += 'update-file';
        break;
    }

    url += '?key=' + apiKey + '&json=1';

    return url;
  }


};
<commit_msg>Crowdin error handled on getting status<commit_after>/*
 * grunt-crowdin-request
 * https://github.com/cloakedninjas/grunt-crowdin-request
 *
 * Copyright (c) 2014 <NAME>
 * Licensed under the MIT license.
 */

'use strict';

module.exports = function(grunt) {
  var fs = require('fs');
  var request = require('request');

  var config = {
    crowdinUrl: 'https://api.crowdin.com/api'
  };

  grunt.registerMultiTask('crowdin-request', 'Upload a .pot file to crowdin.net', function() {

    var done = this.async();

    switch (this.target) {
      case 'upload':
        initUpload(done, this.options(), this.data);
        break;

      case 'download':
        // TODO
        done();
        break;

      default:
        grunt.fail.warn('Unknown job: ' + this.target);
        done(false);
    }
  });

  /**
   * Prep the upload process, gather all input variables
   *
   * @param done
   * @param options
   * @param uploadOptions
   */
  function initUpload (done, options, uploadOptions) {
    var gitPattern = '#GIT_BRANCH#';
    if (uploadOptions.uploadFilename.indexOf(gitPattern) !== -1) {
      var git = require('git-rev');

      // get the current branch name

      git.branch(function (branchName) {
        grunt.verbose.writeln('Detected git branch: ' + branchName);
        uploadToCrowdin(done, uploadOptions.uploadFilename.replace(gitPattern, branchName), uploadOptions.srcFile, options);
      })
    }
    else {
      uploadToCrowdin(done, uploadOptions.uploadFilename, uploadOptions.srcFile, options);
    }
  }

  /**
   * Find out if file exists at Crowdin, then pick the correct API method to upload the file
   *
   * @param done
   * @param uploadFilename
   * @param uploadFile
   * @param options
   */
  function uploadToCrowdin(done, uploadFilename, uploadFile, options) {
    getStatus(options, function (err, response) {
      if (err) {
        done(false);
      }
      else {
        try {
          var apiMethod = 'add-file';

          for (var i = 0, len = response.files.length; i < len; i++) {
            if (response.files[i].name === uploadFilename) {
              apiMethod = 'update-file';
              break;
            }
          }

          updateFileAtCrowdin(apiMethod, uploadFilename, uploadFile, options, done);
        }
        catch (e) {
          grunt.fail.warn('Unable to parse JSON');
          done(false);
        }
      }
    });
  }

  /**
   * Get the project current status
   *
   * @param options
   * @param callback
   */
  function getStatus(options, callback) {
    var url = buildUrl('info', options['project-identifier'], options['api-key']);

    grunt.verbose.writeln('Making POST request to: ' + url);

    request.post({
      url: url
    }, function (err, httpResponse, body) {
      if (err) {
        grunt.fail.warn('Get status failed: ' + err);
        callback(false, err);
      }
      else {
        var response = JSON.parse(body);

        if (response.error) {
          grunt.fail.warn('Error from Crowdin: ' + response.error.message);
          callback(false, err);
        }
        else {
          grunt.verbose.writeln('Successfully got project info');
          callback(null, response);
        }
      }
    });
  }

  /**
   *
   * @param method
   * @param uploadFilename
   * @param uploadFile
   * @param options
   * @param done
   */
  function updateFileAtCrowdin (method, uploadFilename, uploadFile, options, done) {
    var url = buildUrl(method, options['project-identifier'], options['api-key']);
    var formData = {};

    formData['files[' + uploadFilename + ']'] = fs.createReadStream(uploadFile);

    grunt.verbose.writeln('Making POST request to: ' + url);

    request.post({
      url: url,
      formData: formData
    }, function (err, httpResponse, body) {
      if (err) {
        grunt.fail.warn('Upload failed: ' + err);
        done(false);
      }
      else {
        grunt.verbose.writeln('Upload succeeded');
        done();
      }
    });
  }


  /**
   * Construct a URL to access the Crowdin API
   *
   * @param task
   * @param project
   * @param apiKey
   * @returns {string}
   */
  function buildUrl(task, project, apiKey) {
    var url = config.crowdinUrl + '/project/' + project + '/';

    switch (task) {
      case 'info':
        url += 'info';
        break;

      case 'add-file':
        url += 'add-file';
        break;

      case 'update-file':
        url += 'update-file';
        break;
    }

    url += '?key=' + apiKey + '&json=1';

    return url;
  }


};

{'fraction_non_alphanumeric': 0.13746478873239437, 'fraction_numerical': 0.0015774647887323943, 'mean_word_length': 2.207806288398988, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7354029', 'n_tokens_mistral': 2788, 'n_tokens_neox': 2671, 'n_words': 651}