File: weather_ext.pl

package info (click to toggle)
libweather-com-perl 0.5.0-2
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 340 kB
  • ctags: 148
  • sloc: perl: 3,509; makefile: 44
file content (302 lines) | stat: -rw-r--r-- 9,626 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/perl
# $Revision: 1.6 $
use strict;
use warnings;
use Weather::Com::Finder;

# autoflush
$| = 1;

# have a cvs driven version...
our $VERSION = sprintf "%d.%03d", q$Revision: 1.6 $ =~ /(\d+)/g;

# you have to fill in your ids from weather.com here
my $PartnerId  = '';
my $LicenseKey = '';

# you can preset units of messures here
# (m for metric (default), s for us)
my $units    = 'm';
my $language = 'de';

# if you need a proxy... maybe with authentication
my $Proxy      = '';
my $Proxy_User = '';
my $Proxy_Pass = '';

# debugging on/off
my $debugmode = 0;
if ( $ARGV[0] && ( $ARGV[0] eq "-d" ) ) {
	warn "Debug mode turned on.\n";
	$debugmode = 1;
}

my %weatherargs = (
					'partner_id' => $PartnerId,
					'license'    => $LicenseKey,
					'units'      => $units,
					'debug'      => $debugmode,
					'proxy'      => $Proxy,
					'proxy_user' => $Proxy_User,
					'proxy_pass' => $Proxy_Pass,
					'language'   => $language,
);

# This is a global var for dynamic language test
# it can be set from within the program like
# 'set language de_DE'
my $dyn_lang = undef;    # dynamic language, to be changed

# my weather finder instance
my $weather_finder = Weather::Com::Finder->new(%weatherargs);

# print greeting
print "\nWelcome to Uncle Tom's weather station, extended edition...\n";
print "This is V$VERSION\n";
print "\nPlease enter a location name to look for, e.g\n";
print "'Heidelberg' or 'Seattle, WA', or 'Munich, Germany'\n\n";
print "Additional commands are:\n";
print "  'end'                     to quit this program\n";
print "  'set language <lang_tag>' to dynamically change the language\n";
print "  'language'                to get the current languages tag\n";
print "  'reset language'          to change back to your default language\n\n";

# define prompt
my $prompt = '$> ';
print $prompt;

