Eric Brotto
YaBB Newbies
Offline
Posts: 11
Re: Using the proXML load method more than once?
Reply #4 - Mar 6th , 2010, 1:32am
Thank you so much Koogy! Your suggestion has totally worked. Nice. In case anyone is curious, below is my current code. It is unfinished, so it might appear a bit messy, but it does solve the problem that we were talking about. Of course, feel free to use it. import proxml.*; int numberOfPhotos = 10; PFont font; String apiKey = " "; String nsid = " "; String url = "http://api.flickr.com/services/rest/"; XMLInOut xml = null; XMLInOut infoXml = null; String [] fm = new String [numberOfPhotos]; String [] sv = new String [numberOfPhotos]; String [] sc = new String [numberOfPhotos]; String [] photoID = new String [numberOfPhotos]; String [] photoURL = new String [numberOfPhotos]; String [] title = new String [numberOfPhotos]; String [] li = new String [numberOfPhotos]; int choose = 0; int counter = 0; Info info = new Info(); void setup() { font = loadFont("Futura-Medium-12.vlw"); size(1000,1500); background(0); frameRate(15); xml = new XMLInOut(this); infoXml = new XMLInOut(this,info); for (int i = 0; i < numberOfPhotos; i++){ photoID [i]= ""; photoURL [i]= ""; } getInterestingPhotos(); } void draw() { } void keyPressed() { if (key == 'i'){ nsid = ""; println(photoID[0]); //info.findInfo(photoID[0]); getInfo(); } if (key == 'l'){ //println(info.l); } if (key == ' '){ nsid = ""; printPhotos(); printMainPhoto(choose); } if (key == 'q') { if(choose < numberOfPhotos -1){ choose = choose + 1; println (choose); printMainPhoto(choose);}} if (key == 'w') { if(choose > 0){choose = choose - 1; println (choose); printMainPhoto(choose);}} } // Here we load a URL to our xml variable which will be sent to flickr to get 100 of the most recent interesting photos void getInterestingPhotos() { String rest = url+"?method=flickr.interestingness.getList"; rest += "&api_key=" + apiKey; xml.loadElement(rest); println("xml1 called"); } void getInfo () { String rest = url+"?method=flickr.photos.getInfo"; rest += "&api_key=" + apiKey + "&photo_id=" + photoID[1] ; println("REST " + rest); //String rest = url+"?method=flickr.interestingness.getList"; //rest += "&api_key=" + apiKey; infoXml.loadElement(rest); } void xmlEvent(proxml.XMLElement _x) { parseXML(_x); } void parseXML(proxml.XMLElement _x) { String stat = _x.getAttribute("stat"); if (!stat.equals("ok")) { println("Error from Flickr"); return; } proxml.XMLElement node = _x.getChild(0); String type = node.getName(); if (type.equals("user")) { nsid = node.getAttribute("nsid"); println(nsid); } else if (type.equals("photos")) { int num = node.countChildren(); println(num); getPhotos(node); println("photos node"); } } void getPhotos(proxml.XMLElement _n) { println ("prePhoto print method"); int cnt = _n.countChildren(); cnt = min(cnt,numberOfPhotos); for (int i=0;i<cnt;i++) { proxml.XMLElement ph = _n.getChild(i); fm [i] = ph.getAttribute("farm"); sv [i] = ph.getAttribute("server"); photoID[i] = ph.getAttribute("id"); sc [i] = ph.getAttribute("secret"); title [i] = ph.getAttribute("title"); //li [i] = ph.getAttribute("username"); //println( "This is license " + li[i]); photoURL[i] = "http://farm"+fm[i]+".static.flickr.com/"+ sv[i] + "/" + photoID[i] + "_" + sc[i] + "_s.jpg"; //println(); //println(i + " " + photoURL[i]); println (i + " " + title[i] + " id: " + photoID[i]); } } void printPhotos (){ for (int i = 0; i < numberOfPhotos; i++){ PImage img = loadImage(photoURL[i]); //println(); //println(photoURL[i]); int x = (i%5) * img.width; int y = (i/5) * img.height; image(img,x,y); } } void printMainPhoto (int chosen){ noStroke(); fill(0); rect(400, 0, 500,300); String bigPhoto = photoURL[chosen] = "http://farm"+fm[chosen]+".static.flickr.com/"+ sv[chosen] + "/" + photoID[chosen] + "_" + sc[chosen] + "_m.jpg"; PImage img = loadImage(bigPhoto); //println(photoURL[i]); fill(255); textFont(font); smooth(); text(title[chosen], 400,30); image(img,400,50); }public class Info { int numberOfPhotos = 10; String apiKey = "d1a7da3cf57e7fd369fffa90d9b06ffc"; String nsid = "7e0c62548999a06b"; String url = "http://api.flickr.com/services/rest/"; XMLInOut xml = null; Object a = new Object(); String [] fm = new String [numberOfPhotos]; String [] sv = new String [numberOfPhotos]; String [] sc = new String [numberOfPhotos]; String [] photoID = new String [numberOfPhotos]; String [] photoURL = new String [numberOfPhotos]; String [] title = new String [numberOfPhotos]; String [] li = new String [numberOfPhotos]; int choose = 0; int counter = 0; String var = new String(); Info () {var = null;} void xmlEvent(proxml.XMLElement _x) { parseXML(_x); println("info event method triggered"); } void parseXML(proxml.XMLElement _x) { String stat = _x.getAttribute("stat"); if (!stat.equals("ok")) { println("Error from Flickr - INFO"); return; } else {println ("info stat OK");} proxml.XMLElement node = _x.getChild(0); String type = node.getName(); if (type.equals("user")) { nsid = node.getAttribute("nsid"); println(nsid); } else if (type.equals("photo")) { int num = node.countChildren(); println(num); getInfo(node); println("gettingInfo");} } } void getInfo(proxml.XMLElement _n) { println("getInfo called"); int cnt = _n.countChildren(); cnt = min(cnt,numberOfPhotos); //for (int i=0;i<cnt;i++) { //proxml.XMLElement ph = _n.getChild(i); proxml.XMLElement ph = _n; println(ph); //li[i] = ph.getAttribute("title"); String