<commit_before>// Cloned from https://github.com/cassidoo/clarifai-javascript-starter
module.exports = function (imgurl) {
  console.log("INSIDE clarifai.js!");
  console.log(imgurl);
  var $ = require('jQuery');
  //tags to be returned
  var result_tags;


  var LocalStorage = require('node-localstorage').LocalStorage,
  localStorage = new LocalStorage('./scratch');

  function getCredentials(cb) {
    console.log("getCredentials() is running!");
    var data = {
      grant_type: "client_credentials",
      client_id: "LPOXuuwXLHA2yZ7fBrN_DAHTsu26s2mR9h4DVmMa",
      client_secret: "<KEY>"
    };

    return $.ajax({
      type: "POST",
      url: "https://api.clarifai.com/v1/token",
      data: data
    })
    .then(function(r) {
      localStorage.setItem('accessToken', r.access_token);
      localStorage.setItem('tokenTimestamp', Math.floor(Date.now() / 1000));
      console.log(("ajax in getCredentials ran!"));
      cb();
    });
  }

  function postImage(imgurl) {
    var data = {
      url: imgurl
    };
    var accessToken = localStorage.getItem('accessToken');
    console.log(accessToken);

    $.ajax({ // TODO This ajax is not running. Once it runs, everything should work
      type: "POST",
      url: "https://api.clarifai.com/v1/tag",
      headers: {
        Authorization: "Bearer " + accessToken
      },
      data: data
    }).then(function(r){
      console.log("ajax in postImage ran!");
      result_tags = parseResponse(r);
    });
  }

  function parseResponse(resp) {
    var tags = [];
    if (resp.status_code === 'OK') {
      var results = resp.results;
      tags = results[0].result.tag.classes;
      console.log("clarifai.js is parsing tags!");
    } else {
      console.log('Sorry, something is wrong.');
    }

    $('#tags').text(tags.toString().replace(/,/g, ', '));
    console.log("clarifai.js tags is about to be returned!");
    console.log(tags);
    result_tags = tags;
    return tags;
  }

  if (localStorage.getItem('tokenTimeStamp') - Math.floor(Date.now() / 1000) > 86400
      || localStorage.getItem('accessToken') === null) {
    getCredentials(function() {
      console.log("getCredentials callback is being called!");
      postImage(imgurl); // IMPLTMENT HIS !
    });
  } else {
    postImage(imgurl);
  }
  console.log("everything ran!");
  return result_tags;
} <commit_msg>Use older ver of clarifai.js<commit_after>// Cloned from https://github.com/cassidoo/clarifai-javascript-starter
module.exports = function (imgurl) {
  console.log("INSIDE clarifai.js!");
  console.log(imgurl);

  var $ = require('jQuery');

  var LocalStorage = require('node-localstorage').LocalStorage,
  localStorage = new LocalStorage('./scratch');

  function getCredentials(cb) {
    console.log("getCredentials() is running!");
    var data = {
      grant_type: 'client_credentials',
      client_id: "LPOXuuwXLHA2yZ7fBrN_DAHTsu26s2mR9h4DVmMa",
      client_secret: "<KEY>"
    };

    return $.ajax({
      type: 'POST',
      url: 'https://api.clarifai.com/v1/token',
      data: data
    })
    .then(function(r) {
      localStorage.setItem('accessToken', r.access_token);
      localStorage.setItem('tokenTimestamp', Math.floor(Date.now() / 1000));
      console.log(("ajax in getCredentials ran!"));
      cb();
    });
  }

  function postImage(imgurl) {
    console.log("postImage is running!!");
    var data = {
      url: imgurl
    };
    var accessToken = localStorage.getItem('accessToken');
    console.log(accessToken);

    $.ajax({
      type: 'POST',
      url: 'https://api.clarifai.com/v1/tag',
      headers: {
        Authorization: 'Bearer ' + accessToken
      },
      data: data
    }).then(function(r){
      console.log("ajax in postImage ran!");
      return parseResponse(r);
    });
  }

  function parseResponse(resp) {
    var tags = [];
    if (resp.status_code === 'OK') {
      var results = resp.results;
      tags = results[0].result.tag.classes;
      console.log("clarifai.js working ok!");
    } else {
      console.log('Sorry, something is wrong.');
    }

    $('#tags').text(tags.toString().replace(/,/g, ', '));
    console.log("clarifai.js tags is about to be returned!");
    return tags;
  }

  if (localStorage.getItem('tokenTimeStamp') - Math.floor(Date.now() / 1000) > 86400
      || localStorage.getItem('accessToken') === null) {
    getCredentials(function() {
      console.log("getCredentials callback is being called!");
      postImage(imgurl);
    });
  } else {
    postImage(imgurl);
  }
}
{'fraction_non_alphanumeric': 0.15991140642303434, 'fraction_numerical': 0.010188261351052049, 'mean_word_length': 2.9718557607739666, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 6, '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': '166320', 'n_tokens_mistral': 1496, 'n_tokens_neox': 1462, 'n_words': 283}