]> git.somenet.org - irc/bugbot.git/blob - BotModules/Stocks.bm
some old base
[irc/bugbot.git] / BotModules / Stocks.bm
1 # -*- Mode: perl; tab-width: 4; indent-tabs-mode: nil; -*-
2 ################################
3 # Stocks Module                #
4 ################################
5
6 package BotModules::Stocks;
7 use vars qw(@ISA);
8 @ISA = qw(BotModules);
9 1;
10
11 # XXX Per-channel configurable notification of stock changes
12 # XXX Non-US markets
13
14 sub Help {
15     my $self = shift;
16     my ($event) = @_;
17     return {
18         '' => 'This module gets stock quotes. Ask me a ticker symbol, I will retrieve the quote.',
19         'stock' => 'Call this command with a ticker symbol to get the current stock price and change. Syntax: stock FBAR',
20     };
21 }
22
23 sub Told {
24     my $self = shift;
25     my ($event, $message) = @_;
26     if ($message =~ /^\s*stocks?\s+(.+?)\s*$/osi) {
27         $self->getURI($event, "http://download.finance.yahoo.com/d/quotes.csv?f=sl1d1t1c1ohgv&e=.csv&s=$1", $1);
28     } else {
29         return $self->SUPER::Told(@_);
30     }
31     return 0; # we've dealt with it, no need to do anything else.
32 }
33
34 sub GotURI {
35      my $self = shift;
36      my ($event, $uri, $output, $stock) = @_;
37      $self->debug($output);
38      my $message = "$event->{'from'}: ";
39      # The data currently listed in this format are: ticker symbol, last price, date, time, change, open price, daily high, daily low, and volume.
40      #  -- http://help.yahoo.com/help/us/fin/quote/quote-05.html
41      my @stockValues = split(',', $output);
42      foreach my $part (@stockValues) {
43           $part =~ s/"//gos; # remove all quotes. Bit of a hack, but... XXX
44      }
45      if ($stockValues[4] > 0) {
46          $stockValues[4] = 'up ' . (0+$stockValues[4]);
47      } elsif ($stockValues[4] < 0) {
48          $stockValues[4] = 'down ' . (0-$stockValues[4]);
49      } else {
50          $stockValues[4] = 'no change';
51      }
52      $message .= "Stock quote for $stockValues[0]: $stockValues[1], $stockValues[4] (low: $stockValues[7], high: $stockValues[6])";
53      $self->say($event, $message);
54 }