Flickr API Search Engine
This is a simple demo of how to use Flickr API to make searchs.
In this example i just used tags as a parameter, but if you look here > http://www.flickr.com/services/api/flickr.photos.search.html you'll found a lot of parameters to play with, such as user_id to serach in a particular user gallery; with media param you can filter results by type of media (photos or videos), you can also search by group_id, user contact photos, and i found great the fact you can even search images by location. That last one rocks to do some application combining the Google Maps Api and the Flickr API.
Enjoy! :)
/**
* Copyright JorgeBraccini ( http://wonderfl.net/user/JorgeBraccini )
* GNU General Public License, v3 ( http://www.gnu.org/licenses/quick-guide-gplv3.html )
* Downloaded from: http://wonderfl.net/c/tElT
*/
// forked from Jay.Braccini's WonderflTraceLite
package {
import flash.geom.Point;
import flash.net.URLStream;
import flash.system.Security;
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.events.Event;
import flash.net.URLLoader;
import flash.events.MouseEvent;
import flash.display.Sprite;
import com.bit101.components.InputText;
import com.bit101.components.PushButton;
import com.bit101.components.ScrollPane;
import gs.TweenLite;
public class WonderflTraceLite extends Sprite {
private var _search_bt :PushButton;
private var _search_txt :InputText;
private var _loader :URLLoader;
private var _result_items :Array;
private var _result_container :ScrollPane;
public function WonderflTraceLite() {
inittrace(this);
init();
}
private function init():void {
_loader = new URLLoader();
_loader.addEventListener(Event.COMPLETE, handleOnFlickrResponse);
_search_txt = new InputText();
_search_txt.width = stage.stageWidth - 50;
_search_txt.height = 20;
addChild(_search_txt);
_search_bt = new PushButton();
_search_bt.addEventListener(MouseEvent.CLICK, userInvokesSearch);
_search_bt.label = "Search";
_search_bt.width = 49;
_search_bt.x = _search_txt.width + 1;
addChild(_search_bt);
_result_container = new ScrollPane();
_result_container.y = 21;
_result_container.width = stage.stageWidth;
_result_container.height = stage.stageHeight - 100 -21;
addChild(_result_container);
}
private function userInvokesSearch(e:MouseEvent):void {
var query:String = _search_txt.text as String;
_search_bt.enabled = false;
_search_txt.enabled = false;
search(query.split(" ").toString());
}
private function search(tags:String):void {
var api:String = "http://api.flickr.com/services/rest/";
var key:String = "c512bd33216771ddf747f529d945d4a3";
var method:String = "flickr.photos.search";
var resultsPerPage:Number = 100;
var url:String = api + "?method=" + method + "&api_key=" + key + "&tags=" + tags + "&per_page=" + resultsPerPage.toString() + "&tag_mode=all";
var flickrQueryUrl:URLRequest = new URLRequest(url);
_search_txt.text = "Searching for " + tags;
_loader.load(flickrQueryUrl);
trace("Searching for " + tags +"...");
}
private function handleOnFlickrResponse(e:Event):void {
var response:XML = XML(e.target.data);
_search_bt.enabled = true;
_search_txt.enabled = true;
_search_txt.text = "";
try { clear(); } catch (e:Error) {}
loadNewResponse(response);
}
private function loadNewResponse(response:XML):void {
trace("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
trace(response.toString());
var photos_xml:XMLList = response.photos.children();
var photo_xml:XML;
var photo:ImageLoader;
var url:URLRequest;
var pos:Point = new Point();
var count:Number = 0;
var max_count:Number = 6;
for each(photo_xml in photos_xml) {
Security.loadPolicyFile("http://farm"+photo_xml.attribute("farm")+".static.flickr.com/crossdomain.xml");
url = new URLRequest("http://farm"+photo_xml.attribute("farm")+".static.flickr.com/"+photo_xml.attribute("server")+"/"+photo_xml.attribute("id")+"_"+photo_xml.attribute("secret")+"_s.jpg");
photo = new ImageLoader(url, 75, 75);
photo.x = pos.x;
photo.y = pos.y;
_result_container.content.addChild(photo);
_result_items.push(photo);
if(count + 1 < max_count) {
count++;
pos.x += photo.width + 1;
}else{
count = 0;
pos.x = 0;
pos.y += photo.height + 1;
}
}
_result_container.update();
}
private function clear():void {
var photo:ImageLoader;
for each(photo in _result_items){
_result_container.content.removeChild(photo);
}
_result_container.update();
_result_items = new Array();
}
}
}
import flash.display.Bitmap;
import flash.events.Event;
import flash.net.URLRequest;
import flash.display.Loader;
import gs.TweenLite;
class ImageLoader extends Sprite {
private var _w:Number;
private var _h:Number;
private var _url:URLRequest;
private var _loader:Loader;
private var _container:Sprite;
private var _bg:Sprite;
public function ImageLoader(url:URLRequest, w:Number, h:Number):void {
_w = w;
_h = h;
_url = url;
init();
}
private function init():void {
_container = new Sprite();
_bg = new Sprite();
addChild(_bg);
addChild(_container);
setupAsset();
_loader = new Loader();
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleOnImageLoaded);
_loader.load(_url);
TweenLite.from(_bg, .3, {alpha:0});
}
private function handleOnImageLoaded(e:Event):void {
var bmp:Bitmap = e.target.content as Bitmap;
bmp.smoothing = true;
_container.addChild(bmp);
TweenLite.from(_container, .5, {alpha:0});
}
//Drawing the background sprite, wich will be seen until the image is completely loaded.
private function setupAsset():void {
_bg.graphics.beginFill(0xcccccc);
_bg.graphics.drawRect(0, 0, _w, _h);
_bg.graphics.endFill();
}
}
//////////////////////////////////////////////////////
//////////////////////////////////////////////////////
import flash.display.Sprite;
import com.bit101.components.TextArea;
var textarea:TextArea;
function inittrace(mainRef:Sprite):void {
textarea = new TextArea();
textarea.editable = false;
textarea.width = mainRef.stage.stageWidth;
textarea.height = 100;
textarea.y = mainRef.stage.stageHeight - textarea.height;
mainRef.addChild(textarea);
}
function doTrace(...args):void {
var s:String = "";
for each(var field:* in args) s += field.toString()+" ";
s+="\n";
try {textarea.text = s + textarea.text;}catch (e:Error){}
}
var trace:Function = doTrace;