Ticket #3337: weathercomXML.pl

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

Perl script to pull XML data from weather.com

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.1;
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    } 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}