Ticket #3337: weathercomXML.2.pl

File weathercomXML.2.pl, 4.5 KB (added by simonflood, 13 years ago)

Perl script to pull XML data from weather.com (v0.2)

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