<commit_before>// Dependencies
const BPromise = require('Bluebird');
const rest = require('restler');
const crypto = require('crypto')
const config = require('config')
const _ = require('lodash')

// Constants
const apiKey = config.fatsecret.consumerKey
const sharedSecret = config.fatsecret.sharedKey
const fatSecretRestUrl = 'http://platform.fatsecret.com/rest/server.api'
const encodedFSUrl = encodeURIComponent(fatSecretRestUrl)
const date = new Date();


// Note that the keys are in alphabetical order
const baseReqObj = () => {
  return {
    format: 'json',
    oauth_consumer_key: apiKey,
    oauth_nonce: Math.random().toString(36).replace(/[^a-z]/, '').substr(2),
    oauth_signature_method: 'HMAC-SHA1',
    oauth_timestamp: Math.floor(date.getTime() / 1000),
    oauth_version: '1.0'
  }
};

const getOAuthSignature = (request, access_token='') =>
  crypto.createHmac('sha1', [sharedSecret, access_token].join('&'))
    .update(buildSigBaseStr(request)).digest('base64');

// construct a param=value& string and uriEncode
const buildSigBaseStr = request => {
  const keys = Object.keys(request).sort()
  const parameterString = _.map(keys, key => key + '=' + request[key]).join('&')
  return `POST&${encodedFSUrl}&${encodeURIComponent(parameterString)}`;
}

const foodSearch = (expression) => new BPromise(resolve => {
  const reqObj = {
    format: 'json',
    method: 'foods.search',
    oauth_consumer_key: apiKey,
    oauth_nonce: Math.random().toString(36).replace(/[^a-z]/, '').substr(2),
    oauth_signature_method: 'HMAC-SHA1',
    oauth_timestamp: Math.floor(date.getTime() / 1000),
    oauth_version: '1.0'
  };
  reqObj.search_expression = expression
  reqObj.oauth_signature = getOAuthSignature(reqObj);

  rest.post(fatSecretRestUrl, {
    data: reqObj,
  }).on('complete', (data, response) => {
    console.log(response)
    resolve(data);
  });
});


const fatsecret = {
  food: {
    search: foodSearch
  }
}

module.exports = fatsecret;

// example
// fatsecret.food.search('hamburger')<commit_msg>fix(fatscret): food names with spaces<commit_after>// Dependencies
const BPromise = require('Bluebird');
const rest = require('restler');
const crypto = require('crypto')
const config = require('config')
const _ = require('lodash')

// Constants
const apiKey = config.fatsecret.consumerKey
const sharedSecret = config.fatsecret.sharedKey
const fatSecretRestUrl = 'http://platform.fatsecret.com/rest/server.api'
const encodedFSUrl = encodeURIComponent(fatSecretRestUrl)
const date = new Date();


// Note that the keys are in alphabetical order
const baseReqObj = () => {
  return {
    format: 'json',
    oauth_consumer_key: apiKey,
    oauth_nonce: Math.random().toString(36).replace(/[^a-z]/, '').substr(2),
    oauth_signature_method: 'HMAC-SHA1',
    oauth_timestamp: Math.floor(date.getTime() / 1000),
    oauth_version: '1.0'
  }
};

const getOAuthSignature = (request, access_token='') =>
  crypto.createHmac('sha1', [sharedSecret, access_token].join('&'))
    .update(buildSigBaseStr(request)).digest('base64');

// construct a param=value& string and uriEncode
const buildSigBaseStr = request => {
  const keys = Object.keys(request).sort()
  const parameterString = _.map(keys, key => key + '=' + encodeURIComponent(request[key])).join('&')
  return `POST&${encodedFSUrl}&${encodeURIComponent(parameterString)}`;
}

const foodSearch = (expression) => new BPromise(resolve => {
  const reqObj = {
    format: 'json',
    method: 'foods.search',
    oauth_consumer_key: apiKey,
    oauth_nonce: Math.random().toString(36).replace(/[^a-z]/, '').substr(2),
    oauth_signature_method: 'HMAC-SHA1',
    oauth_timestamp: Math.floor(date.getTime() / 1000),
    oauth_version: '1.0'
  };
  reqObj.search_expression = expression
  reqObj.oauth_signature = getOAuthSignature(reqObj);

  rest.post(fatSecretRestUrl, {
    data: reqObj,
  }).on('complete', (data, response) => {
    console.log(response)
    resolve(data);
  });
});


const fatsecret = {
  food: {
    search: foodSearch
  }
}

module.exports = fatsecret;

// example
// fatsecret.food.search('hot dog')
{'fraction_non_alphanumeric': 0.1593460224499756, 'fraction_numerical': 0.011224987798926306, 'mean_word_length': 4.5997267759562845, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, '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': '1656982', 'n_tokens_mistral': 1394, 'n_tokens_neox': 1347, 'n_words': 264}