Return to Snippet

Revision: 15066
at January 15, 2010 11:24 by chrisaiv


Updated Code
/************************
Use this in your Flash file
************************/

var photoSetLoader :FlickrLoader = new FlickrLoader();
photoSetLoader.load( "flickrPhotoSetID" );
photoSetLoader.addEventListener( Event.COMPLETE, photoSetCompleteHandler, false, 0, true );
function photoSetCompleteHandler( e:Event ):void
{
	var flickrLoader:FlickrLoader = FlickrLoader( e.currentTarget );
		flickrLoader.removeEventListener( Event.COMPLETE, photoSetCompleteHandler );
	var totalImages:Number = flickrLoader.getImageData().length;
	var currentImage:uint = Math.floor( Math.random() * totalImages );
	trace( flickrLoader.getImageData() );
}

/************************
Place this is a file called FlickrLoader.as
************************/
package 
{
	import com.adobe.serialization.json.JSON;

	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;

	public class FlickrLoader extends EventDispatcher
	{
        private var _data:Array;

        private static const FLICKR_API:String = "FLICKR_API_KEY";

		public function FlickrLoader(target:IEventDispatcher=null)
		{
			super(target);
		}

        public function load( photoSetID:String ):void
        {

        	var urlVars:URLVariables = new URLVariables();
        		urlVars.method = "flickr.photosets.getPhotos";
        		urlVars.api_key = FLICKR_API;
        		urlVars.photoset_id = photoSetID;
        		urlVars.format = "json";

        	var urlRequest:URLRequest = new URLRequest();
        		urlRequest.url = "http://api.flickr.com/services/rest/";
        		urlRequest.method = URLRequestMethod.GET;
        		urlRequest.data = urlVars;

            var urlLoader:URLLoader = new URLLoader();
                urlLoader.addEventListener( Event.COMPLETE, onJSONLoaded );
                urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true);
                urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true);
                urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
                urlLoader.addEventListener(ProgressEvent.PROGRESS, progressHandler, false, 0, true);
                urlLoader.load( urlRequest );
        }

		private function onJSONLoaded( e:Event ):void
		{
	        _data = new Array();

	        var rawData = e.currentTarget.data as String;
			var data:String = rawData.slice( rawData.indexOf("{"), rawData.lastIndexOf("}") + 1 );
//			trace( data );
	        var json:Object = JSON.decode( data ) as Object;
	        var photos:Object = json.photoset.photo as Object;

        	//Create new image objects
	        for( var i:uint; i < photos.length; i++ ){
				_data.push({ 
					   "title" : photos[i].title.toString(), 
						"url" : "http://farm" + 
								photos[i].farm + 
								".static.flickr.com/" + 
								photos[i].server + "/" + 
								photos[i].id + "_" + 
								photos[i].secret + ".jpg" 
				   });
	        }

	        //Fire once the XML has been converted into Image Objects
	        dispatchEvent( new Event( Event.COMPLETE ) ); 
		}

		public function getImageData():Array
		{
		        return _data;
		}

		private function httpStatusHandler ( e:HTTPStatusEvent ):void
		{
		        //trace("httpStatusHandler:" + e);
		}

		private function securityErrorHandler ( e:SecurityErrorEvent ):void
		{
		        trace("securityErrorHandler:" + e);
		}

		private function ioErrorHandler( e:IOErrorEvent ):void
		{
		        trace("ioErrorHandler: " + e);
		}

		private function progressHandler( e:ProgressEvent ):void
		{
		        //trace(e.currentTarget.bytesLoaded + " / " + e.currentTarget.bytesTotal);
		}

	}
}

Revision: 15065
at January 15, 2010 11:24 by chrisaiv


