Ticket #2661: animaps.pl

File animaps.pl, 3.7 KB (added by Rich.West@…, 13 years ago)

Updated animaps.pl using Image::Magick module (when needed)

Line 
1#!/usr/bin/perl -w
2#
3# animaps.pl
4#
5# This script grabs the source image(s) and provides an animated version of the
6# still images back to Myth.
7#
8# This script uses the maps.xml file as the list of available sources. Please
9# take a look at that file for examples of how to format particular sources.
10#
11################################################################################
12use strict;
13use Getopt::Std;
14use LWP::Simple;
15use Date::Manip;
16use MapSearch;
17use Data::Dumper;
18our ($opt_v, $opt_t, $opt_T, $opt_l, $opt_u, $opt_d);
19
20###
21#
22# These defines are required by the MythWeather modules.  What is defined here
23# will be displayed in the GUI.
24#
25###
26my $name = 'Animated_Regional_Map';
27my $version = 0.21;
28my $author = 'Lucien Dunning';
29my $email = 'ldunning@gmail.com';
30
31##
32# Some defaults.
33##
34my $updateTimeout = 10*60;
35my $retrieveTimeout = 30;
36my @types = ('amdesc', 'updatetime', 'animatedimage');
37##
38# Our xml file is local to the directory that the script is run in.
39##
40my $dir = "./";
41
42##
43# Get and parse the command line arguments
44##
45getopts('Tvtlu:d:');
46
47if (defined $opt_v) {
48    print "$name,$version,$author,$email\n";
49    exit 0;
50}
51
52if (defined $opt_T) {
53    print "$updateTimeout,$retrieveTimeout\n";
54    exit 0;
55}
56if (defined $opt_l) {
57    MapSearch::AddDescSearch(shift);
58    foreach my $result (@{MapSearch::doSearch()}) {
59        print "$result->{animation}::$result->{description}\n" if ($result->{animation});
60    }
61    exit 0;
62}
63
64if (defined $opt_t) {
65    foreach (@types) {print; print "\n";}
66    exit 0;
67}
68
69if (defined $opt_d) {
70    $dir = $opt_d;
71}
72
73my $loc = shift;
74
75if (!defined $loc || $loc eq "") {
76    die "Invalid usage";
77}
78
79sub animated_split
80{
81   my ($path) = shift;
82   my ($image) = @_;
83
84   my ($animated, $frame_count);
85
86   use Image::Magick;
87
88   $animated=Image::Magick->new;
89   $animated->Read($path . $image);
90   $animated->Deconstruct();
91   for($frame_count=0; $animated->[$frame_count]; $frame_count++)
92   {
93      $animated->[$frame_count]->Write($path . $frame_count);
94   }
95   return ($frame_count);
96}
97
98##
99# If we got to here, then we have all of the right arguments.  We should
100# only get one location via the command line options.
101#
102# First, we need to search through the XML file for the specified location
103# and get all of the necessary attributes of that location.
104##
105MapSearch::AddAniSearch($loc);
106my $results = MapSearch::doSearch();
107my $base = $results->[0]->{animation};
108my $desc = $results->[0]->{description};
109my $size = $results->[0]->{imgsize};
110my $file = $desc;
111$file =~ s/[^a-zA-Z0-9]//g;
112my $path = "$dir/$file-";
113my $image_count = 0;
114my $i=0;
115foreach my $image (sort @{$results->[0]->{images}}) {
116    ++$image_count;
117}
118##
119# Because Myth does not support animated GIFs, we have to do a little
120# hacking if the source is an animated GIF.
121#
122# Hack to check if there is only one image listed as the source for the
123# animated images.  If it is, then we assume it is an animated GIF (it better
124# be, darn it!) and we split it in to its individual frames.
125##
126if (($image_count == 1) && (@{$results->[0]->{images}}[0] =~ /.gif$/))
127{
128    # Get the Animated GIF
129    getstore("$base/" . @{$results->[0]->{images}}[0], $path . "anim.gif");
130    # Split it in to its individual parts
131    $i = &animated_split($path,"anim.gif");
132}
133else
134{
135   foreach my $image (sort @{$results->[0]->{images}}) {
136       getstore("$base/$image", $path . "$i");
137       ++$i;
138   }
139}
140# assume all the same size, so just check first
141if (!$size) {
142    use Image::Size;
143    my ($x, $y) = imgsize("${path}0");
144    $size = "${x}x$y" if ($x && $y);
145}
146
147print "amdesc::$desc\n";
148printf "animatedimage::${path}%%1-$i%s\n", ($size && "-$size" || '');
149print "updatetime::Last Updated on " . UnixDate("now", "%b %d, %I:%M %p %Z") . "\n";