Posted By

chrisaiv on 06/22/09


Tagged

flickr as3


Versions (?)

Who likes this?

10 people have marked this snippet as a favorite

akashaman
johnwun
Pingpongbal
jinx_dee
TrentSkunk
mrgrob
palo
tspitzr
dubogii
ballen205


AS3: Flickr Loader


 / Published in: ActionScript 3
 

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/

  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
Posted By: andrewdobson on November 30, 2009

Getting the following error on Compile:

FlickrLoader.as(63): col: 64 Error: Incorrect number of arguments. Expected no more than 0.

data.push( new Object (photos[i].title.toString(), "http://farm" + photos[i].farm + ".static.flickr.com/" + photos[i].server + "/" + photos[i].id + "" + photos[i].secret + ".jpg"));

Posted By: chrisaiv on December 2, 2009

Hey Andrew,

Well from what you've provided, one thing I notice right off the bat is that you might be missing an underscore(_) between photos[i].id and photos[i].secret.

Posted By: chrisaiv on December 2, 2009

You're also missing the underscore( _ ) on the _data array. Maybe when you're submitting the error message through this comments box, it strips the underscores out.

Posted By: jinx_dee on January 14, 2010

This looks like broken script..Its gives me the same error as Andrew...

1137: Incorrect number of arguments. Expected no more than 0. at

"http://farm" +

[I dont understand If you pass two value to an object how can you call it]

Can someone help me?? Or can someone post the fla thats working J

Posted By: chrisaiv on January 15, 2010

Hey jinx_dee,

I changed the code a bit to fix your issue. Fingers Crossed.

Posted By: kingrowla on October 17, 2010

I see you can pull the title of the image out of the array, is there any way to pull the description of the image as well?

You need to login to post a comment.