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.

  1. #!/usr/bin/perl  
  2.   
  3. # $Id: hpsetdisp.pl 11 2006-03-22 01:21:03Z yaakov $  
  4.   
  5. # hpsetdisp.pl  
  6. # Connects to a JetDirect equipped HP printer and uses  
  7. # HP's control language to set the ready message on the  
  8. # LCD display.  Takes an IP address and message on the  
  9. # command line. My favorite message is "INSERT COIN".  
  10. # Keep in mind the limitations of the display when composing  
  11. # your clever verbiage.  
  12. #  
  13. # THIS PROGRAM IS PROVIDED WITH NO WARRANTY OF ANY KIND EXPRESSED OR IMPLIED  
  14. # THE AUTHOR CANNOT BE RESPONSIBLE FOR THE EFFECTS OF THIS PROGRAM  
  15. # IF YOU ARE UNCERTAIN ABOUT THE ADVISABILITY OF USING IT, DO NOT!  
  16. #  
  17. # Yaakov (http://kovaya.com/ )  
  18. #  
  19. # Modified by Garrett Hoofman  
  20. # 10/18/2007  
  21. # http://www.visionsofafar.com  
  22.   
  23. # Modified by David Jacoby  
  24. # 12/10/2008  
  25. # http://varlogrant.blogspot.com/  
  26.   
  27. # Modified by David Jacoby, using notify-send and tweaking Unicode  
  28. # 09/08/2008  
  29. # http://varlogrant.blogspot.com/  
  30.   
  31. use strict ;  
  32. use warnings ;  
  33. use 5.010 ;  
  34. use Getopt::Long ;  
  35. use IO::Interactive qw{ interactive } ;  
  36. use IO::Socket ;  
  37. use XML::DOM ;  
  38. use Data::Dumper ;  
  39. use subs qw{  get_weather notify } ;  
  40.   
  41. my $zip         = '' ;  
  42. my $forc        = 'F' ;  
  43. my $interactive = '' ;  
  44. my $degree = '°' ; #the unicode, straight  
  45.    $degree = "\x{00b0}" ; #the unicode, specified  
  46. my $rdymsg      = "Ready" ;  
  47.   
  48. GetOptions(  
  49.     'zipcode=s'   => \$zip ,  
  50.     'forc=s'      => \$forc ,  
  51.     ) ;  
  52.   
  53. if ( $forc eq 'c' || $forc eq 'C' ) { $forc = 'C' }  
  54. else { $forc = 'F' }  
  55.   
  56. $zip !~ /\d/ and exit 0 ;  
  57. my $weather = get_weather ;  
  58. notify $weather ;  
  59.   
  60. exit ;  
  61.   
  62. ########## ########## ########## ########## ########## ########## ##########  
  63. sub get_weather {  
  64.     my $rdymsg ;  
  65.     my $parser = XML::DOM::Parser->new() ;  
  66.     my $file   = 'http://www.google.com/ig/api?weather=XXXXX&hl=en' ;  
  67.     #my $zip    = shift ;  
  68.     $zip =~ s{(\d{5})}{}mx ;  
  69.     $zip = $1 ;  
  70.     $file =~ s/XXXXX/$zip/mx ;  
  71.     my $doc = $parser->parsefile( $file ) ;  
  72.     my $city =  
  73.         $doc->getElementsByTagName( 'city' )->item( 0 )->getAttribute('data') ;  
  74.     my $curr_temp ;  
  75.     if ( $forc eq 'C' ) {  
  76.         $curr_temp =  
  77.         $doc->getElementsByTagName( 'temp_c' )->item( 0 )->getAttribute('data') ;  
  78.         }  
  79.     else {  
  80.         $curr_temp =  
  81.         $doc->getElementsByTagName( 'temp_f' )->item( 0 )->getAttribute('data') ;  
  82.         }  
  83.     my $curr_cond =  
  84.         $doc->getElementsByTagName( 'condition' )->item( 0 )->getAttribute('data') ;  
  85.   
  86.     my @output ;  
  87.     push @output , qq{Conditions for $city} ;  
  88.     push @output , qq$curr_temp$degree $forc$curr_cond  } ;  
  89.     return join "\n" , @output ;  
  90.     }  
  91. ########## ########## ########## ########## ########## ########## ##########  
  92.   
  93. ########## ########## ########## ########## ########## ########## ##########  
  94. sub notify {  
  95.     my $notify  = '/usr/bin/notify-send' ;  
  96.     my $weather = shift ;  
  97.     say qq$notify $weather } ;  
  98.     $weather = join q{'} , '' , $weather , '' ;  
  99.     qx$notify $weather } ;  
  100.     return ;  
  101.     }  
  102. ########## ########## ########## ########## ########## ########## ##########  

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete