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