A quick introduction to the BOSS REST API and how to use it in JavaScript using YUI3. Make sure to also download the code examples:
http://icant.co.uk/stuff/bosscodebr.zip
23. The REST API:
boss.yahooapis.com/ysearch/{type}/v1/{search}
search is the term to look for
(url-encoded)
Put “” around terms to ensure the right order, f.e. “donkey
fur” (you don’t want to see cats, do you?)
Filter with a -, f.e. donkey -shrek
Restrict to a site using site:, f.e. donkey site:flickr.com
24. The REST API:
boss.yahooapis.com/ysearch/{type}/v1/{search}
Other parameters:
appid: your app ID (needed)
count: amount of results
start: where to start the counting
region / lang: country and language
format: xml or json
sites: restrict to certain sites (comma separated)
25. Web search REST API:
boss.yahooapis.com/ysearch/web/v1/{search}
Extra parameters:
filter: To filter out nasties, use filter=-porn-hate
type: to search different types. You can use html, text, pdf, xl,
msword, ppt or groups like msoffice and nonhtml. You can
also do a type=msoffice,-xl
26. Image search REST API:
boss.yahooapis.com/ysearch/images/v1/{search}
Extra parameters:
filter: no nudies
dimensions: all, small, medium, large, wallpaper,
widewallpaper
refererurl: all images in that url
url: image at that url
27. News search REST API:
boss.yahooapis.com/ysearch/news/v1/{search}
Extra parameters:
age: how old the news are in days. Last five days would be
“5d”
28. There are restrictions how to
display results and
information as to what data
comes back.
29. For this, read the guide!
http://developer.yahoo.com/search/boss/boss_guide/
47. <p>Warning: this is terrible code, USE A LIBRARY INSTEAD!</p>
<ul id=quot;searchesquot;>
<li><a href=quot;http://search.yahoo.com/search?va=donkeysquot;>
Search for Donkeys
</a>
</li>
<li><a href=quot;http://search.yahoo.com/search?va=kittensquot;>
Search for kittens
</a>
</li>
</ul>
<div id=quot;searchresultsquot;></div>
48. <script type=quot;text/javascriptquot; charset=quot;utf-8quot;>
function founddonkeys(o){
var donkeys = o.ysearchresponse.resultset_web;
var results = document.createElement('ul');
for(var i=0,j=donkeys.length;i<j;i++){
var item = document.createElement('li');
var link = document.createElement('a');
var abstract = document.createElement('p');
link.setAttribute('href',donkeys[i].clickurl);
link.innerHTML = donkeys[i].title;
item.appendChild(link);
abstract.innerHTML = donkeys[i].abstract;
item.appendChild(abstract);
results.appendChild(item);
}
var resultsbox = document.getElementById('searchresults');
resultsbox.innerHTML = '';
resultsbox.appendChild(results);
}
var APIkey = 'TX6b4XHV34EnPXW0sYEr51hP1pn5O8KAGs'+
'.LQSXer1Z7RmmVrZouz5SvyXkWsVk-';
var searchlinks =
document.getElementById('searches').getElementsByTagName('a');
for(var i=0;searchlinks[i];i++){
49. var APIkey = 'TX6b4XHV34EnPXW0sYEr51hP1pn5O8KAGs'+
'.LQSXer1Z7RmmVrZouz5SvyXkWsVk-';
var searchlinks = document.getElementById('searches').
getElementsByTagName('a');
for(var i=0;searchlinks[i];i++){
searchlinks[i].onclick = function(){
var searchterm = this.href.split('va=')[1];
var url = 'http://boss.yahooapis.com/ysearch/web/v1/' +
searchterm + '?format=json&' +
'callback=founddonkeys' + '&appid=' + APIkey
var s = document.createElement('script');
s.setAttribute('type','text/javascript');
s.setAttribute('src',url);
document.getElementsByTagName('head')[0].appendChild(s);
return false;
}
}
</script>