]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/Quote.pl
14ee9fb3ba4d5df272814e0b9a204675357096bd
[infobot.git] / src / Modules / Quote.pl
1 #
2 #  Quote.pl: retrieve stock quotes from yahoo
3 #            heavily based on Slashdot.pl
4 #   Version: v0.1
5 #    Author: Michael Urman <mu@zen.dhis.org>
6 # Licensing: Artistic
7 # changes from Morten Brix Pedersen (mbrix) and Tim Riker <Tim@Rikers.org>
8 #
9
10 package Quote;
11
12 use strict;
13
14 sub commify {
15     my $input = shift;
16     $input = reverse $input;
17     $input =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
18     return scalar reverse $input;
19 }
20
21 sub Quote {
22     my $stock = shift;
23     my @results = &::getURL('http://quote.yahoo.com/d/quotes.csv' .
24             "?s=$stock&f=sl1d1t1c1ohgv&e=.csv");
25
26
27     if (!scalar @results) {
28         &::msg($::who, "i could not get a stock quote :(");
29     }
30
31     my ($reply);
32     foreach my $result (@results) {
33         # get rid of the quotes
34         $result =~ s/\"//g;
35
36         my ($ticker, $recent, $date, $time, $change, $open,
37             $high, $low, $volume) = split(',',$result);
38
39         # add some commas
40         # "+ 0" removes trailing cr/lf/etc.
41         my $newvol = commify($volume + 0);
42
43         $reply .= ' ;; ' if $reply;
44         $reply .= "$ticker: $recent ($high/$low), $date $time, " .
45                 "Opened $open, Volume $newvol, Change $change";
46     }
47
48     if ($reply eq '') {
49         $reply = "i couldn't get the quote for $stock. sorry. :(";
50     }
51
52     &::performStrictReply($reply);
53 }
54
55 1;