Updated Code
package 
{
	import com.adobe.serialization.json.JSON;

	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;

	public class FlickrLoader extends EventDispatcher
	{
        private var _data:Array;

        private static const FLICKR_API:String = "FLICKR_API_KEY";

		public function FlickrLoader(target:IEventDispatcher=null)
		{
			super(target);
		}

        public function load( photoSetID:String ):void
        {

        	var urlVars:URLVariables = new URLVariables();
        		urlVars.method = "flickr.photosets.getPhotos";
        		urlVars.api_key = FLICKR_API;
        		urlVars.photoset_id = photoSetID;
        		urlVars.format = "json";

        	var urlRequest:URLRequest = new URLRequest();
        		urlRequest.url = "http://api.flickr.com/services/rest/";
        		urlRequest.method = URLRequestMethod.GET;
        		urlRequest.data = urlVars;

            var urlLoader:URLLoader = new URLLoader();
                urlLoader.addEventListener( Event.COMPLETE, onJSONLoaded );
                urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true);
                urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true);
                urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
                urlLoader.addEventListener(ProgressEvent.PROGRESS, progressHandler, false, 0, true);
                urlLoader.load( urlRequest );
        }

		private function onJSONLoaded( e:Event ):void
		{
	        _data = new Array();

	        var rawData = e.currentTarget.data as String;
			var data:String = rawData.slice( rawData.indexOf("{"), rawData.lastIndexOf("}") + 1 );
//			trace( data );
	        var json:Object = JSON.decode( data ) as Object;
	        var photos:Object = json.photoset.photo as Object;

        	//Create new image objects
	        for( var i:uint; i < photos.length; i++ ){
				_data.push({ 
					   "title" : photos[i].title.toString(), 
						"url" : "http://farm" + 
								photos[i].farm + 
								".static.flickr.com/" + 
								photos[i].server + "/" + 
								photos[i].id + "_" + 
								photos[i].secret + ".jpg" 
				   });
	        }

	        //Fire once the XML has been converted into Image Objects
	        dispatchEvent( new Event( Event.COMPLETE ) ); 
		}

		public function getImageData():Array
		{
		        return _data;
		}

		private function httpStatusHandler ( e:HTTPStatusEvent ):void
		{
		        //trace("httpStatusHandler:" + e);
		}

		private function securityErrorHandler ( e:SecurityErrorEvent ):void
		{
		        trace("securityErrorHandler:" + e);
		}

		private function ioErrorHandler( e:IOErrorEvent ):void
		{
		        trace("ioErrorHandler: " + e);
		}

		private function progressHandler( e:ProgressEvent ):void
		{
		        //trace(e.currentTarget.bytesLoaded + " / " + e.currentTarget.bytesTotal);
		}

	}
}

Revision: 15064
at June 22, 2009 23:22 by chrisaiv


Updated Code
/************************
Use this in your Flash file
************************/

var photoSetLoader :FlickrLoader = new FlickrLoader();
photoSetLoader.load( "flickrPhotoSetID" );
photoSetLoader.addEventListener( Event.COMPLETE, photoSetCompleteHandler, false, 0, true );
function photoSetCompleteHandler( e:Event ):void
{
	var flickrLoader:FlickrLoader = FlickrLoader( e.currentTarget );
		flickrLoader.removeEventListener( Event.COMPLETE, photoSetCompleteHandler );
	var totalImages:Number = flickrLoader.getImageData().length;
	var currentImage:uint = Math.floor( Math.random() * totalImages );
	trace( flickrLoader.getImageData() );
}

