Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Google Weather for dwm

by xyzzy (Pilgrim)
on May 24, 2011 at 21:49 UTC ( #906578=CUFP: print w/replies, xml ) Need Help??

♥ perl

if your terminal does not support unicode (I just discovered that PM code blocks do not...), consider changing the i subroutine to be something more visible

#!/usr/bin/perl -w use strict; use encoding 'utf8'; use Weather::Google; use Math::Units qw/convert/; my $LOCATION = "Nowhere"; # set to city name or postal code my $gw = new Weather::Google($LOCATION); # single-character icons for weather conditions # returns one string with one icon per argument sub i { my $i=''; $i .= /sun/i ? chr 0x2600 : /cloud/i ? chr 0x2601 : /rain|shower/i ? chr 0x2602 : /snow/i ? '*': $_ foreach @_; # the above regexes probably don't cover ALL conditions # but they do cover the ones I have seen over the past few days return $i } # format a temperature # originally the second argument was $f but I found that # I had to convert the temperature much more than I had to # not convert the temperature. If you are one of those types # that thinks in non-SI units, you might want to change that. sub t { my ($t, $c) = @_; return int($c ? $t : convert($t,'F','C')) . chr 0x00b0; } print '[', t($gw->temp_c,1), '(',t($gw->forecast(0,'high')), '/', t($gw->forecast(0,'low')), ')', i($gw->forecast(0,'condition')), '|', i($gw->forecast(1,'condition')), t($gw->forecast(1,'high')), '/', t($gw->forecast(1,'low')), ']';

The above script produces something like [17(18/5)|23/11] A newline is omitted because the script's output is combined with other scripts' outputs to create a single string of statuses and notifications. If you want to run it alone on the command line, I suggest adding ;echo to the end of your invocation.

Thanks to Daniel LeWarne for the module!

$,=qq.\n.;print q.\/\/____\/.,q./\ \ / / \\.,q.    /_/__.,q..
Happy, sober, smart: pick two.

Replies are listed 'Best First'.
Re: Google Weather for dwm
by ww (Archbishop) on May 25, 2011 at 02:17 UTC
    Nice idea, and nearly there... I think.

    However, executing the script (Win32) with the default codepage for US yields:

    Malformed UTF-8 character (unexpected continuation byte 0xb5, with no +preceding start byte) at (eval 27) line 7. Malformed UTF-8 character (unexpected continuation byte 0xb5, with no +preceding start byte) at (eval 27) line 19. Malformed UTF-8 character (unexpected continuation byte 0xb5, with no +preceding start byte) at (eval 27) line 34. Malformed UTF-8 character (unexpected continuation byte 0xb5, with no +preceding start byte) at (eval 27) line 34. [19┬░(25┬░/13┬░)Scattered Thunders +torms|Γ26┬░/13┬░]

    Switching codepages helps:

    >mode con cp select=65001 Status for device CON: ---------------------- Lines: 600 Columns: 220 Keyboard rate: 31 Keyboard delay: 1 Code page: 65001 > # Identical four "Malformed UTF-8 message [18(25/13)Scattered Thunderstorms|&#9729;26/13] /13] <p>Certainly an improvement... and given my (wilful) iggerance of code +pages and charsets, perhaps one (user) step from being as desired. Ho +wever, <c> ;echo on
    or off has no apparent effect. Nonetheless, interesing project!

      I get same malformed character warning (Linux), presumably due to global nature of use encoding. Removing the use encoding line and instead adding binmode( STDOUT, ":encoding(UTF-8)" ); works for me.

      Good Day,

        Ah, perfect. I never played around with unicode before but then I discovered that it has the skull and crossbones and the biohazard symbol and chess pieces and i ching and all sorts of other fun stuff.

        ww, echo under Unix prints to standard out (in this case to add an extra newline)

        $,=qq.\n.;print q.\/\/____\/.,q./\ \ / / \\.,q.    /_/__.,q..
        Happy, sober, smart: pick two.
Re: Google Weather for dwm
by duelafn (Vicar) on May 25, 2011 at 13:45 UTC

      Thanks for the list, very useful. I'll have to dig around through the unicode pages. I think I should also expand the icon to two chars to allow things like *? (chance of snow) or ~☁ ((partly|mostly) cloudy). Or perhaps == for "Overcast"?

      I was considering adding different output for different times of day: in the morning show today's forecasted high and conditions, in the evening show the forecasted low (for that night) and the conditions for the next day. It's too bad Google doesn't provide hourly forecasts.

      $,=qq.\n.;print q.\/\/____\/.,q./\ \ / / \\.,q.    /_/__.,q..
      Happy, sober, smart: pick two.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://906578]
Approved by luis.roca
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2017-11-25 08:20 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (355 votes). Check out past polls.