AS3: Flickr Loader


/ Published in: ActionScript 3
Save to your folder(s)

You can access Flickr using AS3 the same way as Javascript with JSON. You just have to massage the data a bit. Here's how you would use the class. In your Main.as, create an instance of the FlickrLoader. You will need the JSON.decoder class from http://code.google.com/p/as3corelib/


Copy this code and paste it in your HTML
  1. /************************
  2. Use this in your Flash file
  3. ************************/
  4.  
  5. var photoSetLoader :FlickrLoader = new FlickrLoader();
  6. photoSetLoader.load( "flickrPhotoSetID" );
  7. photoSetLoader.addEventListener( Event.COMPLETE, photoSetCompleteHandler, false, 0, true );
  8. function photoSetCompleteHandler( e:Event ):void
  9. {
  10. var flickrLoader:FlickrLoader = FlickrLoader( e.currentTarget );
  11. flickrLoader.removeEventListener( Event.COMPLETE, photoSetCompleteHandler );
  12. var totalImages:Number = flickrLoader.getImageData().length;
  13. var currentImage:uint = Math.floor( Math.random() * totalImages );
  14. trace( flickrLoader.getImageData() );
  15. }
  16.  
  17. /************************
  18. Place this is a file called FlickrLoader.as
  19. ************************/
  20. package
  21. {
  22. import com.adobe.serialization.json.JSON;
  23.  
  24. import flash.events.Event;
  25. import flash.events.EventDispatcher;
  26. import flash.events.HTTPStatusEvent;
  27. import flash.events.IEventDispatcher;
  28. import flash.events.IOErrorEvent;
  29. import flash.events.ProgressEvent;
  30. import flash.events.SecurityErrorEvent;
  31. import flash.net.URLLoader;
  32. import flash.net.URLRequest;
  33. import flash.net.URLRequestMethod;
  34. import flash.net.URLVariables;
  35.  
  36. public class FlickrLoader extends EventDispatcher
  37. {
  38. private var _data:Array;
  39.  
  40. private static const FLICKR_API:String = "FLICKR_API_KEY";
  41.  
  42. public function FlickrLoader(target:IEventDispatcher=null)
  43. {
  44. super(target);
  45. }
  46.  
  47. public function load( photoSetID:String ):void
  48. {
  49.  
  50. var urlVars:URLVariables = new URLVariables();
  51. urlVars.method = "flickr.photosets.getPhotos";
  52. urlVars.api_key = FLICKR_API;
  53. urlVars.photoset_id = photoSetID;
  54. urlVars.format = "json";
  55.  
  56. var urlRequest:URLRequest = new URLRequest();
  57. urlRequest.url = "http://api.flickr.com/services/rest/";
  58. urlRequest.method = URLRequestMethod.GET;
  59. urlRequest.data = urlVars;
  60.  
  61. var urlLoader:URLLoader = new URLLoader();
  62. urlLoader.addEventListener( Event.COMPLETE, onJSONLoaded );
  63. urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true);
  64. urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true);
  65. urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
  66. urlLoader.addEventListener(ProgressEvent.PROGRESS, progressHandler, false, 0, true);
  67. urlLoader.load( urlRequest );
  68. }
  69.  
  70. private function onJSONLoaded( e:Event ):void
  71. {
  72. _data = new Array();
  73.  
  74. var rawData = e.currentTarget.data as String;
  75. var data:String = rawData.slice( rawData.indexOf("{"), rawData.lastIndexOf("}") + 1 );
  76. // trace( data );
  77. var json:Object = JSON.decode( data ) as Object;
  78. var photos:Object = json.photoset.photo as Object;
  79.  
  80. //Create new image objects
  81. for( var i:uint; i < photos.length; i++ ){
  82. _data.push({
  83. "title" : photos[i].title.toString(),
  84. "url" : "http://farm" +
  85. photos[i].farm +
  86. ".static.flickr.com/" +
  87. photos[i].server + "/" +
  88. photos[i].id + "_" +
  89. photos[i].secret + ".jpg"
  90. });
  91. }
  92.  
  93. //Fire once the XML has been converted into Image Objects
  94. dispatchEvent( new Event( Event.COMPLETE ) );
  95. }
  96.  
  97. public function getImageData():Array
  98. {
  99. return _data;
  100. }
  101.  
  102. private function httpStatusHandler ( e:HTTPStatusEvent ):void
  103. {
  104. //trace("httpStatusHandler:" + e);
  105. }
  106.  
  107. private function securityErrorHandler ( e:SecurityErrorEvent ):void
  108. {
  109. trace("securityErrorHandler:" + e);
  110. }
  111.  
  112. private function ioErrorHandler( e:IOErrorEvent ):void
  113. {
  114. trace("ioErrorHandler: " + e);
  115. }
  116.  
  117. private function progressHandler( e:ProgressEvent ):void
  118. {
  119. //trace(e.currentTarget.bytesLoaded + " / " + e.currentTarget.bytesTotal);
  120. }
  121.  
  122. }
  123. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.