/* * Gets temperature data from Yahoo RSS feed and displays it numerically * on the canvas. * * Copyright (C) 2012-2013 Mithat Konar */ // Constants: // change the URL to get the desired location and units // (see http://developer.yahoo.com/weather/). final String URL = "http://weather.yahooapis.com/forecastrss?w=12781882&u=f"; // path in XML data to current conditions: final String ELEMENT_CONDITION="channel/item/yweather:condition"; // attribute name in element above for current temperature: final String ATTRIBUTE_CONDITION_TEMPERATURE = "temp"; // path in XML data to element that holds units of measurement: final String ELEMENT_UNITS = "channel/yweather:units"; // attribute name in element above that holds units for temperature: final String ATTRIBUTE_UNIT_TEMPERATURE = "temperature"; // misc. final int CANVAS_BORDER=10; // Functions: void setup() { size(250, 140); smooth(); try { XML weather = loadXML(URL); // instantiate an XML object from URL XML condition = weather.getChild(ELEMENT_CONDITION); // pull current cond. XML units = weather.getChild(ELEMENT_UNITS); // pull units data background(#296783); PFont font; // we'll use this to hold font data font = loadFont("Monospaced.bold-48.vlw"); textFont(font, 48); textAlign(CENTER, CENTER); fill(#ffffff); text(condition.getInt(ATTRIBUTE_CONDITION_TEMPERATURE) + "\u00b0" + units.getString(ATTRIBUTE_UNIT_TEMPERATURE), width/2, height/2); } catch (Exception e) { println("Problem loading " + URL); println(e); // Turn the canvas red with a big 'X' in it to indicate error. background(#ff0000); strokeWeight(5); line(CANVAS_BORDER, CANVAS_BORDER, width-CANVAS_BORDER, height-CANVAS_BORDER); line(CANVAS_BORDER, height-CANVAS_BORDER, width-CANVAS_BORDER, CANVAS_BORDER); } println("Exiting app."); }