1 | #!/usr/bin/perl -w
|
---|
2 |
|
---|
3 | use strict;
|
---|
4 |
|
---|
5 | use Data::Dumper;
|
---|
6 | use Getopt::Std;
|
---|
7 | use LWP::Simple;
|
---|
8 | use XML::Simple;
|
---|
9 |
|
---|
10 | our ($opt_d, $opt_l, $opt_t, $opt_T, $opt_u, $opt_v);
|
---|
11 |
|
---|
12 | my $name = 'weathercomXML';
|
---|
13 | my $version = 0.1;
|
---|
14 | my $author = 'Simon Flood';
|
---|
15 | my $email = 'simon@floodfamily.info';
|
---|
16 | my $updateTimeout = 15*60;
|
---|
17 | my $retrieveTimeout = 30;
|
---|
18 | my @types = ('3dlocation', '6dlocation', 'appt', 'cclocation', 'date-0',
|
---|
19 | 'date-1', 'date-2', 'date-3', 'date-4', 'date-5', 'desc1',
|
---|
20 | 'desc2', 'desc3', 'dewpoint', 'high-0', 'high-1', 'high-2',
|
---|
21 | 'high-3', 'high-4', 'high-5', 'icon-0', 'icon-1', 'icon-2',
|
---|
22 | 'icon-3', 'icon-4', 'icon-5', 'latitude', 'longitude', 'low-0',
|
---|
23 | 'low-1', 'low-2', 'low-3', 'low-4', 'low-5', 'observation_time',
|
---|
24 | 'pressure', 'relative_humidity', 'station_id', 'temp',
|
---|
25 | 'updatetime', 'visibility', 'weather', 'weather_icon', 'wind_dir',
|
---|
26 | 'wind_gust', 'wind_speed', 'wind_spdgst');
|
---|
27 | my $dir = './';
|
---|
28 | my $unit;
|
---|
29 |
|
---|
30 | my $base_url = "http://xoap.weather.com/";
|
---|
31 | my $weather_url = $base_url . "weather/local/";
|
---|
32 | my $query_url = $base_url . "search/search";
|
---|
33 |
|
---|
34 | getopts('d:ltTu:v');
|
---|
35 |
|
---|
36 | if (defined $opt_d) {
|
---|
37 | $dir = $opt_d;
|
---|
38 | }
|
---|
39 |
|
---|
40 | if (defined $opt_l) {
|
---|
41 | my $response = get $query_url . "?where=" . $ARGV[0];
|
---|
42 | my $xml = XMLin($response);
|
---|
43 | foreach my $id (sort keys %{$xml->{loc}}) {
|
---|
44 | print $id, "::", $xml->{loc}->{$id}->{content}, "\n";
|
---|
45 | }
|
---|
46 | exit 0;
|
---|
47 | }
|
---|
48 |
|
---|
49 | if (defined $opt_t) {
|
---|
50 | foreach (@types) {
|
---|
51 | print; print "\n";
|
---|
52 | }
|
---|
53 | exit 0;
|
---|
54 | }
|
---|
55 |
|
---|
56 | if (defined $opt_T) {
|
---|
57 | print "$updateTimeout,$retrieveTimeout\n";
|
---|
58 | exit 0;
|
---|
59 | }
|
---|
60 |
|
---|
61 | if (defined $opt_u) {
|
---|
62 | if ($opt_u =~ /ENG/) {
|
---|
63 | $unit = "s";
|
---|
64 | } elsif ($opt_u =~ /SI/) {
|
---|
65 | $unit = "m";
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | if (defined $opt_v) {
|
---|
70 | print "$name,$version,$author,$email\n";
|
---|
71 | exit 0;
|
---|
72 | }
|
---|
73 |
|
---|
74 | my $loc = shift;
|
---|
75 |
|
---|
76 | if (!(defined $unit && defined $loc && !$loc eq '')) {
|
---|
77 | die "Invalid usage";
|
---|
78 | }
|
---|
79 |
|
---|
80 | my $response = get $weather_url . $loc . '?cc=*&dayf=6&unit=' . $unit;
|
---|
81 | die unless defined $response;
|
---|
82 |
|
---|
83 | my $xml = XMLin($response);
|
---|
84 |
|
---|
85 | foreach (@types) {
|
---|
86 | my $label;
|
---|
87 | my $value;
|
---|
88 |
|
---|
89 | $label = $_;
|
---|
90 |
|
---|
91 | if (/[36]dlocation/) {
|
---|
92 | $value = $xml->{loc}->{dnam};
|
---|
93 | } elsif (/appt/) {
|
---|
94 | $value = $xml->{cc}->{flik} . chr(176) . $xml->{head}->{ut};
|
---|
95 | } elsif (/cclocation/) {
|
---|
96 | $value = $xml->{cc}->{obst};
|
---|
97 | } elsif (/date-([0-5])/) {
|
---|
98 | $value = $xml->{dayf}->{day}->[$1]->{t};
|
---|
99 | } elsif (/desc([123])/) {
|
---|
100 | $value = $xml->{dayf}->{day}->[$1-1]->{part}->[0]->{t};
|
---|
101 | } elsif (/dewpoint/) {
|
---|
102 | $value = $xml->{cc}->{dewp} . chr(176) . $xml->{head}->{ut};
|
---|
103 | } elsif (/high-([0-5])/) {
|
---|
104 | $value = $xml->{dayf}->{day}->[$1]->{hi};
|
---|
105 | } elsif (/icon-([0-5])/) {
|
---|
106 | $value = $xml->{dayf}->{day}->[$1]->{part}->[0]->{icon} . '.png';
|
---|
107 | } elsif (/latitude/) {
|
---|
108 | $value = $xml->{loc}->{lat};
|
---|
109 | } elsif (/longitude/) {
|
---|
110 | $value = $xml->{loc}->{lon};
|
---|
111 | } elsif (/low-([0-5])/) {
|
---|
112 | $value = $xml->{dayf}->{day}->[$1]->{low};
|
---|
113 | } elsif (/observation_time/) {
|
---|
114 | $value = $xml->{cc}->{lsup};
|
---|
115 | } elsif (/pressure/) {
|
---|
116 | $value = $xml->{cc}->{bar}->{r} . ' ' . $xml->{head}->{up};
|
---|
117 | } elsif (/station_id/) {
|
---|
118 | $value = $loc;
|
---|
119 | } elsif (/temp/) {
|
---|
120 | $value = $xml->{cc}->{tmp} . chr(176) . $xml->{head}->{ut};
|
---|
121 | } elsif (/relative_humidity/) {
|
---|
122 | $value = $xml->{cc}->{hmid} . ' %';
|
---|
123 | } elsif (/updatetime/) {
|
---|
124 | $value = $xml->{dayf}->{lsup};
|
---|
125 | } elsif (/visibility/) {
|
---|
126 | $value = $xml->{cc}->{vis} . ' ' . $xml->{head}->{ud};
|
---|
127 | } elsif (/wind_dir/) {
|
---|
128 | $value = $xml->{cc}->{wind}->{t};
|
---|
129 | } elsif (/weather$/) {
|
---|
130 | $value = $xml->{cc}->{t};
|
---|
131 | } elsif (/weather_icon/) {
|
---|
132 | $value = $xml->{cc}->{icon} . '.png';
|
---|
133 | } elsif (/wind_gust/) {
|
---|
134 | $value = $xml->{cc}->{wind}->{gust};
|
---|
135 | if ($value ne 'N/A') {
|
---|
136 | $value .= ' ' . $xml->{head}->{us};
|
---|
137 | }
|
---|
138 | } elsif (/wind_spdgst/) {
|
---|
139 | $value = "$xml->{cc}->{wind}->{s} ($xml->{cc}->{wind}->{gust}) $xml->{head}->{us}";
|
---|
140 | } elsif (/wind_speed/) {
|
---|
141 | $value = $xml->{cc}->{wind}->{s} . ' ' . $xml->{head}->{us};
|
---|
142 | }
|
---|
143 |
|
---|
144 | print $label . "::" . $value . "\n";
|
---|
145 | } |
---|