danito (owner)

Fork Of

gist: 19959 by garyhod... Ubiquity command to perform...

Revisions

gist: 65654 Download_button fork
public
Description:
Fork of amazon locale to include .fr
Public Clone URL: git://gist.github.com/65654.git
Embed All Files: show embed
ubiquity-amazon-local.js #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* Ubiquity command to perform a 'Blended' search in Amazon Germany, UK, USA, Japan, France 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", "fr":"France"} )},
  
  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);
   }
  
});