kelvinluck.com

a stroke of luck

FlashrSimpleSearch

Update 3:

This is a very old page imported from my previous blog. If there is missing content below or anything that doesn’t make sense then please check the page on my old blog.

Update 2:

For the latest up to date information about Flashr please check out the new Flashr microsite

Update:

This example will no longer work as-is – you will need to update your Flashr files as described here.

Somebody on the Flashr mailing list was having difficulties getting to grips with Flashr. Since a picture is worth a thousand words and I needed to do some examples of using Flashr 0.5 anyway, I put this very simple example together…

Basically all it does is connects to the Flickr API and does a search for a tag using flickr.photos.search. It then calls flickr.photos.getInfo on each of the returned photos to get more detailed information (like authors name, description and tags used). Once it has got all this information it simply traces it to the output window in Flash.

All of this is made much easier by a feature in the as yet not officially released Flashr 0.5. Now when you make multiple requests to the Flickr API the requests are added to a queue and each one executes in turn. This makes writing applications that rely on multiple API requests (such as this one) much simpler.

DOWNLOAD
You can download all the files for this example from here. This includes the latest version of Flashr 0.5 (see the trac timeline for recent changes). The specific file you are interested in is com.kelvinluck.flashr.example.FlashrSimpleSearch.as.

THE CODE
Here is the entire class which powers this little “application”.

import com.kelvinluck.util.LogWrapper;
import com.kelvinluck.flashr.core.FlashrResponse;
import com.dynamicflash.utils.Delegate;
import com.kelvinluck.flashr.core.ResultsSet;
import com.kelvinluck.flashr.core.Photo;
import com.kelvinluck.flashr.core.Flashr;
/**
* Class: FlashrSimpleSearch
*
* Very simple example showing how to search for a tag using Flashr 0.5 and then
* get more detailed information about each matching photo.
*
* Currently just traces the relvant information out.
*
* Author:
* Kelvin Luck
*/

class com.kelvinluck.flashr.example.FlashrSimpleSearch extends MovieClip
{
   
   public static var SEARCH_TAG:String = "snow";
   public static var NUM_RESULTS:Number = 10;
   
   private var _flashr:Flashr;
   private var _flashrResponse:FlashrResponse;
   
   private var _numResults:Number;
   private var _photos:Array;
   
   /**
   * Function: FlashrSimpleSearch
   * Constructor
   **/

   function FlashrSimpleSearch()
   {
      // uncomment the following lines if you want to see the internals of what is happening with Flashr.
      //LogWrapper.getInstance().init();
      //LogWrapper.getInstance().addTracePublisher();
     
      _photos = [];
     
      _flashr = Flashr.getFlashr();
      _flashr.apiKey = "b40e05adf210ad4c4cc4da00f99f4184";
      _flashr.cacheQueries = true;
     
     
      _flashrResponse = new FlashrResponse();
      _flashrResponse.onPhotosSearch = Delegate.create(this, onPhotosSearch);
      _flashrResponse.onPhotosGetInfo = Delegate.create(this, onPhotosGetInfo);
     
      trace("Searching for " + NUM_RESULTS + " photos matching tag '" + SEARCH_TAG + "'");
      _flashr.photosSearch({tags:SEARCH_TAG, per_page:NUM_RESULTS});
   }
   
   function onPhotosSearch(rs:ResultsSet)
   {
      _numResults = rs.photos.length;
      trace("Loaded data for " + _numResults + " photos out of " + rs.total + " matching tag, requesting more details about each photo");
      for (var i:Number=0; i<_numresults ; i++) {
         var thisPhoto:Photo = rs.photos[i];
         _flashr.photosGetInfo(thisPhoto.id, thisPhoto.secret);
      }
   }
   function onPhotosGetInfo(photo:Photo)
   {
      trace("Loaded detailed data for photo '" + photo.id + "'");
      _photos.push(photo);
      if (_photos.length == _numResults) {
         onAllPhotosLoaded();
      }
   }
   function onAllPhotosLoaded()
   {
      trace("*************ALL PHOTO INFO LOADED*************");
      trace(" ");
      for (var i:Number=0; i<_photos.length; i++) {
         var thisPhoto:Photo = _photos[i];
         trace("Photo " + thisPhoto.id + " :");
         trace("  Title: " + thisPhoto.title);
         trace("  Description: " + thisPhoto.description);
         trace("  Taken: " + thisPhoto.dateTaken);
         trace("  Author: " + thisPhoto.owner.username);
         trace("  Author ID: " + thisPhoto.owner.nsid);
         trace("  Thumbnail: " + thisPhoto.thumbnailUrl);
         trace("  Photo page URL: " + thisPhoto.photoPageUrl);
         trace("  Tags: " + thisPhoto.getTagsAsStrings());
         trace("------------------");
         trace(" ");
      }
   }
   
   /**
   * Function: toString
   **/

   public function toString():String
   {
      return "[com.kelvinluck.flashr.example.FlashrSimpleSearch]";
   }
}

I hope this is useful to someone, please leave any feedback as a comment below or on the Flashr mailing list;

9 Comments, Comment or Ping

Reply to “FlashrSimpleSearch”