Guest User

Yahoo Weather

a guest
Mar 22nd, 2012
1,992
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 8.41 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. #################################################################
  4. # Yahoo Weather Rss Information Atomizer
  5. # Version 0.7.7
  6. # Loud-Soft.com
  7. # Provided As Is
  8. #################################################################
  9.  
  10. use strict;
  11. use XML::XPath;
  12. use LWP::Simple;
  13. use XML::XPath::XMLParser;
  14. use Getopt::Long;
  15. use File::Copy;
  16.  
  17. #################################################################
  18. # Variables
  19. #################################################################
  20. # Constants (Change these to localize)
  21. my $zipcode = "CAXX0504";
  22. my $unit = "c";
  23. my $scripthome = "/Users/haroon/Documents/yweather-dir/";
  24. my $icondir = $scripthome."images/";
  25. my $datadir = $scripthome."data/";
  26. my $datafile = $datadir."weather.xml";
  27. my $imagefile = $icondir."weather.png";
  28.  
  29. # Constants (Do not change these)
  30. my $pre="yweather";
  31. my $uri="http://xml.weather.yahoo.com/ns/rss/1.0";
  32. my $url="http://xml.weather.yahoo.com/forecastrss?p=$zipcode&u=$unit";
  33. my %data;
  34. my $xp;
  35.  
  36. #################################################################
  37. # Subroutines
  38. #################################################################
  39. # Print usage
  40. sub usage {
  41.     print "Yahoo Weather Information\n\n";
  42.     print "Usage:\n";
  43.     print " ./yweather.pl -ct   Displays current temperature\n\n";
  44.     print "Arguments: \n";
  45.     print " -lc             City    \n";
  46.     print " -lr         Region\n";
  47.     print " -lt         Country\n";
  48.     print " -cc         Weather Code (used for images)\n";
  49.     print " -ct         Current Temperature\n";
  50.     print " -cfl        Current Feels Like\n";
  51.     print " -cw         Current Weather Description\n";
  52.     print " -cd         Current Date\n";
  53.     print " -ah         Current Humidity\n";
  54.     print " -av         Current Visibilty\n";
  55.     print " -ap         Current Barometric Pressure\n";
  56.     print " -ar         Change in Barometric Pressure\n";
  57.     print " -sr         Time of Sunrise\n";
  58.     print " -ss         Time of Sunset\n";
  59.     print " -wc         Current Wind Chill\n";
  60.     print " -wd         Current Wind Direction\n";
  61.     print " -ws         Current Wind Speed\n";
  62.     print " -ut         Temperature Unit\n";
  63.     print " -ud         Distance Unit\n";
  64.     print " -up         Pressure Unit \n";
  65.     print " -us         Speed Unit\n";
  66.     print " -fd1            Today's Day\n";
  67.     print " -fg1            Today's Date\n";
  68.     print " -fl1            Today's Low Temp\n";
  69.     print " -fh1            Today's High Temp\n";
  70.     print " -ft1            Today's Description\n";
  71.     print " -fc1            Today's Weather Code\n";
  72.     print " -fd2            Tomorrow's Day\n";
  73.     print " -fg2            Tomorrow's Date\n";
  74.     print " -fl2            Tomorrow's Low Temp\n";
  75.     print " -fh2            Tomorrow's High Temp\n";
  76.     print " -ft2            Tomorrow's Description\n";
  77.     print " -fc2            Tomorrow's Weather Code\n";
  78.     print " --copyimage     Copy Appropriate Image to Current Image (deprecated)\n";
  79.     print " --update        Update xml source file\n"   ;
  80.     print " \n";
  81.     print "All data is returned without units. To get data with units,\n";
  82.     print "use a combination of commands.\n\n";
  83.     print "Example: (Displays Current temperature with unit)\n";
  84.     print " ./yweather.pl -ct && ./yweather.pl -ut\n";
  85. }
  86.  
  87. # Print data
  88. sub args{
  89.     my ($arg) = @_;
  90.     print $data{$arg} . "\n";
  91. }
  92.  
  93. # Subroutine to update xml data from yahoo
  94. sub update_weather {
  95.     LWP::Simple::getstore($url,$datafile);
  96. }
  97.  
  98. # Subroutine to download images from yahoo
  99. sub get_images {
  100.     my $imgurl = "http://l.yimg.com/a/i/us/nws/weather/gr/";
  101.     for (0..47) {
  102.         LWP::Simple::getstore($imgurl.$_."d.png",$icondir.$_."d.png");
  103.         LWP::Simple::getstore($imgurl.$_."n.png",$icondir.$_."n.png");
  104.     }
  105. }
  106.  
  107. # Parse XML
  108. sub get_data {
  109.     my $ret;
  110.     my($element, $attribute, $index) = @_;
  111.     if ($index){$index=1;}
  112.    
  113.     my $nodeset = $xp->find("//yweather:$element");
  114.     my $node = $nodeset->get_node($index);
  115.     $ret = $node->getAttribute($attribute);
  116.     return $ret;
  117. }
  118.  
  119. # Calculate Feels Like (given temperature in fahrenheit and humidity)
  120. # source http://en.wikipedia.org/wiki/Heat_index#Formula
  121. sub calculate_feels_like {
  122.     my $unit = $data{'ut'};
  123.  
  124.     # convert temperature to Fahrenheit
  125.     my $tempf = $unit eq 'C' ? $data{'ct'} * 9/5 + 32 : $data{'ct'};
  126.     my $humid = $data{'ah'};
  127.  
  128.     my $feels;
  129.  
  130.     # heat index calculation is only useful when temperature > 80F and humidity > 40%
  131.     if ($humid > 40 && $tempf > 80) {
  132.         $feels = -42.379 + 2.04901523 * $tempf + 10.14333127 * $humid
  133.             - 0.22475541 * $tempf * $humid - 6.83783 * 10**(-3)*($tempf**(2))
  134.             - 5.481717 * 10**(-2)*($humid**(2))
  135.             + 1.22874 * 10**(-3)*($tempf**(2))*($humid)
  136.             + 8.5282 * 10**(-4)*($tempf)*($humid**(2))
  137.             - 1.99 * 10**(-6)*($tempf**(2))*($humid**(2));
  138.  
  139.         # convert back to original unit if necessary
  140.         if ($unit eq 'C') {
  141.             $feels = ($feels - 32) * 5/9;
  142.         }
  143.     } else {
  144.         # simply return wind chill
  145.         $feels = $data{'wc'};
  146.     }
  147.     return ($feels == int($feels)) ? int($feels) : int($feels + 1);
  148. }
  149.  
  150. # Copy correct image to the image define in $imagefile
  151. sub copy_image {
  152.     my ($second, $minute, $hour, $dayOfMonth, $month,
  153.         $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
  154.     my $night = $data{'ss'};
  155.     my $morning = $data{'sr'};
  156.     my $imagesub;
  157.    if ($hour > 11){
  158.         if(($hour-12) < int(substr($night,0,1))){
  159.             $imagesub = "d";
  160.         }elsif(($minute) < int(substr($night,2,3))){
  161.             $imagesub = "d";
  162.         }else{
  163.             $imagesub = "n";
  164.         }
  165.     } else {
  166.         if(($hour) < int(substr($morning,0,1))){
  167.             $imagesub = "n";
  168.         }elsif(($minute) < int(substr($morning,2,3))){
  169.             $imagesub = "n";
  170.         }else{
  171.             $imagesub = "d";
  172.         }
  173.     }
  174.     File::Copy::copy($icondir.$data{'cc'}.$imagesub.".png", $imagefile)
  175.         or die $icondir . $data{'cc'}.$imagesub.".png" . " could not be copied to " .
  176.         $imagefile;
  177. }
  178.  
  179. #################################################################
  180. # Check that files exist
  181. #################################################################
  182. #ensure directories exist
  183. unless(-d $datadir){
  184.     mkdir $datadir or die;
  185. }
  186. unless(-d $icondir){
  187.     mkdir $icondir or die;
  188. }
  189.  
  190. # Check if images exist
  191. if (!(-e $icondir."0d.png")){get_images()}
  192.  
  193. # Check if weather.xml exists
  194. if (!(-e $datafile)){update_weather()}
  195. $xp = XML::XPath->new(filename => $datafile);
  196. $xp->set_namespace($pre, $uri);
  197.  
  198.  
  199. #################################################################
  200. # Data Setup
  201. #################################################################
  202. # Location Information
  203. $data{'lc'} = get_data("location","city");
  204. $data{'lr'} = get_data("location","region");
  205. $data{'lt'} = get_data("location","country");
  206.  
  207. # Current Weather Information
  208. $data{'cc'} = get_data("condition","code");
  209. $data{'ct'} = get_data("condition","temp");
  210. $data{'cw'} = get_data("condition","text");
  211. $data{'cd'} = get_data("condition","date");
  212.  
  213. # Current Atmosphere Information
  214. $data{'ah'} = get_data("atmosphere","humidity");
  215. $data{'av'} = get_data("atmosphere","visibility");
  216. $data{'ap'} = get_data("atmosphere","pressure");
  217. $data{'ar'} = get_data("atmosphere","rising");
  218.  
  219. # Todays Sunrise and sunset
  220. $data{'sr'} = get_data("astronomy","sunrise");
  221. $data{'ss'} = get_data("astronomy","sunset");
  222.  
  223. # Current wind information
  224. $data{'wc'} = get_data("wind","chill");
  225. $data{'wd'} = get_data("wind","direction");
  226. $data{'ws'} = get_data("wind","speed");
  227.  
  228. # Unit information
  229. $data{'ut'} =get_data("units","temperature");
  230. $data{'ud'} =get_data("units","distance");
  231. $data{'up'} =get_data("units","pressure");
  232. $data{'us'} =get_data("units","speed");
  233.  
  234. # Forecast (Today)
  235. $data{'fd1'} =get_data("forecast[1]","day");
  236. $data{'fg1'} =get_data("forecast[1]","date");
  237. $data{'fl1'} =get_data("forecast[1]","low");
  238. $data{'fh1'} =get_data("forecast[1]","high");
  239. $data{'ft1'} =get_data("forecast[1]","text");
  240. $data{'fc1'} =get_data("forecast[1]","code");
  241.  
  242. # Forecast (Tomorrow)
  243. $data{'fd2'} =get_data("forecast[2]","day");
  244. $data{'fg2'} =get_data("forecast[2]","date");
  245. $data{'fl2'} =get_data("forecast[2]","low");
  246. $data{'fh2'} =get_data("forecast[2]","high");
  247. $data{'ft2'} =get_data("forecast[2]","text");
  248. $data{'fc2'} =get_data("forecast[2]","code");
  249.  
  250. # Current feels like
  251. $data{'cfl'} = calculate_feels_like();
  252.  
  253. # Check if image exist
  254. if (!(-e $imagefile)){copy_image()}
  255.  
  256. #################################################################
  257. # Parse arguments
  258. #################################################################
  259. if(($#ARGV + 1) == 1){
  260.     my $arg = substr($ARGV[0],1);
  261.     if (defined($data{$arg})){
  262.         args($arg);
  263.     } elsif($arg eq "-update"){
  264.         update_weather();
  265.     } elsif($arg eq "-copyimage"){
  266.         copy_image();
  267.     } else {
  268.         usage();
  269.     }
  270. } else {
  271.     usage();
  272. }
Advertisement
Add Comment
Please, Sign In to add comment