1 #!/usr/bin/perl -w 2 ############################################################### 3 # The Weather Note program downloads weather inforamtion from the net, 4 # extracts desired data and then displays it using the perl module GD.pm. 5 # The GD.pm enables a picture to be used as a background or it has tools 6 # for creating image graphics. Once the image is incorporated, text can be 7 # overlayed. 8 # Author: Mackie Zwald 9 # Date: April 12, 2000 10 # Program Name: Weather Note 11 ############################################################### 12 use GD 13 #Download the current weather information and save it to a file. 14 system ("lynx -dump http://www.wunderground.com/US/OH/Columbus.html >0409WundWeath"); 15 #Open the file and extract the temperature, wind, humidity and current conditions. 16 open (WEATH_INFO, "0409WundWeath") || die "no WundWeath today: $!"; 17 while () { 18 if (/Temperature\s\d+.*/) { 19 @temp_info = split; 20 shift(@temp_info); 21 } 22 if (/^\s+\bWind\b.*mph/) { 23 @wind_info = split; 24 } 25 if (/^\s+Wind\sCalm/) { 26 @wind_info = split; 27 } 28 if (/Humidity\s\d\d/) { 29 @humid_info = split; 30 } 31 if (/^\s+Conditions/) { 32 @condition_info = split; 33 shift(@condition_info); 34 } 35 if (/Updated:/) { 36 @time_info = split; 37 shift @time_info; 38 } 39 } 40 close WEATH_INFO; 41 #The next 5 lines were included to test the information that had been downloaded. 42 #You may uncomment them to see if the program is obtaining the right information. 43 #print "@temp_info 44 #@wind_info 45 #@condition_info 46 #@humid_info 47 #@time_info\n"; 48 #Next select the appropriate image to go with the current weather condition. 49 #The default is the "sunny" image because I am an optimist. 50 while (<@condition_info>) { 51 if ($condition_info[0] =~ /partly/i && $condition_info[1] =~ /cloudy/i) { 52 $image_png = "partlycloudy.png"; 53 }elsif ($condition_info[1] =~ /clouds/i) { 54 $image_png = "partlycloudy.png"; 55 }elsif ($condition_info[0] =~ /rain/i) { 56 $image_png = "rain.png"; 57 }elsif ($condition_info[1] =~ /rain/i) { 58 $image_png = "rain.png"; 59 }elsif ($condition_info[0] =~ /mist/i) { 60 $image_png = "rain.png"; 61 }elsif ($condition_info[0] =~ /mostly/i && $condition_info[1] =~ /cloudy/i) { 62 $image_png = "mostlycloudy.png"; 63 }elsif ($condition_info[0] =~ /partly/i && $condition_info[1] =~ /sunny/i) { 64 $image_png = "partlycloudy.png"; 65 }else { 66 $image_png = "sunny.png"; 67 } 68 } 69 #Open the image 70 open (PNG, $image_png) || die "couldn't open $image_png $!"; 71 $myImage = newFromPng GD::Image(\*PNG) || die; 72 close PNG; 73 #allocate colors for the text 74 $black = $myImage->colorAllocate(0,0,0); 75 #Put the text onto the image. 76 $myImage->string(gdMediumBoldFont,30,5,"Columbus, OH",$black); 77 $myImage->string(gdMediumBoldFont,45,20,"@time_info[4,5]",$black); 78 $myImage->string(gdGiantFont,55,35,"@temp_info",$black); 79 $myImage->string(gdSmallFont,10,50,"@wind_info[0,1,2]",$black); 80 $myImage->string(gdSmallFont,20,60,"@wind_info[3,4]",$black); 81 $myImage->string(gdSmallFont,10,70,"@humid_info",$black); 82 $myImage->string(gdSmallFont,10,80,"@condition_info",$black); 83 $myImage->string(gdSmallFont,10,90,"at @time_info[0,1]",$black); 84 binmode STDOUT; 85 #The "$png_data" section sends the script output to the screen (DISPLAY) or to a file 86 #(weath_note_img.png). You should comment or uncomment the desired section. 87 #For our puposes we send it to a file that is referenced on a html page. 88 #$png_data = $myImage->png; 89 # open (DISPLAY,"| display -") || die; 90 # binmode DISPLAY; 91 # print DISPLAY $png_data; 92 # close DISPLAY; 93 94 95 $png_data = $myImage->png; 96 open (OUT, ">weath_note_img.png") || die "$0:couldn't open weath_note_img.png\n"; 97 binmode OUT; 98 print OUT "$png_data"; 99 close OUT;