|
| 1 | +#!/usr/bin/perl |
| 2 | +# vim:ts=4:sw=4:ai:et:si:sts=4 |
| 3 | +# |
| 4 | +# Static satellite map grabber for Environment Canada. |
| 5 | +# |
| 6 | +# This script downloads satellite map data from the Environment Canada |
| 7 | +# website. It uses the lists of JPEG images supplied by the page at |
| 8 | +# http://www.weatheroffice.gc.ca/satellite/index_e.html. |
| 9 | +# |
| 10 | +# The bulk of the code in this script was originally authored by |
| 11 | +# Lucien Dunning (ldunning@gmail.com). |
| 12 | +# Based on envcan_animaps.pl by Joe Ripley <vitaminjoe@gmail.com> |
| 13 | + |
| 14 | +use strict; |
| 15 | +use warnings; |
| 16 | + |
| 17 | +use English; |
| 18 | +use File::Path; |
| 19 | +use File::Basename; |
| 20 | +use Cwd 'abs_path'; |
| 21 | +use lib dirname(abs_path($0 or $PROGRAM_NAME)), |
| 22 | + '/usr/share/mythtv/mythweather/scripts/ca_envcan', |
| 23 | + '/usr/local/share/mythtv/mythweather/scripts/ca_envcan'; |
| 24 | + |
| 25 | +use Getopt::Std; |
| 26 | +use LWP::Simple; |
| 27 | +use Date::Manip; |
| 28 | +use ENVCANMapSearch; |
| 29 | +use Image::Magick; |
| 30 | + |
| 31 | +our ($opt_v, $opt_t, $opt_T, $opt_l, $opt_u, $opt_d); |
| 32 | + |
| 33 | +my $name = 'ENVCAN-Static-Map'; |
| 34 | +my $version = 0.1; |
| 35 | +my $author = 'Gavin Hurlbut'; |
| 36 | +my $email = 'gjhurlbu@gmail.com'; |
| 37 | +my $updateTimeout = 10*60; |
| 38 | +my $retrieveTimeout = 30; |
| 39 | +my @types = ('smdesc', 'updatetime', 'map', 'copyright'); |
| 40 | +my $dir = "/tmp/envcan"; |
| 41 | + |
| 42 | +getopts('Tvtlu:d:'); |
| 43 | + |
| 44 | +if (defined $opt_v) { |
| 45 | + print "$name,$version,$author,$email\n"; |
| 46 | + exit 0; |
| 47 | +} |
| 48 | + |
| 49 | +if (defined $opt_T) { |
| 50 | + print "$updateTimeout,$retrieveTimeout\n"; |
| 51 | + exit 0; |
| 52 | +} |
| 53 | +if (defined $opt_l) { |
| 54 | + my $search = shift; |
| 55 | + ENVCANMapSearch::AddSatSearch($search); |
| 56 | + ENVCANMapSearch::AddSatClassSearch($search); |
| 57 | + ENVCANMapSearch::AddImageTypeSearch($search); |
| 58 | + foreach my $result (@{ENVCANMapSearch::doSearch()}) { |
| 59 | + print "$result->{entry_id}::($result->{satellite_class}) $result->{satellite} $result->{image_type}\n"; |
| 60 | + } |
| 61 | + exit 0; |
| 62 | +} |
| 63 | + |
| 64 | +if (defined $opt_t) { |
| 65 | + foreach (@types) {print; print "\n";} |
| 66 | + exit 0; |
| 67 | +} |
| 68 | + |
| 69 | +if (defined $opt_d) { |
| 70 | + $dir = $opt_d; |
| 71 | +} |
| 72 | + |
| 73 | +if (!-d $dir) { |
| 74 | + mkpath( $dir, {mode => 0755} ); |
| 75 | +} |
| 76 | + |
| 77 | +my $loc = shift; |
| 78 | + |
| 79 | +if (!defined $loc || $loc eq "") { |
| 80 | + die "Invalid usage"; |
| 81 | +} |
| 82 | + |
| 83 | +# Get map info |
| 84 | +ENVCANMapSearch::AddAniSearch($loc); |
| 85 | +my $results = ENVCANMapSearch::doSearch(); |
| 86 | +my $desc = $results->[0]->{satellite}; |
| 87 | + |
| 88 | +# Get HTML and find image list |
| 89 | +my $response = get $results->[0]->{animated_url}; |
| 90 | +die unless defined $response; |
| 91 | + |
| 92 | +my @image_list; |
| 93 | +my $size; |
| 94 | +my $base_url = "http://www.weatheroffice.gc.ca"; |
| 95 | +my $file = $loc; |
| 96 | +my $path = "$dir/$file"; |
| 97 | + |
| 98 | +# Get list of images (at most 10) |
| 99 | +foreach my $line (split(/\n/, $response)) { |
| 100 | + if ($line =~ /theImagesComplete\[\d*\] \= \"(.*)\"\;/) { |
| 101 | + push (@image_list, $1); |
| 102 | + if ($#image_list >= 1) { shift @image_list; } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +# Download map file, if necessary (maps are stale after 15 minutes) |
| 107 | +my $image = shift @image_list; |
| 108 | +my $ext = $image; |
| 109 | +$ext =~ s/.*\.(.*?)$/$1/; |
| 110 | + |
| 111 | +my $cached = 0; |
| 112 | +if (-f "$path.$ext" ) { |
| 113 | + my @stats = stat(_); |
| 114 | + if ($stats[9] > (time - 900)) { |
| 115 | + $cached = 1; |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +if (!$cached) { |
| 120 | + getstore($base_url . $image, "$path.$ext"); |
| 121 | +} |
| 122 | + |
| 123 | +print "smdesc::$desc\n"; |
| 124 | +print "map::$path.$ext\n"; |
| 125 | +print "updatetime::Last Updated on " . |
| 126 | + UnixDate("now", "%b %d, %I:%M %p %Z") . "\n"; |
| 127 | +print "copyright::Environment Canada\n"; |
0 commit comments