Skip to content

Instantly share code, notes, and snippets.

@garyhodgson
Created October 26, 2008 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save garyhodgson/19959 to your computer and use it in GitHub Desktop.
Save garyhodgson/19959 to your computer and use it in GitHub Desktop.
Ubiquity command to perform a 'Blended' search in Amazon Germany, UK, USA, Japan or Canada.
/**
* 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 <b>" + directObject.summary + "</b>";
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 <b>${query}</b>";
ptemplate = "<div style=\"min-height:400px;max-height: "+h+"px;overflow-y: scroll;\">";
ptemplate += "{for item in items}";
ptemplate += "<div style='clear: both; padding: 10px 0px;'>";
ptemplate += "<a href='${item.url}'> {if item.image}<img src='${item.image.src}' style='float: left; margin-right: 10px; height: ${item.image.height}px; width: ${item.image.width}px;'/>{/if}<u>${item.title}</u></a>";
ptemplate += "<small>{if item.author}<br />by ${item.author}{/if}{if item.price} <br />for ${item.price.amount} (${item.price.currency}){/if}</small>";
ptemplate += "</div>";
ptemplate += "{/for}";
ptemplate += "</div>";
previewBlock.innerHTML = CmdUtils.renderTemplate(ptemplate, previewData);
}
});
},
execute: function(directObject, mods) {
var country = mods.from.text || 'UK';
var locale = ALM[country.toUpperCase()] || 'co.uk';
var url = "http://www.amazon."+locale+"/s/ref=nb_ss_gw?url=search-alias%3Daps&field-keywords={QUERY}&x=0&y=0";
var query = directObject.text.replace(" ", "+");
var urlString = url.replace("{QUERY}", query);
Utils.openUrlInBrowser(urlString);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment