arctarus (owner)

Revisions

gist: 34649 Download_button fork
public
Description:
ubiquity's command for 11870.com
Public Clone URL: git://gist.github.com/34649.git
Embed All Files: show embed
x #
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
146
147
148
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));
     }
})