oosAPI = {}
oosAPI = {
API_URL: "http://11870.com/api/v1/search?",
KONSULTO_URL: "http://11870.com/konsulto/",
AUTHSIGN: "d576a33193e39d4fa3ca9cfd07afa411",
APPTOKEN: "6d055f77b749e509f9bc65b445b922c0",
RESULT_LIMIT: 4,
resultsTmpl: "{for item in items}" +
"<span><a style=\"text-decoration:underline; font-weight:bold;"+
"font-size:1em; color:#06C\" href=\"${item.oosUrl}\" />${item.site_name}</a> "+
"${item.phone}</span>" +
"<br/><span style=\"font-size:.8em\">${item.adr} (${item.locality})</span>"+
"{if item.url}"+
"<br/><a style=\"text-decoration:underline; font-weight:bold;"+
"font-size:.8em; color:#06C\" href=\"${item.url}\" />${item.url}</a>"+
"{/if}" +
"<br/><span style=\"font-size:.8em;color:#AAA\">${item.summary}...</span>"+
"<br/><br/>" +
"{/for}",
getKonsultoUrl: function(term){
return this.KONSULTO_URL + term;
},
getNodeValue: function(node, tagName){
if (node.getElementsByTagName(tagName).length <= 0
|| node.getElementsByTagName(tagName)[0].length <= 0)
return null;
return node.getElementsByTagName(tagName)[0].childNodes[0].nodeValue;
},
apiSearchService: function(term, localitySlug, callback){
var searchParams = {
q: term,
authSign: this.AUTHSIGN,
appToken: this.APPTOKEN,
count: this.RESULT_LIMIT
}
if (localitySlug != null && localitySlug != "")
searchParams.l = localitySlug;
jQuery.ajax({
type: "GET",
url: this.API_URL,
data: searchParams,
dataType: "xml",
error: function() {
pblock.innerHTML("algo no ha ido demasiado bien :(");
},
success: callback
});
},
searchLocality: function(locality, callback){
var searchParams = {
authSign: this.AUTHSIGN,
appToken: this.APPTOKEN,
sl: locality
}
jQuery.ajax({
type: "GET",
url: this.API_URL,
data: searchParams,
dataType: "xml",
error: function() {
displayMessage("algo no ha ido demasiado bien :(");
},
success: callback
});
}
};
var noun_type_ooslocality = {
_name: "localidad",
suggestions: null,
oldsearch: null,
callback: function(data, textStatus){
var suggestions = new Array();
var entries = data.getElementsByTagName('entry');
if (entries.length > 1)
CmdUtils.log("encontrados " + entries.length +" resultados!");
else
CmdUtils.log("no hay resultados :(");
for (var i = 0; i < entries.length; i++){
suggestions.push(
CmdUtils.makeSugg(
oosAPI.getNodeValue(entries[i], "summary"),
null,
oosAPI.getNodeValue(entries[i], "id")
));
}
noun_type_ooslocality.suggestions = suggestions.splice(0,5);
},
suggest: function(text, html){
//CmdUtils.log(text + " / " + noun_type_ooslocality.oldsearch);
if (noun_type_ooslocality.oldsearch == null || noun_type_ooslocality.oldsearch.indexOf(text) == -1)
noun_type_ooslocality.suggestions = null;
if (noun_type_ooslocality.suggestions == null){
noun_type_ooslocality.oldsearch = text;
CmdUtils.log("buscar " + text);
oosAPI.searchLocality(text, noun_type_ooslocality.callback);
return [];
}
return noun_type_ooslocality.suggestions;
}
}
CmdUtils.CreateCommand({
name: "11870",
icon: "http://11870.com/favicon.ico",
homepage: "http://arctarus.es",
author: { name: "Gabriel Ortuño", email: "arctarusnet@gmail.com"},
license: "GPLV3",
description: "Busca en 11870.com",
help: "busca sitios o servicios en 11870.com",
takes: {"search term": noun_arb_text},
modifiers: {"en": noun_type_ooslocality},
preview: function(pblock, search_term, mods) {
oosAPI.apiSearchService(search_term.text, mods["en"].data, function(data, textStatus){
var sites = new Array();
var entries = data.getElementsByTagName('entry');
for (var i = 0; i < entries.length; i++){
sites.push({
site_name: oosAPI.getNodeValue(entries[i], "title"),
oosUrl: oosAPI.getNodeValue(entries[i], "id"),
summary: oosAPI.getNodeValue(entries[i], "summary"),
adr: oosAPI.getNodeValue(entries[i], "oos:useraddress"),
phone: oosAPI.getNodeValue(entries[i], "oos:telephone"),
locality: oosAPI.getNodeValue(entries[i], "oos:locality"),
url: oosAPI.getNodeValue(entries[i],"oos:url")
});
};
pblock.innerHTML = CmdUtils.renderTemplate(oosAPI.resultsTmpl, {items: sites});
});
},
execute: function(search_term, mods) {
if (mods["en"].text != undefined && mods["en"].text != null && mods["en"].text != "")
search_term.text += " en " + mods["en"].text;
Utils.openUrlInBrowser(oosAPI.getKonsultoUrl(search_term.text));
}
})