<commit_before>var sys = require('sys'),
OperationHelper = require('apac').OperationHelper;
var _ = require('underscore');
var isbnlib = require('./isbn');
var aws_credentials = require('./aws_cred');

opHelper = new OperationHelper({
    awsId:     aws_credentials.awsId,
    awsSecret: aws_credentials.awsSecret,
    assocId:   aws_credentials.assocId,
});

// Lookup a book by ISBN-13, and get product information from AWS
isbn_lookup_unthrottled = function(isbn_dirty, callback) {
    // normalize ISBN to 13:
    var isbn = isbnlib.to_isbn_13(isbn_dirty)
    if (_.isNull(isbn)) {
        sys.print("Provided ISBN could not be parsed. ("+isbn+")\n");
    }
    var idtype = "EAN";
    opHelper.execute('ItemLookup', {
        'SearchIndex': 'Books',
        'IdType': idtype,
        'ItemId': isbn,
        'ResponseGroup': 'ItemAttributes,Images'
    }, function(error, results) {
        if (error) { sys.print('Error: ' + error + "\n") }
        //sys.print("Results:\n" + sys.inspect(results) + "\n");
        if (!! _.isUndefined(results.Items.Item)) {
        } else if (results.Items.Item.constructor == Array) {
            sys.print("This query returned multiple books, we'll just blindly take the first for now.");
            // We need to figure out which one of these items we should display...
            // for now we just take the first.
            callback(error, results.Items.Item.shift());
        } else {
            //sys.print(sys.inspect(results.Items.Item));
            callback(error, results.Items.Item);
        }
    });
}

// Amazon prefers 1 second between calls, or 503 errors become likely.
var throttle_time = 1100
var previous_run = 0;

// This runs an ISBN lookup, ensuring that calls are spaced at least
// 'throttle_time' apart.
exports.isbn_lookup = function() {
    var context = this, args = arguments;
    var delta = +new Date() - previous_run;
    previous_run = +new Date();
    if (delta > throttle_time) {
        isbn_lookup_unthrottled.apply(context,args);
    } else {
        sys.print("(throttling AWS for "+(throttle_time-delta)/1000+" seconds)\n");
        setTimeout(function() {
            isbn_lookup_unthrottled.apply(context,args);
        }, throttle_time - delta);
    }
}<commit_msg>Use underscore's throttle method, instead of our broken one.<commit_after>var sys = require('sys'),
OperationHelper = require('apac').OperationHelper;
var _ = require('underscore');
var isbnlib = require('./isbn');
var aws_credentials = require('./aws_cred');

opHelper = new OperationHelper({
    awsId:     aws_credentials.awsId,
    awsSecret: aws_credentials.awsSecret,
    assocId:   aws_credentials.assocId,
});

// Lookup a book by ISBN-13, and get product information from AWS
isbn_lookup_unthrottled = function(isbn_dirty, callback) {
    // normalize ISBN to 13:
    var isbn = isbnlib.to_isbn_13(isbn_dirty)
    if (_.isNull(isbn)) {
        sys.print("Provided ISBN could not be parsed. ("+isbn+")\n");
    }
    var idtype = "EAN";
    opHelper.execute('ItemLookup', {
        'SearchIndex': 'Books',
        'IdType': idtype,
        'ItemId': isbn,
        'ResponseGroup': 'ItemAttributes,Images'
    }, function(error, results) {
        if (error) { sys.print('Error: ' + error + "\n") }
        //sys.print("Results:\n" + sys.inspect(results) + "\n");
        if (!! _.isUndefined(results.Items.Item)) {
        } else if (results.Items.Item.constructor == Array) {
            sys.print("This query returned multiple books, we'll just blindly take the first for now.");
            // We need to figure out which one of these items we should display...
            // for now we just take the first.
            callback(error, results.Items.Item.shift());
        } else {
            //sys.print(sys.inspect(results.Items.Item));
            callback(error, results.Items.Item);
        }
    });
}

// Amazon prefers 1 second between calls, or 503 errors become likely.
exports.isbn_lookup = _.throttle(isbn_lookup_unthrottled, 1100);
{'fraction_non_alphanumeric': 0.13680538250685273, 'fraction_numerical': 0.008223274358335409, 'mean_word_length': 2.8448275862068964, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '6908557', 'n_tokens_mistral': 1260, 'n_tokens_neox': 1220, 'n_words': 320}