# -*- Mode: perl; tab-width: 4; indent-tabs-mode: nil; -*- ################################ # Stocks Module # ################################ package BotModules::Stocks; use vars qw(@ISA); @ISA = qw(BotModules); 1; # XXX Per-channel configurable notification of stock changes # XXX Non-US markets sub Help { my $self = shift; my ($event) = @_; return { '' => 'This module gets stock quotes. Ask me a ticker symbol, I will retrieve the quote.', 'stock' => 'Call this command with a ticker symbol to get the current stock price and change. Syntax: stock FBAR', }; } sub Told { my $self = shift; my ($event, $message) = @_; if ($message =~ /^\s*stocks?\s+(.+?)\s*$/osi) { $self->getURI($event, "http://download.finance.yahoo.com/d/quotes.csv?f=sl1d1t1c1ohgv&e=.csv&s=$1", $1); } else { return $self->SUPER::Told(@_); } return 0; # we've dealt with it, no need to do anything else. } sub GotURI { my $self = shift; my ($event, $uri, $output, $stock) = @_; $self->debug($output); my $message = "$event->{'from'}: "; # The data currently listed in this format are: ticker symbol, last price, date, time, change, open price, daily high, daily low, and volume. # -- http://help.yahoo.com/help/us/fin/quote/quote-05.html my @stockValues = split(',', $output); foreach my $part (@stockValues) { $part =~ s/"//gos; # remove all quotes. Bit of a hack, but... XXX } if ($stockValues[4] > 0) { $stockValues[4] = 'up ' . (0+$stockValues[4]); } elsif ($stockValues[4] < 0) { $stockValues[4] = 'down ' . (0-$stockValues[4]); } else { $stockValues[4] = 'no change'; } $message .= "Stock quote for $stockValues[0]: $stockValues[1], $stockValues[4] (low: $stockValues[7], high: $stockValues[6])"; $self->say($event, $message); }