Why an API?
Loading HTML and XML documents is convenient for pulling information from the web, however, for more sophisticated applications, many sites offer an API. An API (Application Programming Interface) is an interface through which one application can access the services of another. There are many APIs that can be used with Processing and you might look through the Data / Protocols section of the Processing libraries web page for some ideas.Here, we will look at an example that performs a web search, using the Yahoo API. Although you can access the Yahoo API directly in Processing, I have created a library that makes it a bit simpler (as well as allowing the search to be performed asynchronously and not cause the sketch to pause). You will need to download both my Processing library, as well as the Yahoo API files (these are known as an SDK: “Software Development Kit” ).
- Download the library here
- Go and get a developer ID
- Download the Yahoo! Search SDK Find the file: yahoo_search-2.X.X.jar and put it in the library folder (along with the above download).
Example 18-10: Yahoo Search
import pyahoo.*;
// Create a YahooSearch object. You have to pass in the API key given to you by Yahoo.
YahooSearch yahoo;
void setup() {
size(400,400);
// Make a search object, pass in your key
yahoo = new YahooSearch(this, "YOUR API KEY HERE");
}
void mousePressed() {
// Search for a String. By default you will get back 10 results.
// If you want more (or less), you can request a specific number by saying:
// yahoo.search("processing.org", 30);
yahoo.search("processing.org");
}
void draw() {
noLoop();
}
// When the search is complete
void searchEvent(YahooSearch yahoo) {
// Get Titles and URLs
String[] titles = yahoo.getTitles();
// Search results arrive as an array of Strings.
// You can also get the summaries with getSummaries().
String[] urls = yahoo.getUrls();
for (int i = 0; i < titles.length; i++) {
println( "__________" );
println(titles[i]);
println(urls[i]);
}
}
The library can be used to perform a simple visualization. Example 18-11 searches for five names and draws a circle for each one (with a size tied to the total number of results available).








