/** * Ubiquity command to perform a 'Blended' search in Amazon Germany, UK, USA, Japan or Canada. */ /** * Core Type Extension * Now returns the index of the suggestion array as the data element */ function ModNounType(name, expectedWords) {this._init(name, expectedWords);}; var F = function() {}; F.prototype = CmdUtils.NounType.prototype; ModNounType.prototype = new F(); ModNounType.prototype.suggest = function(text, html) { var suggestions = []; if (typeof text != "string") { return []; } for (var x in this._expectedWords) { var word = this._expectedWords[x].toLowerCase(); if (word.indexOf(text.toLowerCase()) > -1) { suggestions.push(CmdUtils.makeSugg(word, word, x)); } } return suggestions; } CmdUtils.CreateCommand({ name: "amazon-local", author: { name: "Gary Hodgson", homepage : "http://www.garyhodgson.com/", email : "contact@garyhodgson.com"}, icon: "http://www.amazon.com/favicon.ico", description: "Adaption of the built-in Amazon command. Perform a 'Blended' search in Amazon Germany, UK, USA, Japan or Canada.", help: "The search type used is 'Blended', and not limited to Books, however using 'Book' or 'DVD' in the search criteria seems to help narrow the results if needed.", homepage: "http://garyhodgson/ubiquity", license: "MPL", takes: {"keywords" : noun_arb_text}, modifiers: {"from" : new ModNounType( "Country", {"de":"Germany", "co.uk":"UK", "com":"USA", "jp":"Japan", "ca":"Canada"} )}, preview: function(previewBlock, directObject, mods) { if(!directObject.text || directObject.text.length < 1) { previewBlock.innerHTML = "Searches for items on Amazon"; return; } var country = mods.from.text || 'UK'; var locale = mods.from.data || 'co.uk'; previewBlock.innerHTML = "Searching Amazon."+locale+" for items matching " + directObject.summary + ""; var apiUrl = "http://ecs.amazonaws."+locale+"/onca/xml"; var apiParams = { Service: "AWSECommerceService", AWSAccessKeyId: "1VDMBQXHG2NNSG2D0682", Version: "2008-08-19", Operation: "ItemSearch", Condition: "All", ResponseGroup: "ItemAttributes,Images", SearchIndex: "Blended", Keywords: directObject.text }; jQuery.ajax({ type: "GET", url: apiUrl, data: apiParams, dataType: "xml", error: function() { previewBlock.innerHTML = "Error searching Amazon."+locale; }, success: function(responseData) { const AMAZON_MAX_RESULTS = 20; responseData = jQuery(responseData); var items = []; responseData.find("Items Item").slice(0, AMAZON_MAX_RESULTS).each(function(itemIndex) { var itemDetails = jQuery(this); var newItem = { title: itemDetails.find("ItemAttributes Title").text(), url: itemDetails.find("DetailPageURL").text() }; if (itemDetails.find("ItemAttributes Author").length > 0) { newItem.author = itemDetails.find("ItemAttributes Author").text(); } else if (itemDetails.find("ItemAttributes Actor").length > 0) { newItem.author = itemDetails.find("ItemAttributes Actor").text(); } if(itemDetails.find("ItemAttributes ListPrice").length > 0) { newItem.price = { amount: itemDetails.find("ItemAttributes ListPrice FormattedPrice").text(), currency: itemDetails.find("ItemAttributes ListPrice CurrencyCode").text() }; } if(itemDetails.find("SmallImage").length > 0) { newItem.image = { src: itemDetails.find("SmallImage:first URL").text(), height: itemDetails.find("SmallImage:first Height").text(), width: itemDetails.find("SmallImage:first Width").text() }; } items.push(newItem); }); var previewData = { query: directObject.summary, numitems: responseData.find("Items TotalResults").text(), items: items }; var w = CmdUtils.getWindowInsecure(); var offset = w.outerHeight - w.innerHeight var h = (offset < w.innerHeight)? w.innerHeight - offset : w.innerHeight; var ptemplate = "Found ${numitems} items on Amazon."+locale+" matching ${query}"; ptemplate = "