<commit_before>var request = require('request')
  , _ = require('underscore')
  , Q = require('q')
  , querystring = require('querystring')

var models_16g = {
  'ME341LL/A': 'iphone 5s space gray verizon 16gb',
  'ME343LL/A': 'iphone 5s gold verizon 16gb',
  'ME342LL/A': 'iphone 5s silver verizon 16gb',
};

var ZIPCODE = 94043;
var PHONE_NUMBER = null;   // '5555555555'

function check(url) {
  var deferred = Q.defer();
  request(url, function(err, resp, body) {
    obj = JSON.parse(body);
    if (!obj.body || !obj.body.success || !obj.body.stores) {
      deferred.reject();
      return;
    }
    deferred.resolve(obj.body.stores);
  });
  return deferred.promise;
}

function checkStore(store, model, desc) {
  if (store.partsAvailability[model].pickupDisplay !== 'unavailable') {
    var msg = 'phone available: ' +  desc + ' @ ' + store.storeDisplayName;
    console.info(msg);
    return true;
  }
  return false;
}

function checkStores(stores, model, desc) {
  var any = _.some(stores, function(store) {
    return checkStore(store, model, desc);
  });

  if (any && PHONE_NUMBER) {
    request.post({
      uri:'http://textbelt.com/text',
      headers:{'content-type': 'application/x-www-form-urlencoded'},
      body: querystring.stringify({
        number: ''+PHONE_NUMBER,
        message: desc + ' is available in your area.',
      }, function(err, resp, body) {
        console.log(arguments);
      })
    });
  }
}

_.each(models_16g, function(desc, model) {
  var modeluri = encodeURIComponent(model);
  var url = 'http://store.apple.com/us/retail/availabilitySearch?parts.0=' + modeluri + '&zip=' + ZIPCODE;
  check(url).then(function(stores) {
    checkStores(stores, model, desc);
  }).fail(function(err) {
    console.log('search failed');
    console.error(err);
  });
});
<commit_msg>working texting<commit_after>/**
 * appe-monitor script
 * MIT License
 *
 * @author <NAME> (ianww.com)
 */

var request = require('request')
  , _ = require('underscore')
  , Q = require('q')

var models_16g = {
  'ME341LL/A': 'iphone 5s space gray verizon 16gb',
  'ME343LL/A': 'iphone 5s gold verizon 16gb',
  'ME342LL/A': 'iphone 5s silver verizon 16gb',
  // Add products you're interested in.
};

if (process.argv.length < 3) {
  console.log('usage: node apple.js zipcode');
  process.exit(1);
}

var ZIPCODE = process.argv[2];
var PHONE_NUMBER = null;
if (process.argv.length > 3) {
  PHONE_NUMBER = process.argv[3];
}

function check(url) {
  var deferred = Q.defer();
  request(url, function(err, resp, body) {
    obj = JSON.parse(body);
    if (!obj.body || !obj.body.success || !obj.body.stores) {
      deferred.reject();
      return;
    }
    deferred.resolve(obj.body.stores);
  });
  return deferred.promise;
}

function checkStore(store, model, desc) {
  if (store.partsAvailability[model].pickupDisplay !== 'unavailable') {
    var msg = 'phone available: ' +  desc + ' @ ' + store.storeDisplayName;
    console.info(msg);
    return true;
  }
  return false;
}

function checkStores(stores, model, desc) {
  var avail_stores  = [];
  var any = _.some(stores, function(store) {
    var avail = checkStore(store, model, desc);
    if (avail) {
      avail_stores.push(store);
    }
    return avail;
  });

  if (any && PHONE_NUMBER) {
    request.post('http://textbelt.com/text',
      { form: {
          number: ''+PHONE_NUMBER,
          message: desc + ' is available at ' + avail_stores[0].storeName,
        }
      }, function(err, resp, body) {
      });
  }
}

_.each(models_16g, function(desc, model) {
  var modeluri = encodeURIComponent(model);
  var url = 'http://store.apple.com/us/retail/availabilitySearch?parts.0=' + modeluri + '&zip=' + ZIPCODE;
  check(url).then(function(stores) {
    checkStores(stores, model, desc);
  }).fail(function(err) {
    console.log('search failed');
    console.error(err);
  });
});

{'fraction_non_alphanumeric': 0.16563307493540053, 'fraction_numerical': 0.01731266149870801, 'mean_word_length': 3.3396860986547083, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, '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': '1701674', 'n_tokens_mistral': 1419, 'n_tokens_neox': 1350, 'n_words': 282}