From 48126f0c53dc3ecb3519888059ce09a7e5534151 Mon Sep 17 00:00:00 2001 From: Matthew Barksdale Date: Tue, 1 Feb 2011 23:00:54 -0500 Subject: [PATCH] Added a Weather module. --- doc/CHANGELOG | 1 + doc/TODO | 2 +- modules/Weather.pm | 134 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 modules/Weather.pm diff --git a/doc/CHANGELOG b/doc/CHANGELOG index bab8093..abe673f 100644 --- a/doc/CHANGELOG +++ b/doc/CHANGELOG @@ -3,6 +3,7 @@ Auto IRC Bot 3.0: Change Log 3.0 Indev ================================================================== + * Added Weather module. * Added Calc module. * Fixed the NAME,VER,SVER,REV,RSTAGE constants. * Added a CTCP VERSION reply. diff --git a/doc/TODO b/doc/TODO index 1c74e95..662c57c 100644 --- a/doc/TODO +++ b/doc/TODO @@ -36,7 +36,7 @@ Legend: [ ] Create system file for Microsoft Windows (currently unsupported) [!] Features - [ ] Weather module + [X] Weather module [X] Google Calculator module [ ] YouTube Search module [ ] Twitter module diff --git a/modules/Weather.pm b/modules/Weather.pm new file mode 100644 index 0000000..83c9a09 --- /dev/null +++ b/modules/Weather.pm @@ -0,0 +1,134 @@ +# Auto IRC Bot. An advanced, lightweight and powerful IRC bot. +# Copyright (C) 2010-2011 Xelhua Development Team (doc/CREDITS) +# This program is free software; rights to this code are stated in doc/LICENSE. +package m_Weather; +use strict; +use warnings; +use API::Std qw(cmd_add cmd_del trans); +use API::IRC qw(privmsg notice); +use LWP::UserAgent; +use XML::Simple; + +# Initialization subroutine. +sub _init +{ + # Create the Weather command. + cmd_add("WEATHER", 0, \%m_Weather::SHELP_Weather, \%m_Weather::FHELP_WEATHER, \&m_Weather::weather) or return 0; + + # Success. + return 1; +} + +# Void subroutine. +sub _void +{ + # Delete the Weather command. + cmd_del("WEATHER") or return 0; + + # Success. + return 1; +} + +# Help hashes. +our %SHELP_Weather = ( + 'en' => "Retrieve the weather.", + ); + +our %FHELP_Weather = ( + 'en' => "This command will retrieve the weather via Wunderground for the specified location. Syntax: WEATHER ", + ); + +# Callback for Weather command. +sub weather +{ + my (%data) = @_; + + # Create an instance of LWP::UserAgent. + my $ua = LWP::UserAgent->new(); + $ua->agent('Auto IRC Bot'); + $ua->timeout(2); + # Put together the call to the Wunderground API. + my @args = @{ $data{args} }; + if (!defined $args[0]) { + notice($data{svr}, $data{nick}, trans("Not enough parameters")."."); + return 0; + } + my $loc = join(' ', @args); + $loc =~ s/ /%20/g; + my $url = "http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=".$loc; + # Get the response via HTTP. + my $response = $ua->get($url); + + if ($response->is_success) { + # If successful, decode the content. + my $d = XMLin($response->decoded_content); + # And send to channel + if (!ref($d->{observation_location}->{country})) { + my $windc = $d->{wind_string}; + if (substr($windc, length($windc) - 1, 1) eq " ") { $windc = substr($windc, 0, length($windc) - 1); } + privmsg($data{svr}, $data{chan}, "Results for \2".$d->{observation_location}->{full}."\2 - \2Temperature:\2 ".$d->{temperature_string}." \2Wind Conditions:\2 ".$windc." \2Conditions:\2 ".$d->{weather}); + privmsg($data{svr}, $data{chan}, "\2Heat index:\2 ".$d->{heat_index_string}." \2Humidity:\2 ".$d->{relative_humidity}." \2Pressure:\2 ".$d->{pressure_string}." - ".$d->{observation_time}); + } + else { + # Otherwise, send an error message. + privmsg($data{svr}, $data{chan}, "Location not found."); + } + } + else { + # Otherwise, send an error message. + privmsg($data{svr}, $data{chan}, "An error occurred while retrieving your weather."); + } + + return 1; +} + +# Start initialization. +API::Std::mod_init("Weather", "Xelhua", "1.00", "3.0d", __PACKAGE__); + +__END__ + +=head1 Weather + +=head2 Description + +=over + +This module adds the WEATHER command for retrieving +the current weather. + +=back + +=head2 How To Use + +=over + +Add Weather to the module autoload. + +=back + +=head2 Examples + +=over + + !weather 10111 + Results for Central Park, New York - Temperature: 27 F (-3 C) Wind Conditions: From the NE at 9 MPH Gusting to 22 MPH Conditions: Overcast + Heat index: NA Humidity: 89% Pressure: 30.22 in (1023 mb) - Last Updated on February 1, 9:51 PM EST + +=back + +=head2 To Do + +=over + +* Add Spanish, French and German translations for the help hashes. + +=back + +=head2 Technical + +=over + +This module requires LWP::UserAgent and XML::Simple both obtainable from CPAN (http://cpan.org) +This module is compatible with Auto version 3.0a2+. + +=back