[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Stock Quotes

Rizz++ for his insight.

I rewrote it (now version 0.10!), and it's about twice as small and ten
times as robust.  A thought for a next version would be something like
"detailed stock quote for <BLAH>" to give all the info you get from the
CSV style.  Currently just reports last trade and change (KISS was

But this does the trick for now, I feel a little bad taking this away from
Rizz since his purpose seemed to be this exact thing, but I saw his
"dot" do this this morning and thought "What a great idea", so thanks to
Rizz for the inspiration also.

I ended up going with LWP::Simple instead of Telnet since it was already
embedded in my code and it's one function call.


On Wed, 29 Dec 1999, Cap'n Carl wrote:

> Another way of doing it: Add Net::Telnet to your Perl libs and use
> code like the following:
>   elsif( $str =~ /^(quote|ticker|stock) (\S+)/i && $directed ) {
>     my $t = new Net::Telnet( Timeout => 10 );
>     $t->open( Host => 'finance.yahoo.com', Port => 80 ) or
> 	 return "unable to get info.  try later, dude.";
>     $t->print( "get /d/quotes.csv?s=$2&f=sl1d1t1c1ohgv&e=.csv\n\n" );           
>     my $res = $t->getline;                                                      
>     chop( $res );                                                               
>     $t->close;                                                                  
>     $res =~ s/"//g;                                                             
>     ( $symbol, $last, $c_date, $c_time, $change, $open, $high,
>       $low, $volume ) = split( /,/, $res );
>     if( $last > 0 && $open > 0 ) {
>       $pct = sprintf( "%3.2f", ($change/$open)*100 );
>     }
>     return "$symbol - $last (range:$high - $low open:$open ".
>       "chg:$change $pct % vol:$volume as of $c_time on $c_date)";
>   }
> I joined this list specifically so I could add a module to do just
> that after talking to someone in #infobot.  *8)
> Please note: This is NOT drop-in code for Oznoid's infobot source.
> This is the code that little ``dot'' uses on EFnet.  BTW, say hi
> to her sometime.  She hangs out in quite a few channels.  :)
> Just running with that Perl philosophy.  Four million ways to do
> everything.  *8)
> -- Carl ``Rizz'' Boernecke (carlb@2qwk4u.dmv.com)
>    http://home.dmv.com/~carlb && http://www.n2o.org
#  Stock -- infobot module for stock quote lookups.
# 	    requires the LWP::Simple module.
#  By Jay Jacobs (teckle)
#  CSV pointers from Carl Boernecke (Rizz)

package Infobot::Module::Stock;

use strict;
use Infobot::Module;

$Infobot::Module::Stock::VERSION = "0.10_00";
$Infobot::Module{"Stock"} = $Infobot::Module::Stock::VERSION;
@Infobot::Module::Stock::ISA = qw(Infobot::Module);

my $any_bad;

  eval { require LWP::Simple };
  $@ and $any_bad = "LWP::Simple";
  warn "Infobot::Module::Stock requires $any_bad" if $any_bad;

sub Infobot::Module::Stock::new {
  my $class = shift ;
  return undef if $any_bad;

  my $self  = $class->SUPER::new(@_);
  $self->regex(qr/stock\s+(?:price |quote |for )?\s*(?:for|at|of)?\s*(\S+)/i);
  $self->usage('stock <SYMBOL>');
  $self->descrip("Tries to lookup stock quote for SYMBOL");

  bless $self, $class;

sub Infobot::Module::Stock::action {
  my ($self, $message) = @_;
  return undef unless $self->enabled;

  my $symbol = uc($message->get('args')->[0]);
  $self->status(5,"looking up stock for $symbol");
  my $content = LWP::Simple::get("http://finance.yahoo.com/d/quotes.csv?s=$symbol\&f=sl1d1t1c1ohgv\&e=.csv");
  return("$symbol: could not get information") if (! defined $content);
  my ($trade, $change) = (split(/,/, $content))[1,4];
  return("stock symbol '$symbol' doesn't exist") if ($trade eq "0.00");
  return("$symbol last traded at $trade ($change change)");



=head1 NAME

Stock -- looks up the current stock quote of a given stock.


stock for <SYMBOL>

stock quote for <SYMBOL>




	Connects up to yahoo's stock webpage, pulls down
	a CSV file and parses it for the info.

=head1 AUTHORS

Jay Jacobs (teckle) <jjacobs@securetty.org>
Rizz++ for the csv information.