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 | ################################################################################ |
---|
12 | use strict; |
---|
13 | use Getopt::Std; |
---|
14 | use LWP::Simple; |
---|
15 | use Date::Manip; |
---|
16 | use MapSearch; |
---|
17 | use Data::Dumper; |
---|
18 | our ($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 | ### |
---|
26 | my $name = 'Animated_Regional_Map'; |
---|
27 | my $version = 0.21; |
---|
28 | my $author = 'Lucien Dunning'; |
---|
29 | my $email = 'ldunning@gmail.com'; |
---|
30 | |
---|
31 | ## |
---|
32 | # Some defaults. |
---|
33 | ## |
---|
34 | my $updateTimeout = 10*60; |
---|
35 | my $retrieveTimeout = 30; |
---|
36 | my @types = ('amdesc', 'updatetime', 'animatedimage'); |
---|
37 | ## |
---|
38 | # Our xml file is local to the directory that the script is run in. |
---|
39 | ## |
---|
40 | my $dir = "./"; |
---|
41 | |
---|
42 | ## |
---|
43 | # Get and parse the command line arguments |
---|
44 | ## |
---|
45 | getopts('Tvtlu:d:'); |
---|
46 | |
---|
47 | if (defined $opt_v) { |
---|
48 | print "$name,$version,$author,$email\n"; |
---|
49 | exit 0; |
---|
50 | } |
---|
51 | |
---|
52 | if (defined $opt_T) { |
---|
53 | print "$updateTimeout,$retrieveTimeout\n"; |
---|
54 | exit 0; |
---|
55 | } |
---|
56 | if (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 | |
---|
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 | my $loc = shift; |
---|
74 | |
---|
75 | if (!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 | ## |
---|
86 | MapSearch::AddAniSearch($loc); |
---|
87 | my $results = MapSearch::doSearch(); |
---|
88 | my $base = $results->[0]->{animation}; |
---|
89 | my $desc = $results->[0]->{description}; |
---|
90 | my $size = $results->[0]->{imgsize}; |
---|
91 | my $file = $desc; |
---|
92 | $file =~ s/[^a-zA-Z0-9]//g; |
---|
93 | my $path = "$dir/$file-"; |
---|
94 | my $image_count = 0; |
---|
95 | my $i=0; |
---|
96 | foreach 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 | ## |
---|
107 | if (($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. |
---|
114 | system ("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 | } |
---|
122 | else |
---|
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 |
---|
130 | if (!$size) { |
---|
131 | use Image::Size; |
---|
132 | my ($x, $y) = imgsize("${path}0"); |
---|
133 | $size = "${x}x$y" if ($x && $y); |
---|
134 | } |
---|
135 | |
---|
136 | print "amdesc::$desc\n"; |
---|
137 | printf "animatedimage::${path}%%1-$i%s\n", ($size && "-$size" || ''); |
---|
138 | print "updatetime::Last Updated on " . UnixDate("now", "%b %d, %I:%M %p %Z") . "\n"; |
---|