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” ).

Once you have the key, you are ready to go. Th e library works in a similar fashion to simpleML. You make a YahooSearch object and call the search() function. When the search is completed, it will arrive in an event callback: searchEvent(). There, you can ask for URLs, titles, or summaries (all available as String arrays).



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).



  • http://sites.google.com/site/lorentrogers/ Loren Rogers

    I’ve noticed that the developer ID link is not going to the right place..
    and when I get to the right spot (i think):
    https://developer.apps.yahoo.com/wsregapp/
    the form won’t go through.. perhaps its just a problem with my connection?

    Thanks
    – Loren

  • http://www.spikynorman.net fred gannett

    +VE: I have hacked a lot of C and Perl in my time and have wanted to do something more visual.
    This is a great system for learning visual programming, covers up ( for a while) the nonsense of java.
    Inspiring…

    -VE: Have you had a listen to the reCaptcha below ?
    Sounds like random recordings from a remote call centre.
    I know the sounds are supposed to be a bit mangled – but come on – at least make them understandable. I really don’t think anyone is going to hack that. Hows about slicing up an audio book ?

    Fred Gannett

  • http://www.dscomic.com Rob

    Glad I found this – I’ve been tinkering with the Yahoo API for mail and I’ve been curious to read some other sources on how to work with this API.

  • Erin Lewis

    Is the Yahoo Search SDK no longer offered? Cant seem to find it on the yahoo developer site.

  • http://www.learningprocessing.com Daniel Shiffman

    It may be deprecated, I’m going to rewrite a lot of these examples for a new edition of the book soon. For now, the SDK is part of pyahoo.zip download.

  • Erin Lewis

    that’s great to hear of a new edition of the book. when do you expect to be releasing it?

  • http://www.learningprocessing.com Daniel Shiffman

    My hope is next fall!

  • alex

    hi Daniel I have found your book so very helpful and am realy enjoying it.