/************************
Place this is a file called FlickrLoader.as
************************/
package 
{
	import com.adobe.serialization.json.JSON;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;

	public class FlickrLoader extends EventDispatcher
	{
        private var _data:Array;
        
        private static const FLICKR_API:String = "FLICKR_API_KEY";

		public function FlickrLoader(target:IEventDispatcher=null)
		{
			super(target);
		}

        public function load( photoSetID:String ):void
        {
        	
        	var urlVars:URLVariables = new URLVariables();
        		urlVars.method = "flickr.photosets.getPhotos";
        		urlVars.api_key = FLICKR_API;
        		urlVars.photoset_id = photoSetID;
        		urlVars.format = "json";
        	
        	var urlRequest:URLRequest = new URLRequest();
        		urlRequest.url = "http://api.flickr.com/services/rest/";
        		urlRequest.method = URLRequestMethod.GET;
        		urlRequest.data = urlVars;

            var urlLoader:URLLoader = new URLLoader();
                urlLoader.addEventListener( Event.COMPLETE, onJSONLoaded );
                urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true);
                urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true);
                urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
                urlLoader.addEventListener(ProgressEvent.PROGRESS, progressHandler, false, 0, true);
                urlLoader.load( urlRequest );
        }
                    
		private function onJSONLoaded( e:Event ):void
		{
	        _data = new Array();
	        
	        var rawData = e.currentTarget.data as String;
			var data:String = rawData.slice( rawData.indexOf("{"), rawData.lastIndexOf("}") + 1 );
//			trace( data );
	        var json:Object = JSON.decode( data ) as Object;
	        var photos:Object = json.photoset.photo as Object;

        	//Create new image objects
	        for( var i:uint; i < photos.length; i++ ){
	        	_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" ) 
        			); 
	        }

	        //Fire once the XML has been converted into Image Objects
	        dispatchEvent( new Event( Event.COMPLETE ) ); 
		}
		
		public function getImageData():Array
		{
		        return _data;
		}
		
		private function httpStatusHandler ( e:HTTPStatusEvent ):void
		{
		        //trace("httpStatusHandler:" + e);
		}
		
		private function securityErrorHandler ( e:SecurityErrorEvent ):void
		{
		        trace("securityErrorHandler:" + e);
		}
		
		private function ioErrorHandler( e:IOErrorEvent ):void
		{
		        trace("ioErrorHandler: " + e);
		}
		
		private function progressHandler( e:ProgressEvent ):void
		{
		        //trace(e.currentTarget.bytesLoaded + " / " + e.currentTarget.bytesTotal);
		}
		
	}
}

Revision: 15063
at June 22, 2009 23:20 by chrisaiv


Updated Code
/************************
Use this in your Flash file
************************/

var photoSetLoader :FlickrLoader = new FlickrLoader();
photoSetLoader.load( "flickrPhotoSetID" );
photoSetLoader.addEventListener( Event.COMPLETE, photoSetCompleteHandler, false, 0, true );
function photoSetCompleteHandler( e:Event ):void
{
	var flickrLoader:FlickrLoader = FlickrLoader( e.currentTarget );
		flickrLoader.removeEventListener( Event.COMPLETE, photoSetCompleteHandler );
	var totalImages:Number = flickrLoader.getImageData().length;
	var currentImage:uint = Math.floor( Math.random() * totalImages );
	trace( flickrLoader.getImageData() );
}

/************************
Place this is a file called FlickrLoader.as
************************/
package 
{
	import com.adobe.serialization.json.JSON;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;

	public class FlickrLoader extends EventDispatcher
	{
        private var _data:Array;
        private var responseFunction:Function;
        
        private static const FLICKR_API:String = "";

		public function FlickrLoader(target:IEventDispatcher=null)
		{
			super(target);
		}

        public function load( photoSetID:String ):void
        {
        	
        	var urlVars:URLVariables = new URLVariables();
        		urlVars.method = "flickr.photosets.getPhotos";
        		urlVars.api_key = FLICKR_API;
        		urlVars.photoset_id = photoSetID;
        		urlVars.format = "json";
        	
        	var urlRequest:URLRequest = new URLRequest();
        		urlRequest.url = "http://api.flickr.com/services/rest/";
        		urlRequest.method = URLRequestMethod.GET;
        		urlRequest.data = urlVars;

            var urlLoader:URLLoader = new URLLoader();
                urlLoader.addEventListener( Event.COMPLETE, onJSONLoaded );
                urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true);
                urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true);
                urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
                urlLoader.addEventListener(ProgressEvent.PROGRESS, progressHandler, false, 0, true);
                urlLoader.load( urlRequest );
        }
                    
		private function onJSONLoaded( e:Event ):void
		{
	        _data = new Array();
	        
	        var rawData = e.currentTarget.data as String;
			var data:String = rawData.slice( rawData.indexOf("{"), rawData.lastIndexOf("}") + 1 );
//			trace( data );
	        var json:Object = JSON.decode( data ) as Object;
	        var photos:Object = json.photoset.photo as Object;

        	//Create new image objects
	        for( var i:uint; i < photos.length; i++ ){
	        	_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" ) 
        			); 
	        }

	        //Fire once the XML has been converted into Image Objects
	        dispatchEvent( new Event( Event.COMPLETE ) ); 
		}
		
		public function getImageData():Array
		{
		        return _data;
		}
		
		private function httpStatusHandler ( e:HTTPStatusEvent ):void
		{
		        //trace("httpStatusHandler:" + e);
		}
		
		private function securityErrorHandler ( e:SecurityErrorEvent ):void
		{
		        trace("securityErrorHandler:" + e);
		}
		
		private function ioErrorHandler( e:IOErrorEvent ):void
		{
		        trace("ioErrorHandler: " + e);
		}
		
		private function progressHandler( e:ProgressEvent ):void
		{
		        //trace(e.currentTarget.bytesLoaded + " / " + e.currentTarget.bytesTotal);
		}
		
	}
}

