Ticket #3056: animaps.pl

File animaps.pl, 3.6 KB (added by Rich.West@…, 13 years ago)
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
79##
80# If we got to here, then we have all of the right arguments.  We should
81# only get one location via the command line options.
82#
83# First, we need to search through the XML file for the specified location
84# and get all of the necessary attributes of that location.
85##
86MapSearch::AddAniSearch($loc);
87my $results = MapSearch::doSearch();
88my $base = $results->[0]->{animation};
89my $desc = $results->[0]->{description};
90my $size = $results->[0]->{imgsize};
91my $file = $desc;
92$file =~ s/[^a-zA-Z0-9]//g;
93my $path = "$dir/$file-";
94my $image_count = 0;
95my $i=0;
96foreach my $image (sort @{$results->[0]->{images}}) {
97    ++$image_count;
98}
99##
100# Because Myth does not support animated GIFs, we have to do a little
101# hacking if the source is an animated GIF.
102#
103# Hack to check if there is only one image listed as the source for the
104# animated images.  If it is, then we assume it is an animated GIF (it better
105# be, darn it!) and we split it in to its individual frames.
106##
107if (($image_count == 1) && (@{$results->[0]->{images}}[0] =~ /.gif$/))
108{
109    # Get the Animated GIF
110    getstore("$base/" . @{$results->[0]->{images}}[0], $path . "anim.gif");
111    # Split it in to its individual parts
112    system("/usr/bin/convert +adjoin -coalesce " . $path . "anim.gif " . $path . "%01d");
113    # Now, count up all of the individual pieces.
114system ("echo \"ls -l $path\" > /tmp/test.log");
115    open (LS, "ls -l $path\* |");
116    while (<LS>)
117    {
118       ++$i if ((/$path/) && !(/anim.gif/));
119    }
120    close (LS);
121}
122else
123{
124   foreach my $image (sort @{$results->[0]->{images}}) {
125       getstore("$base/$image", $path . "$i");
126       ++$i;
127   }
128}
129# assume all the same size, so just check first
130if (!$size) {
131    use Image::Size;
132    my ($x, $y) = imgsize("${path}0");
133    $size = "${x}x$y" if ($x && $y);
134}
135
136print "amdesc::$desc\n";
137printf "animatedimage::${path}%%1-$i%s\n", ($size && "-$size" || '');
138print "updatetime::Last Updated on " . UnixDate("now", "%b %d, %I:%M %p %Z") . "\n";