//////////////// String flickr_api_key = "your flickr api key"; String proxy_url = "your proxy url"; float s = 0.0; int indx = 0; Flickr flickr = new Flickr(flickr_api_key); void setup() { size(200,200); framerate(20); smooth(); PFont tipo; tipo = loadFont("sansserif.bold-16.vlw"); textFont(tipo,16); background(0, 0, 0); Hashtable args = new Hashtable(); args.put("tags", "daughter"); flickr.photosSearch(args); } void draw() { background(255); if(flickr.size() > 0) { if (s < 1.0) s += 0.1; scale(s); flickr.elementAt(indx).draw(); text( flickr.elementAt(indx).title, 5, 21); // interval = 5 sec. if (frameCount/framerate > 5) { frameCount = 0; indx++; if(flickr.size() <= indx) { indx = 0; } s = 0.0; } } fill(0, 102, 153); textMode(SCREEN); } void mousePressed() { indx++; if(flickr.size() <= indx) { indx = 0; } s = 0.0; } class Photo { String id; String owner; String secret; String title; boolean isPublic; boolean isFriend; boolean isFamily; // set after fetch String url; String label; private PImage _pimg; Photo(XMLElement xml) { _pimg = null; id = xml.getStringAttribute("id"); owner = xml.getStringAttribute("owner"); secret = xml.getStringAttribute("secret"); title = xml.getStringAttribute("title"); isPublic = xml.getStringAttribute("ispublic").equals("1"); isFriend = xml.getStringAttribute("isfriend").equals("1"); isFamily = xml.getStringAttribute("isfamily").equals("1"); } void setData(PImage pimg, String _url, String _label) { _pimg = pimg; url = _url; label = _label; } void draw() { int x = 0, y = 0; int iwidth, iheight; float dx = (float)width / _pimg.width; float dy = (float)height / _pimg.height; float d = 1.0; if (dx < dy) d = dx; else d = dy; iwidth = (int)(d * _pimg.width); iheight = (int)(d * _pimg.height); if (width > iwidth) { x = (width - iwidth) / 2; } if (height > iheight) { y = (height - iheight) / 2; } image(_pimg, x, y, iwidth, iheight); } } public void stop() { flickr.stop(); super.stop(); } // Flickr Object class Flickr { private String _api_key; private boolean _stopFetching; private Vector _fetched_photos; private Vector _fetching_photos; private Flickr(String flickr_api_key) { _stopFetching = false; _fetched_photos = new Vector(); _fetching_photos = new Vector(); _api_key = flickr_api_key; } public int size() { return _fetched_photos.size(); } public Photo elementAt(int n) { return (Photo)_fetched_photos.elementAt(n); } public void photosSearch(final Hashtable args) { if (_fetched_photos.size() > 0) _fetched_photos.clear(); if (_fetching_photos.size() > 0) _fetching_photos.clear(); // thread start new Thread( new Runnable() { public void run() { FlickrAPI api = new FlickrAPI(_api_key); FlickrAPIResponse rsp = api.photos_search(args); if (rsp.checkStat()) { XMLElement photos = (XMLElement)rsp.getXML().getChildren().elementAt(0); Enumeration enum = photos.enumerateChildren(); while (enum.hasMoreElements() && !_stopFetching) { XMLElement photo = (XMLElement)enum.nextElement(); String id = photo.getStringAttribute("id"); if (id != null) { // create photo Photo p = new Photo(photo); // stack photo _fetching_photos.addElement(p); } } } // fetching start new Thread( new _fetchPhotoThread() ).start(); } } ).start(); } class _fetchPhotoThread implements Runnable { public void run() { for (int i = 0; _fetching_photos.size() > 0 && !_stopFetching; i++) { Photo p = (Photo)_fetching_photos.elementAt(0); _fetching_photos.removeElementAt(0); print("fetching: " + p.id); FlickrAPI api = new FlickrAPI(_api_key); FlickrAPIResponse rsp = api.photos_getSizes(p.id); if (rsp.checkStat()) { XMLElement sizes = (XMLElement)rsp.getXML().getChildren().elementAt(0); Enumeration enum = sizes.enumerateChildren(); while (enum.hasMoreElements()) { XMLElement sz = (XMLElement)enum.nextElement(); if (sz.getName().equals("size")) { if (sz.getAttribute("label").equals("Small")) { String url = sz.getStringAttribute("source"); if (url != null) { if (online) { url = proxy_url + "flickr_photo.php?url=" + url; } println("load : " + url); PImage pimg = loadImage(url); if (pimg == null) { println(" fail -> skip"); break; } println(" done"); p.setData(pimg, url, "Small"); _fetched_photos.addElement(p); if(_fetched_photos.size() > 20) { _fetching_photos.clear(); } break; } } } } } } } } void stop() { _stopFetching = true; } } // API Respons Object class FlickrAPIResponse { private String _api_key; XMLElement _xml; FlickrAPIResponse(String api_key, XMLElement xml) { _api_key = api_key; _xml = xml; } public String getAPIKey() { return _api_key; } public boolean checkStat() { if (_xml.getName().equals("rsp")) { if (_xml.getStringAttribute("stat").equals("ok")) { return true; } } return false; } XMLElement getXML() { return _xml; } } // FlickrAPI lapper object class FlickrAPI { private String _ep; // end point url private String _api_key; // Flickr api key FlickrAPI (String flickr_api_key) { _ep = online ? proxy_url + "flickr_proxy.php?" : "http://www.flickr.com/services/rest/?"; _api_key = flickr_api_key; } private String _join(String[] strings) { String ret = ""; for(int i=0; i