MythTV  master
envcan_animaps.pl
Go to the documentation of this file.
1 #!/usr/bin/perl
2 # vim:ts=4:sw=4:ai:et:si:sts=4
3 #
4 # Animated 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 
13 use strict;
14 use warnings;
15 
16 use English;
17 use File::Path;
18 use File::Basename;
19 use Cwd 'abs_path';
20 use lib dirname(abs_path($0 or $PROGRAM_NAME)),
21  '/usr/share/mythtv/mythweather/scripts/ca_envcan',
22  '/usr/local/share/mythtv/mythweather/scripts/ca_envcan';
23 
24 use Getopt::Std;
25 use LWP::Simple;
26 use Date::Manip;
27 use ENVCANMapSearch;
28 use Image::Magick;
29 
30 our ($opt_v, $opt_t, $opt_T, $opt_l, $opt_u, $opt_d);
31 
32 my $name = 'ENVCAN-Animated-Map';
33 my $version = 0.5;
34 my $author = 'Joe Ripley / Gavin Hurlbut';
35 my $email = 'vitaminjoe@gmail.com / gjhurlbu@gmail.com';
36 my $updateTimeout = 10*60;
37 my $retrieveTimeout = 30;
38 my @types = ('amdesc', 'updatetime', 'animatedimage', 'copyright',
39  'copyrightlogo');
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 >= 10) { shift @image_list; }
103  }
104 }
105 
106 # Download map files, if necessary (maps are stale after 15 minutes)
107 my $i = 0;
108 my $outimage = Image::Magick->new;
109 foreach my $image (@image_list) {
110  my $getImage = 1;
111  if (-f "$path$i" ) {
112  my @stats = stat(_);
113  if ($stats[9] > (time - 900)) {
114  $outimage->Read( "$path$i" );
115  $i++;
116  next;
117  }
118  }
119 
120  getstore($base_url . $image, "$path$i");
121  $outimage->Read( "$path$i" );
122  $i++;
123 }
124 
125 $outimage->Write( filename => "$dir/$file.gif", delay => 75 );
126 
127 print "amdesc::$desc\n";
128 print "animatedimage::$dir/$file.gif\n";
129 print "updatetime::Last Updated on " .
130  UnixDate("now", "%b %d, %I:%M %p %Z") . "\n";
131 print "copyright::Environment Canada\n";
132 print "copyrightlogo::none\n";