summaryrefslogtreecommitdiff
path: root/bin/vweather
blob: 0c7f337e97a7a8b242ce4d665d916de4b9c4a50f (plain)
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
#!/usr/bin/env perl
## Copyright © 2011 by Daniel Friesel <derf@finalrewind.org>
## License: WTFPL:
##   0. You just DO WHAT THE FUCK YOU WANT TO.
use strict;
use warnings;
use 5.010;
use autodie;

use GD;
use Getopt::Std;
use Term::ANSIColor;
use Weather::Google;

my %opt;
my $alpha;
my $dump;
my $share = $0;

my $weather = Weather::Google->new(
	'Essen, Germany',
	{
		language => 'de',
	},
);

my $forecasts = $weather->forecast_conditions();


sub show_weather_console {
	for my $day(@{$forecasts}) {
		printf(
			"%-6s %s%3d°c%s %s%3d°c%s %s\n",
			$day->{'day_of_week'},
			color('blue'),
			$day->{'low'},
			color('reset'),
			color('red'),
			$day->{'high'},
			color('reset'),
			$day->{'condition'},
		);

		if ($dump) {
			say $day->{'icon'};
		}
	}
}

sub show_weather_png {
	my ($w, $h) = (200, 32);
	my $im = GD::Image->new($w, $h);
	my $line_height = 16;
	my $offset_wday = 5;
	my $offset_icon = $offset_wday + 20;
	my $offset_low  = $offset_icon + 22;
	my $offset_high = $offset_low + 2*7 + 4;

	my $white  = $im->colorAllocateAlpha(255, 255, 255, $alpha);
	my $blue   = $im->colorAllocateAlpha(  0,   0, 200, $alpha);
	my $red    = $im->colorAllocateAlpha(200,   0,   0, $alpha);
	my $gray   = $im->colorAllocateAlpha(127, 127, 127, $alpha);
	my $black  = $im->colorAllocateAlpha(  0,   0,   0, $alpha);

	$im->filledRectangle(1, 1, $w - 2, $h - 2, $white);

	for my $i (0..3) {
		my $y_offset = ($i < 2 ? $i : ($i % 2)) * $line_height;
		my $x_offset = ($i < 2 ? 0  : ($w/2 + 10));
		my $day = $forecasts->[$i];

		my $wday = substr($day->{'day_of_week'}, 0, 2);
		my $low  = sprintf('%2d', $day->{'low'});
		my $high = sprintf('%2d', $day->{'high'});
		my ($icon) = ($day->{'icon'} =~ m{ / ([^/]+) \. gif $ }x);

		if (not defined $day) {
			last;
		}

		$im->string(gdMediumBoldFont, $x_offset +  $offset_wday, $y_offset,
			$wday , $black);
		$im->string(gdMediumBoldFont, $x_offset + $offset_low, $y_offset,
			$low  , $blue);
		$im->string(gdMediumBoldFont, $x_offset + $offset_high, $y_offset,
			$high , $red);

		if ($dump) {
			say "${share}/weather/${icon}";
		}

		if (-e "${share}/weather/${icon}") {
			my $tmp = GD::Image->newFromPng("${share}/weather/${icon}", 1);
			$im->copy($tmp, $x_offset + $offset_icon, $y_offset, 0, 0, 16, 16);
		}
	}

	$im->rectangle(0, 0, $w -1, $h -1, $gray);

	open(my $out_fh, '>', '/tmp/vweather.png');
	binmode $out_fh;
	print $out_fh $im->png();
	close($out_fh);
}

getopts('a:cdfg', \%opt);
$alpha = $opt{'a'} // 40;
$dump = $opt{'d'} // 0;

if (-l $share) {
	my $link = readlink($share);
	$share =~ s{ [^/]+ $ }{}x;
	$link  =~ s{ / bin / vweather $ }{}x;
	$share .= "${link}/share";
}
else {
	$share =~ s{ / bin / vweather $ }{}x;
	$share .= '/share';
}

if ($opt{'c'}) {
	show_weather_console();
}
elsif ($opt{'g'}) {
	show_weather_png();
}

if ($opt{'f'}) {
	exec('feh', '/tmp/vweather.png');
}

__END__

=head1 NAME

=head1 SYNOPSIS

=head1 DESCRIPTION

=head1 OPTIONS

=head1 EXIT STATUS

=head1 CONFIGURATION

=head1 DEPENDENCIES

=head1 BUGS AND LIMITATIONS

=head1 AUTHOR

Copyright (C) 2011 by Daniel Friesel E<lt>derf@finalrewind.orgE<gt>

=head1 LICENSE

  0. You just DO WHAT THE FUCK YOU WANT TO.