Created
December 3, 2008 08:05
-
-
Save desktable/31480 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
douban_search_num_html = "<div><b>${num}</b> douban items found matching <b>${term}</b>.</div><br/>"; | |
douban_search_item_html = "<div style=\"clear: both;\"><b><u><a href=\"${item.link[1][\"@href\"]}\">${item.title[\"$t\"]}</a></u></b><br/>" + | |
"<img src=\"${item.link[2][\"@href\"]}\" style=\"float: left; height: 80px; background-color: white;\" />" + | |
"{for aut in item.author}<small>${aut.name[\"$t\"]}.</small>{/for}" + | |
"{for db in item[\"db:attribute\"]}<small>${db[\"$t\"]}.</small>{/for}" + | |
"</div><br/>"; | |
douban_search_item_no_img_html = "<div style=\"clear: both;\"><b><u><a href=\"${item.link[1][\"@href\"]}\">${item.title[\"$t\"]}</a></u></b><br/>" + | |
"{for aut in item.author}<small>${aut.name[\"$t\"]}.</small>{/for}" + | |
"{for db in item[\"db:attribute\"]}<small>${db[\"$t\"]}.</small>{/for}" + | |
"</div><br/>"; | |
noun_type_section = new CmdUtils.NounType( "section", | |
[ "generic" , "music", "movie", "book" ] | |
); | |
douban_section_url_template = { | |
"generic" : "http://www.douban.com/subject_search?search_text=${term}", | |
"music" : "http://www.douban.com/subject_search?search_text=${term}&cat=1003", | |
"movie" : "http://www.douban.com/subject_search?search_text=${term}&cat=1002", | |
"book" : "http://www.douban.com/subject_search?search_text=${term}&cat=1001", | |
}; | |
CmdUtils.CreateCommand({ | |
name: "douban-search", | |
takes: {term: noun_arb_text}, | |
modifiers: { in: noun_type_section }, | |
icon: "http://www.douban.com/pics/dou24.gif", | |
homepage: "http://gist.github.com/31480", | |
author: { name: "CHEN Daizhuo", email: "chendaizhuo@gmail.com" }, | |
license: "GPL", | |
description: "douban-search: douban becomes ubiquity.", | |
help: "Invokes the douban search engine from your ubiquity command line.", | |
__getSection : function( rawData ) { | |
return rawData.in.text || "generic"; | |
}, | |
__getSearchTerm : function( rawData ) { | |
return rawData.text || ""; | |
}, | |
__getTargetURL : function( section, term ) { | |
if (term.length < 1) { | |
if (section == "generic") section=""; | |
return "http://www.douban.com/" + section; | |
} | |
var template = douban_section_url_template[section]; | |
var subs = {"term" : term }; | |
var res = CmdUtils.renderTemplate(template, subs); | |
return res; | |
}, | |
preview: function( pblock, expSearchTerm, modifiers ) { | |
var section = this.__getSection(modifiers); | |
var term = this.__getSearchTerm(expSearchTerm); | |
var msg = (term.length < 1) | |
? ("Go to the <b>" + section +"</b> home page of douban") | |
: ("Searches douban (" + section + ") for " + term); | |
pblock.innerHTML = msg; | |
if (section != "generic") { | |
var url = "http://api.douban.com/" + section + "/subjects"; | |
var params = { | |
q: term, | |
"start-index": 1, | |
"max-results": 3, | |
alt: "json", | |
apikey: "05eaefdd447763e211c0cf97cd662ba6", | |
}; | |
CmdUtils.previewGet( pblock, url, params, function(data) { | |
pblock.innerHTML = CmdUtils.renderTemplate( | |
douban_search_num_html, | |
{ term: term, num: data["opensearch:totalResults"]["$t"] } | |
); | |
for (var i=0;i<data.entry.length;i++) { | |
if (data.entry[i].link.length>2) { | |
pblock.innerHTML = pblock.innerHTML + CmdUtils.renderTemplate( | |
douban_search_item_html, | |
{item: data.entry[i] } | |
); } | |
else { | |
pblock.innerHTML = pblock.innerHTML + CmdUtils.renderTemplate( | |
douban_search_item_no_img_html, | |
{item: data.entry[i] } | |
); } } | |
}, "json"); | |
} | |
}, | |
execute: function( expSearchTerm, modifiers ) { | |
var section = this.__getSection(modifiers); | |
var term = this.__getSearchTerm(expSearchTerm); | |
var url = this.__getTargetURL(section, term); | |
Utils.openUrlInBrowser(url); | |
} | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment