#!/usr/bin/perl
# weatherWall.pl
# Author: Gordon Hardy
# Website: http://www.gordonhardy.com
# Description: This application parses a Yahoo! weather XML feed and sets
# two image files based on the current weather conditions. Written for
# use on OS X.
# License: GPL
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
#user variables
$mode = "Photographs"; # later I plan to implement an "abstract art" option
$zip_code = "12345"; # your zip code
$pic_dir = "~/Pictures/WeatherWall/"; # location of weatherwall picture folders
$url = "http://weather.yahooapis.com/forecastrss?p=".$zip_code;
#configure which weather codes map to which generic types
#(the numbers are from the Yahoo! weather XML
$rain = "|3|4|9|11|12|37|38|39|40|45|47|";
$snow = "|5|6|7|8|10|13|14|15|16|17|18|25|35|41|42|43|46|";
$fog = "|20|21|22|";
$windy = "|23|24|";
$cloudy = "|26|27|28|29|30|44|";
$sunny = "|32|36|";
#retrieve weather data
$cmd = "curl -s '".$url."' > weather.tmp";
`$cmd`;
#load it into a string
open(INF,"weather.tmp");
@ary_inf = ;
close(INF);
$w = join("",@ary_inf);
#parse it and pull back current conditions text and code
$f_index = index($w,"= 0){
$folder = "Rain";
} elsif(index($snow,"|".$cur_cond_code."|") >= 0){
$folder = "Snow";
} elsif(index($fog,"|".$cur_cond_code."|") >= 0){
$folder = "Fog";
} elsif(index($windy,"|".$cur_cond_code."|") >= 0){
$folder = "Windy";
} elsif(index($cloudy,"|".$cur_cond_code."|") >= 0){
$folder = "Cloudy";
} elsif(index($sunny,"|".$cur_cond_code."|") >= 0){
$folder = "Sunny";
} else {
exit; #we don't have a weather match so don't change the background
}
#get list of pictures from that folder
$dir = $pic_dir.$mode."/".$folder."/";
$cmd = "ls ".$dir;
@ary_out = `$cmd`;
#pick a random image
$rand = int(rand(@ary_out+1));
$pic = @ary_out[$rand];
chomp($pic);
#copy the picture to be cycled on to the desktop
$pic_path = $dir.$pic;
$cmd = "cp ".$pic_path." ".$pic_dir."Current/1.jpg";
`$cmd`;
$cmd = "cp ".$pic_path." ".$pic_dir."Current/2.jpg";
`$cmd`;
exit;