// Craiglist search demo - November 2011 - rtwomey@u.washington.edu // // // void setup() { size(200, 200); background(50); fill(200); String baseURL = "http://seattle.craigslist.org/search/fua?"; // search for furniture String searchString="cubicle"; // what we are searching for String query = baseURL+"query="+searchString+"&format=rss"; println(query); // do actual query call XMLElement xml = new XMLElement(this, query); // holds the results //println(xml); // gather results XMLElement[] items = xml.getChildren("item"); // get item results //println(items); println(items.length + " results:"); //loop over results for (int i = 0; i < items.length; i++) { String titleEl = items[i].getChild("title").getContent(); String desc = items[i].getChild("description").getContent(); String date = items[i].getChild("dc:date").getContent(); String url = items[i].getChild("link").getContent(); // display me! //println(items[i]); String title = titleEl.replaceAll("FS:\\s", ""); title = title.replaceAll("(\\(.*\\))", ""); // strip locations title = title.replaceAll("\\$\\d*", ""); // strip costs title = title.trim(); //println(title); // grab cost if present Pattern p = Pattern.compile("\\$\\d*"); Matcher m = p.matcher(titleEl); boolean found = m.find(); String cost; if (found) { //println(found); cost = m.group(); } else { cost="not specified"; }; // grab location Pattern q = Pattern.compile("(-->Location: ).*\\<"); Matcher n = q.matcher(desc); found = n.find(); String location; if(found) { location = n.group(); location = location.replaceAll("-->Location: ",""); location = location.replaceAll("\\<",""); } else { location="not found"; } println(title); println(cost); println(location); println(); // or process me with regular expressions // or string operations // or draw me to screen. store me for later. } } void draw() { }