]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/Quote.pl
* Accidentally left in a debug output
[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 =
24       &::getURL( 'http://quote.yahoo.com/d/quotes.csv'
25           . "?s=$stock&f=sl1d1t1c1ohgv&e=.csv" );
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
34         # get rid of the quotes
35         $result =~ s/\"//g;
36
37         my (
38             $ticker, $recent, $date, $time, $change,
39             $open,   $high,   $low,  $volume
40         ) = split( ',', $result );
41
42         # add some commas
43         # "+ 0" removes trailing cr/lf/etc.
44         my $newvol = commify( $volume + 0 );
45
46         $reply .= ' ;; ' if $reply;
47         $reply .=
48             "$ticker: $recent ($high/$low), $date $time, "
49           . "Opened $open, Volume $newvol, Change $change";
50     }
51
52     if ( $reply eq '' ) {
53         $reply = "i couldn't get the quote for $stock. sorry. :(";
54     }
55
56     &::performStrictReply($reply);
57 }
58
59 1;
60
61 # vim:ts=4:sw=4:expandtab:tw=80