Revision: 15062
at June 22, 2009 23:16 by chrisaiv


Initial Code
package src
{
	import com.adobe.serialization.json.JSON;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;

	public class FlickrLoader extends EventDispatcher
	{
        private var _data:Array;
        private var responseFunction:Function;
        
        private static const FLICKR_API:String = "";

		public function FlickrLoader(target:IEventDispatcher=null)
		{
			super(target);
		}

        public function load( photoSetID:String ):void
        {
        	
        	var urlVars:URLVariables = new URLVariables();
        		urlVars.method = "flickr.photosets.getPhotos";
        		urlVars.api_key = FLICKR_API;
        		urlVars.photoset_id = photoSetID;
        		urlVars.format = "json";
        	
        	var urlRequest:URLRequest = new URLRequest();
        		urlRequest.url = "http://api.flickr.com/services/rest/";
        		urlRequest.method = URLRequestMethod.GET;
        		urlRequest.data = urlVars;

            var urlLoader:URLLoader = new URLLoader();
                urlLoader.addEventListener( Event.COMPLETE, onJSONLoaded );
                urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true);
                urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true);
                urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
                urlLoader.addEventListener(ProgressEvent.PROGRESS, progressHandler, false, 0, true);
                urlLoader.load( urlRequest );
        }
                    
		private function onJSONLoaded( e:Event ):void
		{
	        _data = new Array();
	        
	        var rawData = e.currentTarget.data as String;
			var data:String = rawData.slice( rawData.indexOf("{"), rawData.lastIndexOf("}") + 1 );
//			trace( data );
	        var json:Object = JSON.decode( data ) as Object;
	        var photos:Object = json.photoset.photo as Object;

        	//Create new image objects
	        for( var i:uint; i < photos.length; i++ ){
	        	_data.push( 
        			new Image(	photos[i].title.toString(), "", 
        						"http://farm" + 
								photos[i].farm + ".static.flickr.com/" + 
								photos[i].server + "/" + 
								photos[i].id + "_" + 
								photos[i].secret + ".jpg" ) 
        			); 
	        }

	        //Fire once the XML has been converted into Image Objects
	        dispatchEvent( new Event( Event.COMPLETE ) ); 
		}
		
		public function getImageData():Array
		{
		        return _data;
		}
		
		private function httpStatusHandler ( e:HTTPStatusEvent ):void
		{
		        //trace("httpStatusHandler:" + e);
		}
		
		private function securityErrorHandler ( e:SecurityErrorEvent ):void
		{
		        trace("securityErrorHandler:" + e);
		}
		
		private function ioErrorHandler( e:IOErrorEvent ):void
		{
		        trace("ioErrorHandler: " + e);
		}
		
		private function progressHandler( e:ProgressEvent ):void
		{
		        //trace(e.currentTarget.bytesLoaded + " / " + e.currentTarget.bytesTotal);
		}
		
	}
}

Initial URL

                                

Initial Description
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/

Initial Title
AS3: Flickr Loader

Initial Tags

                                

Initial Language
ActionScript 3