I'm a scuba diver and spend a lot of my weekends sat around beaches, boats etc. Given that I'm in the UK, this often translates to being sat around in the rain, howling gales, and other inclement weather. Given that my dive days start early, or involve some travelling it would be nice to get some detailed information regarding the areas in which I'm diving.

Unfortunately (or not, depending on how you want to look at it), the diving often means that I'm away from the web & means of accessing weather information that is out there - the local weather information on the radio is sporadic to say the least and often amount to little more than "it's raining" or "it'll be sunny", nowhere near enough info.

So my problem was that while the info is available, I've no way of accessing it - so I came up with this script that will obtain a reasonably good marine forecast and send it to my mobile phone as a text message. Now this in itself presents problems, since we have a limited amount of text available - something like 120 characters and that's not much when we have to include all the various bits of into. So this code is called at regular intervals via cron, goes and grabs the forecast from the UK Meteorological Office, does some processing on the text and then sends it to my phone using an email to text gateway.

BTW, this is just for personal use, so I've assumed that copyright isn't a problem

If any one has any comments, feel free to pass them on

#!/usr/bin/perl use strict; use warnings; use diagnostics; #--------------------------------------------------------------------- +---------- # Program: weather_mail # Arguments: none # Return value: undefined # #--------------------------------------------------------------------- +---------- require LWP::UserAgent; use Mail::Mailer; # # OK, so the first thing is to go and get our forecast using HTTP::Req +uest # # We get this from the British Meteorological Office on a specific pag +e # http://www.meto.gov.uk/datafiles/offshore.html # my $ua = LWP::UserAgent->new; # # OK, we need to grab this through a proxy since we're behind a firewa +ll # $ua->proxy('http', 'http://web-proxy.nn.nn.com:8088/'); # # So go and grab the page # my $request = HTTP::Request->new('GET', 'http://www.meto.gov.uk/datafi +les/offshore.html'); my $response = $ua->request($request); # # If it worked # if ($response->is_success) { $_ = $response->content; /^AT ([0-9]+).* ([0-9]+) ([A-Z]{3}).* ([0-9]{4})<BR>/mi; my $date_of_forecast = "ISSUED: $1 $2/$3/$4"; $_ = $response->content; /(.*PORTLAND.*<BR>\n)((^.+<BR>\n)+)<P>/mi; my $forecast = sprintf("%s\n%s %s\n", $date_of_forecast, $1,$2); my %swaps = (# From To "OCCASIONALLY", "OCC.", "MILES", "M.", "SHOWER", "SHWR", "RAIN", "RN", "THUNDER", "THNDR", " +", " ", "ERLY", "", "EXPECTED", "EXP", "SLOW", "SLW", "INCREASING", "INC", "DECREASING", "DEC", "NORTH", "N.", "EAST", "E.", "SOUTH", "S.", "WEST", "W.", "<BR>", "" ); foreach (keys(%swaps)) { $forecast =~ s/$_/$swaps{$_}/gi; } my $mailer = Mail::Mailer->new; $mailer->open({ From => "nn\@nn.co.uk", To => "nn\@nn.co.uk", Subject => "", }) or die "Can't open: $!\n"; print $mailer $forecast; $mailer->close(); }

Replies are listed 'Best First'.
Re: Weather to my Mobile
by jj808 (Hermit) on Oct 08, 2002 at 16:33 UTC
    If you're having problems fitting the forcast into a single text message, have a look at Lingua::EN::Squeeze. This compresses text (e.g. 'you' will automatically become 'u'), and you can also add your own words and abbreviations (as you have done with the %swaps hash).

    JJ

Re: Weather to my Mobile
by kryberg (Pilgrim) on Oct 08, 2002 at 17:32 UTC
    This is great. I've wanted to do something like this for a long time - pulling current conditions information from the US National Weather Service and posting it to a web site. I hadn't gotten around to it and your code inspired me to do it. I now have it working on the web.

    I had never used LWP::UserAgent before so this was a good little project for me. I'm glad you posted your code. Nice job of commenting it.

      Glad it was of use to someone - it's definitely saved me some early mornings sat in the rain :)

      As for the comments, I tend to be either excessive or have none at all - if it's something short I tend not to bother but once I get over 20 or so lines, they start showing up - if only so I can remember what it's supposed to do after I haven't looked at the code for 2 weeks

Re: Weather to my Mobile
by Popcorn Dave (Abbot) on Oct 08, 2002 at 15:51 UTC
    Very cool!

    I'm just curious why you're sending it as an actual e-mail rather than an SMS. Are you charged for messages or doesn't your phone support them?

    There is no emoticon for what I'm feeling now.

      Mainly because I already had the email to SMS gateway setup - but also because I couldn't find a simple SMS sending method. I considered using LWP via say the O2 webpage but decided to go for the easy option :)