My weather app

I made this weather app using Processing and the Yahoo Weather API I had a lot of fun programming this app it was my first time working with XML. Excuse my english.

//https://developer.yahoo.com/weather/

String woeid="3444";
String units="c";
String url="http://weather.yahooapis.com/forecastrss?w="+woeid+"&u="+units;

String lastbuilddate;
String location;
int humidity;
String sunrise;
String sunset;
String currenttext;
int temp;
String[] day=new String[5];
int[] low=new int[5];
int[] high=new int[5];
String[] text=new String[5];
PGraphics pg;
int[] box={1,5,6,25,26,49};

void setup(){
  size(320,550);
  frameRate(1);

  pg=createGraphics(width,height);
  pg.beginDraw();
  pg.colorMode(RGB,height,255,width);
  for(int Y=0; Y<height; Y++){
    for(int X=0; X<width; X++){
      pg.stroke(Y,240,X);
      pg.point(X,Y);
    }
  }
  pg.colorMode(RGB,255);
  pg.noStroke();
  pg.rectMode(CORNERS);
  pg.fill(255,100);
  for(int I=0; I<box.length/2; I++){
    pg.rect(width/20,height/50*box[I*2],width-width/20,height/50*box[I*2+1]);
  }
  pg.endDraw();
}

void draw(){
  if(!loadXML(url).getChild("channel/lastBuildDate").getContent().equals(lastbuilddate)){
    update();
  }
}

void update(){
  XML xml=loadXML(url);

  lastbuilddate=xml.getChild("channel/lastBuildDate").getContent();
  location=xml.getChild("channel/yweather:location").getString("city");
  humidity=xml.getChild("channel/yweather:atmosphere").getInt("humidity");
  sunrise=xml.getChild("channel/yweather:astronomy").getString("sunrise");
  sunset=xml.getChild("channel/yweather:astronomy").getString("sunset");
  currenttext=xml.getChild("channel/item/yweather:condition").getString("text");
  temp=xml.getChild("channel/item/yweather:condition").getInt("temp");

  XML[] forecast=xml.getChildren("channel/item/yweather:forecast");
  for(int I=0; I<forecast.length; I++){
    day[I]=forecast[I].getString("day");
    low[I]=forecast[I].getInt("low");
    high[I]=forecast[I].getInt("high");
    text[I]=forecast[I].getString("text");
  }

  println("Update");

  clear();

  image(pg,0,0);

  fill(0);
  textAlign(CENTER,CENTER);
  textSize(height/50*(box[0]+box[1])/2-(textWidth(location)/10));
  text(location,width/2,height/50*(box[0]+box[1])/2);
  textSize(20);
  text(currenttext,width/2,height/50*(box[2]+2));
  textSize(75);
  text(temp+"°"+units.toUpperCase(),width/2,height/50*(box[2]+7));
  textSize(15);

  textAlign(LEFT,CENTER);
  text("Max "+high[0]+"°"+units.toUpperCase(),width/10,height/50*(box[2]+13));
  text("Min "+low[0]+"°"+units.toUpperCase(),width/10,height/50*(box[2]+15));
  text("Humidity "+humidity+"%",width/10,height/50*(box[2]+17));

  textAlign(RIGHT,CENTER);
  text("Sunrise "+sunrise,width-width/10,height/50*(box[2]+13));
  text("Sunset "+sunset,width-width/10,height/50*(box[2]+15));

  textAlign(CENTER,CENTER);
  for(int I=0; I<forecast.length; I++){
    text(day[I]+"-"+text[I],width/2,height/50*(box[4]+(I*2+1)*(box[5]-box[4])/(forecast.length*2+1.0)));
    text("Max "+high[I]+"°"+units.toUpperCase()+"  Min "+low[I]+"°"+units.toUpperCase(),width/2,height/50*(box[4]+(I*2+2)*(box[5]-box[4])/(forecast.length*2+1.0)));
  }
}

I know that in void update() there is 2 for(int I=0; I<forecast.length; I++) and that I could just made 1.

Comments

Sign In or Register to comment.