while ( chomp( my $input = <STDIN> ) ) {

	# don't want any empty input
	unless ( $input =~ /\S+/ ) {
		print $prompt;
		next;
	}

	# handle dynamic language
	if ( $input =~ /^set language (.+)$/ ) {
		$dyn_lang = $1;
		print $prompt;
		next;
	}
	if ( $input =~ /^reset language$/ ) {
		$dyn_lang = undef;
		print $prompt;
		next;
	}
	if ( $input =~ /^language$/ ) {
		print $dyn_lang || $language, "\n";
		print $prompt;
		next;
	}

	# and if the user wants to exit...
	last if ( $input =~ /^end|quit|exit$/ );

	# search for matching locations
	my $locations = $weather_finder->find($input);

	# if $weather_finder->find() returned 0, we haven't found anything...
	unless ($locations) {
		print "No weather found for location '$input'\n";
		print $prompt;
		next;
	}

	# if we found anything we'll print it out...
	print "\nFound weather data for " . @{$locations} . " locations:\n\n";

	# define all units of measure (we can use the units of the first location
	# because they should all be equal...)
	my $uodist   = $locations->[0]->units()->distance();
	my $uoprecip = $locations->[0]->units()->precipitation();
	my $uopress  = $locations->[0]->units()->pressure();
	my $uotemp   = $locations->[0]->units()->temperature();
	my $uospeed  = $locations->[0]->units()->speed();

	foreach my $location ( @{$locations} ) {

		# print a nice heading underlined by '===='
		my $heading = "This is the data for " . $location->name() . "\n";
		print $heading;
		print "=" x length($heading), "\n";

		# print location data
		print " * this city is located at: ", $location->latitude(), "deg N, ",
		  $location->longitude(), "deg E\n";
		print " * local time is ", $location->localtime()->time(), "\n";
		print " * sunrise will be/has been at ", $location->sunrise()->time(),
		  "\n";
		print " * sunset (am/pm) will be/has been at ",
		  $location->sunset()->time_ampm(), "\n";
		print " * timezone is GMT + ", $location->timezone(), "hour(s)\n";

		# current conditions
		print "\nCurrent Conditions (last update ",
		  $location->current_conditions()->last_updated()->time(), " on ",
		  $location->current_conditions()->last_updated()
		  ->formatted('dd.mm.yyyy'), "):\n";
		print " * current conditions are ",
		  $location->current_conditions()->description($dyn_lang), ".\n";
		print " * visibilty is about ",
		  $location->current_conditions()->visibility(), " $uodist.\n";
		print " * and the temperature is ",
		  $location->current_conditions()->temperature(), "deg $uotemp.\n";
		print " * the current windchill is ",
		  $location->current_conditions()->windchill(), "deg $uotemp.\n";
		print " * the humidity is ",
		  $location->current_conditions()->humidity(), "\%.\n";
		print " * the dewpoint is ",
		  $location->current_conditions()->dewpoint(), "deg $uotemp.\n";

		# all about wind
		print " * wind speed is ",
		  $location->current_conditions()->wind()->speed(), " $uospeed.\n";
		print " * wind comes from ",
		  $location->current_conditions()->wind()->direction_long($dyn_lang),
		  ".\n";
		print "   ... in short ",
		  $location->current_conditions()->wind()->direction_short($dyn_lang),
		  ".\n";
		print "   ... in degrees ",
		  $location->current_conditions()->wind()->direction_degrees(), ".\n";
		print "   ... max. gust ",
		  $location->current_conditions()->wind()->maximum_gust(),
		  " $uospeed.\n";

		# all about uv index
		print " * uv index is ",
		  $location->current_conditions()->uv_index()->index(), ".\n";
		print "   ... that is ",
		  $location->current_conditions()->uv_index()->description($dyn_lang),
		  ".\n";

		# all about barometric pressure
		print " * air pressure is ",
		  $location->current_conditions()->pressure()->pressure(),
		  " $uopress.\n";
		print "   ... tendency ",
		  $location->current_conditions()->pressure()->tendency(), ".\n";

		# moon...
		print " * moon phase is ",
		  $location->current_conditions()->moon()->description($dyn_lang), "\n";

		# forecasts
		my $forecast = $location->forecast();
		my $today    = $forecast->day(0);

		print "Today:\n";
		print "... day of week: ", $today->date()->weekday(), ", ",
		  $today->date()->date(), "\n";
		if ( $today->high() ) {
			print "... max temp:    ", $today->high(), "\n";
		}
		print "... min temp:    ", $today->low(), "\n";
		print "... sunrise:     ", $today->sunrise()->time(), "\n";
		print "... sunset:      ", $today->sunset()->time(),  "\n";

		print "Daytime data:\n";
		if ( $today->day() ) {
			print "... conditions:     ", $today->day()->conditions($dyn_lang),
			  "\n";
			print "... humidity:       ", $today->day()->humidity(),      "\n";
			print "... precipitation:  ", $today->day()->precipitation(), "\n";
			print "... wind speed:     ", $today->day()->wind()->speed(), "\n";
			print "... max gust:       ", $today->day()->wind()->maximum_gust(),
			  "\n";
			print "... wind dir: ",
			  $today->day()->wind()->direction_long($dyn_lang), "\n";
			print "... wind dir: ", $today->day()->wind()->direction_degrees(),
			  "\n";
		}
		print "Nightly data:\n";
		print "... conditions:     ", $today->night()->conditions($dyn_lang),
		  "\n";
		print "... humidity:       ", $today->night()->humidity(),      "\n";
		print "... precipitation:  ", $today->night()->precipitation(), "\n";
		print "... wind speed:     ", $today->night()->wind()->speed(), "\n";
		print "... max gust:       ", $today->night()->wind()->maximum_gust(),
		  "\n";
		print "... wind dir: ",
		  $today->night()->wind()->direction_long($dyn_lang), "\n";
		print "... wind dir: ", $today->night()->wind()->direction_degrees(),
		  "\n";

		foreach my $day ( $forecast->all() ) {
			print "Have forecast for ", $day->date()->weekday(), ", ",
			  $day->date()->date(), "\n";
			print "Max Temp is ", $day->high(), "\n";
			print "Min Temp is ", $day->low(),  "\n";
			print "Percent chance of precipitation at night: ",
			  $day->night()->precipitation(), "\%\n";
			print "\n";
		}

		print "\n";
	}

	# last but not least print the next prompt
	print $prompt;
}

__END__

=pod

=head1 NAME

weather_ext.pl - Sample script to show the usage of the OO API of I<Weather::Com>

=head1 SYNOPSIS

  #> ./weather_ext.pl [-d]
  
  Welcome to Uncle Tom's weather station, extended edition...
  This is V1.006

  Please enter a location name to look for, e.g
  'Heidelberg' or 'Seattle, WA', or 'Munich, Germany'

  Additional commands are:
    'end'                     to quit this program
    'set language <lang_tag>' to dynamically change the language
    'language'                to get the current languages tag
    'reset language'          to change back to your default language
  
  $>

=head1 DESCRIPTION

**IMPORTANT** You first have to register at I<weather.com> to get a
partner id and a license key for free. Please visit their web site
L<http://www.weather.com/services/xmloap.html>. Then edit this script
and fill in the data into the corresponding variables at the top of 
the script.

The sample script I<weather.pl> asks you for a location name - either 
a city or a 'city, region' or 'city, country' combination. It then uses 
the I<Weather::Com> OO API to get location information, current weather
conditions and 9 days of weahter forecast for this location(s).

If no location matching your input is found, a "no locations found" 
message is printed out.

Else, the number of locations found is printed followed by nicely
formatted weather data for each location.

The command line parameter '-d' enables debugging mode (which is
enabling debugging within all used packages.

=head1 AUTHOR

Thomas Schnuecker, E<lt>thomas@schnuecker.deE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2004-2005 by Thomas Schnuecker

This script is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut