00001 #!/usr/bin/perl
00002 # vim:ts=4:sw=4:ai:et:si:sts=4
00003 #
00004 # Animated satellite map grabber for Environment Canada.
00005 #
00006 # This script downloads satellite map data from the Environment Canada
00007 # website. It uses the lists of JPEG images supplied by the page at
00008 # http://www.weatheroffice.gc.ca/satellite/index_e.html.
00009 #
00010 # The bulk of the code in this script was originally authored by
00011 # Lucien Dunning (ldunning@gmail.com).
00012
00013 use strict;
00014 use warnings;
00015
00016 use English;
00017 use File::Path;
00018 use File::Basename;
00019 use Cwd 'abs_path';
00020 use lib dirname(abs_path($0 or $PROGRAM_NAME)),
00021 '/usr/share/mythtv/mythweather/scripts/ca_envcan',
00022 '/usr/local/share/mythtv/mythweather/scripts/ca_envcan';
00023
00024 use Getopt::Std;
00025 use LWP::Simple;
00026 use Date::Manip;
00027 use ENVCANMapSearch;
00028 use Image::Magick;
00029
00030 our ($opt_v, $opt_t, $opt_T, $opt_l, $opt_u, $opt_d);
00031
00032 my $name = 'ENVCAN-Animated-Map';
00033 my $version = 0.4;
00034 my $author = 'Joe Ripley';
00035 my $email = 'vitaminjoe@gmail.com';
00036 my $updateTimeout = 10*60;
00037 my $retrieveTimeout = 30;
00038 my @types = ('amdesc', 'updatetime', 'animatedimage', 'copyright');
00039 my $dir = "/tmp/envcan";
00040
00041 getopts('Tvtlu:d:');
00042
00043 if (defined $opt_v) {
00044 print "$name,$version,$author,$email\n";
00045 exit 0;
00046 }
00047
00048 if (defined $opt_T) {
00049 print "$updateTimeout,$retrieveTimeout\n";
00050 exit 0;
00051 }
00052 if (defined $opt_l) {
00053 my $search = shift;
00054 ENVCANMapSearch::AddSatSearch($search);
00055 ENVCANMapSearch::AddSatClassSearch($search);
00056 ENVCANMapSearch::AddImageTypeSearch($search);
00057 foreach my $result (@{ENVCANMapSearch::doSearch()}) {
00058 print "$result->{entry_id}::($result->{satellite_class}) $result->{satellite} $result->{image_type}\n";
00059 }
00060 exit 0;
00061 }
00062
00063 if (defined $opt_t) {
00064 foreach (@types) {print; print "\n";}
00065 exit 0;
00066 }
00067
00068 if (defined $opt_d) {
00069 $dir = $opt_d;
00070 }
00071
00072 if (!-d $dir) {
00073 mkpath( $dir, {mode => 0755} );
00074 }
00075
00076 my $loc = shift;
00077
00078 if (!defined $loc || $loc eq "") {
00079 die "Invalid usage";
00080 }
00081
00082 # Get map info
00083 ENVCANMapSearch::AddAniSearch($loc);
00084 my $results = ENVCANMapSearch::doSearch();
00085 my $desc = $results->[0]->{satellite};
00086
00087 # Get HTML and find image list
00088 my $response = get $results->[0]->{animated_url};
00089 die unless defined $response;
00090
00091 my @image_list;
00092 my $size;
00093 my $base_url = "http://www.weatheroffice.gc.ca";
00094 my $file = $loc;
00095 my $path = "$dir/$file-";
00096
00097 # Get list of images (at most 10)
00098 foreach my $line (split(/\n/, $response)) {
00099 if ($line =~ /theImagesComplete\[\d*\] \= \"(.*)\"\;/) {
00100 push (@image_list, $1);
00101 if ($#image_list >= 10) { shift @image_list; }
00102 }
00103 }
00104
00105 # Download map files, if necessary (maps are stale after 15 minutes)
00106 my $i = 0;
00107 my $outimage = Image::Magick->new;
00108 foreach my $image (@image_list) {
00109 my $getImage = 1;
00110 if (-f "$path$i" ) {
00111 my @stats = stat(_);
00112 if ($stats[9] > (time - 900)) {
00113 $outimage->Read( "$path$i" );
00114 $i++;
00115 next;
00116 }
00117 }
00118
00119 getstore($base_url . $image, "$path$i");
00120 $outimage->Read( "$path$i" );
00121 $i++;
00122 }
00123
00124 $outimage->Write( filename => "$dir/$file.gif", delay => 75 );
00125
00126 print "amdesc::$desc\n";
00127 print "animatedimage::$dir/$file.gif\n";
00128 print "updatetime::Last Updated on " .
00129 UnixDate("now", "%b %d, %I:%M %p %Z") . "\n";
00130 print "copyright::Environment Canada\n";