-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.pl
107 lines (90 loc) · 2.62 KB
/
weather.pl
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
#use modules
use LWP::Simple;
my $debug = 0;
#global variables that we need
#-----------------------------
#where to get the weather from - Yahoo!
%locations = (
basel2 => "http://weather.yahoo.com/switzerland/canton-of-basel-city/basel-city-781739/",
basel => "http://weather.yahoo.com/forecast/SZXX0004_c.html?force_units=1",
redhill => "http://weather.yahoo.com/forecast/UKXX0606.html",
malaga => "http://weather.yahoo.com/forecast/SPXX0052.html",
colombo => "http://weather.yahoo.com/forecast/CEXX0001.html",
geneva => "http://weather.travel.yahoo.com/forecast/SZXX0013.html",
zermatt => "http://weather.travel.yahoo.com/forecast/SZXX0042.html",
berlin => "http://weather.travel.yahoo.com/forecast/GMXX0007.html"
);
#list of what we want - by default, gimme all
my @places = ();
foreach ( keys(%locations) ) {
push ( @places, $_ );
}
# does the user want some custom behaviour?
if ( $ARGV[0] ) {
@places = ();
while ( $ARGV[0] ) {
push ( @places, $ARGV[0] );
shift;
}
}
my $t = localtime;
print "Weather Report: $t\n\n";
foreach (@places) {
getWeather($_);
}
# end of program
sub getWeather() {
my ($place) = @_;
my @fc = ();
my $found = 0;
#retrieve the weather from URL
$content1 = get( $locations{$place} );
@content = split ( /\n/, $content1 );
foreach $line (@content) {
chomp($line);
print "Parsing line $line\n" if ($debug);
if ( $line =~ /ENDTEXT FORECAST/ ) {
$found = 0;
}
if ( $found && $line =~ /^<b>/ ) {
$line =~ s/<b>//;
$line =~ s/<\/b>//;
$line =~ s/<p>//;
print "Checking line $line\n" if ($debug);
if ( $line =~ /^To/ ) { # To{day,night,morrow}
print "Found line $line\n" if ($debug);
push ( @fc, $line );
}
}
if ( $line =~ /TEXT FORECAST/ ) {
$found = 1;
}
}
$underline = $place;
$underline =~ s/[a-z]/-/ig;
print "\u$place\n";
print( $underline, "\n" );
foreach $fcl (@fc) {
$fcl = &convert_units($fcl);
print "$fcl\n";
}
print "\n";
}
sub convert_units() {
my ($str) = @_;
#convert to celsius
$str =~ /(\d+)F/;
$num = ( 5 / 9 ) * ( $1 - 32.0 );
$num = int($num);
$str =~ s/(\d+)F/$num C/g;
#conver the wind speeeds to KM/h - to
if ( $str =~ /(\d+) to/ ) {
$num = $1 * 1.6;
$str =~ s/(\d+) to/$num to/;
}
#conver the wind speeeds to KM/h - kph
$str =~ /(\d+) mph/;
$num = $1 * 1.6;
$str =~ s/(\d+) mph/$num kph/;
$str;
}