Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2009/09/08

Flawed Perl

Take my Weather Notification on the Printer script and add the notify-send program which connects to Gnome's OSD setup and you can get some pretty cool stuff.

Except, it seems, something doesn't like Unicode.



At first, I thought the problem was on notify-send, but 1) remembering what had gone on with my twitter weather script (@PurdueWeather) and 2) testing with direct use of Unicode made me decide that it's Perl's fault.

And there are many solutions.

It strikes me that there should be something along the lines of use Unicode ; that, with one line, tells Perl you're using Unicode strings. I'm sure there's a Unicode module, and I'm reasonably sure that's not what it does.

Anyway, code for using notify-send and the Google Weather API to tell you what's going on with the atmosphere is under the cut. For my next trick, I will add images.


#!/usr/bin/perl

# $Id: hpsetdisp.pl 11 2006-03-22 01:21:03Z yaakov $

# hpsetdisp.pl
# Connects to a JetDirect equipped HP printer and uses
# HP's control language to set the ready message on the
# LCD display. Takes an IP address and message on the
# command line. My favorite message is "INSERT COIN".
# Keep in mind the limitations of the display when composing
# your clever verbiage.
#
# THIS PROGRAM IS PROVIDED WITH NO WARRANTY OF ANY KIND EXPRESSED OR IMPLIED
# THE AUTHOR CANNOT BE RESPONSIBLE FOR THE EFFECTS OF THIS PROGRAM
# IF YOU ARE UNCERTAIN ABOUT THE ADVISABILITY OF USING IT, DO NOT!
#
# Yaakov (http://kovaya.com/ )
#
# Modified by Garrett Hoofman
# 10/18/2007
# http://www.visionsofafar.com

# Modified by David Jacoby
# 12/10/2008
# http://varlogrant.blogspot.com/

# Modified by David Jacoby, using notify-send and tweaking Unicode
# 09/08/2008
# http://varlogrant.blogspot.com/

use strict ;
use warnings ;
use 5.010 ;
use Getopt::Long ;
use IO::Interactive qw{ interactive } ;
use IO::Socket ;
use XML::DOM ;
use Data::Dumper ;
use subs qw{ get_weather notify } ;

my $zip = '' ;
my $forc = 'F' ;
my $interactive = '' ;
my $degree = '°' ; #the unicode, straight
$degree = "\x{00b0}" ; #the unicode, specified
my $rdymsg = "Ready" ;

GetOptions(
'zipcode=s' => \$zip ,
'forc=s' => \$forc ,
) ;

if ( $forc eq 'c' || $forc eq 'C' ) { $forc = 'C' }
else { $forc = 'F' }

$zip !~ /\d/ and exit 0 ;
my $weather = get_weather ;
notify $weather ;

exit ;

########## ########## ########## ########## ########## ########## ##########
sub get_weather {
my $rdymsg ;
my $parser = XML::DOM::Parser->new() ;
my $file = 'http://www.google.com/ig/api?weather=XXXXX&hl=en' ;
#my $zip = shift ;
$zip =~ s{(\d{5})}{}mx ;
$zip = $1 ;
$file =~ s/XXXXX/$zip/mx ;
my $doc = $parser->parsefile( $file ) ;
my $city =
$doc->getElementsByTagName( 'city' )->item( 0 )->getAttribute('data') ;
my $curr_temp ;
if ( $forc eq 'C' ) {
$curr_temp =
$doc->getElementsByTagName( 'temp_c' )->item( 0 )->getAttribute('data') ;
}
else {
$curr_temp =
$doc->getElementsByTagName( 'temp_f' )->item( 0 )->getAttribute('data') ;
}
my $curr_cond =
$doc->getElementsByTagName( 'condition' )->item( 0 )->getAttribute('data') ;

my @output ;
push @output , qq{Conditions for $city} ;
push @output , qq{ $curr_temp$degree $forc, $curr_cond } ;
return join "\n" , @output ;
}
########## ########## ########## ########## ########## ########## ##########

########## ########## ########## ########## ########## ########## ##########
sub notify {
my $notify = '/usr/bin/notify-send' ;
my $weather = shift ;
say qq{ $notify $weather } ;
$weather = join q{'} , '' , $weather , '' ;
qx{ $notify $weather } ;
return ;
}
########## ########## ########## ########## ########## ########## ##########