#!/usr/bin/perl -w # # Stock Quote Module # # Currently actually a Yahoo Stock Quote Module; if I feel the urge I # may build a module tree instead. Based on previous Stock Exchange # efforts. # # Waider 07/06/2003 package Stock; use strict; use Data::Dumper; use Date::Parse; use Finance::QuoteHist; use HTML::TokeParser; use LWP::UserAgent; my $CGISITE = 'http://finance.yahoo.com'; my $CGIROOT = $CGISITE . '/q?s='; my $ua; # XXX this only covers the ones I'm interested in my %currencies = ( 'L' => 'GBP', 'OL' => 'NOK', ); sub new { my $self = shift; my %options = @_; my $ua = new LWP::UserAgent; $ua->agent( "GeekToy/0.1 " . $ua->agent ); $ua->env_proxy(); my $obj = bless {}, $self; $obj->{'ua'} = $ua; $obj; } sub quote { my $self = shift; my ( $ticker, $exchange ) = ( $self->{'ticker'}, $self->{'exchange'}); my ( $date, $value, $currency ); $ticker ||= shift; $exchange ||= shift; $exchange ||= ""; my $q = Finance::QuoteHist->new( symbols => [$ticker], start_date => '1 week ago', end_date => 'today' ); my @quotes = $q->quotes(); my $d = $quotes[-1][1]; my $close = $quotes[-1][5]; my $udate = str2time( $d ); if ( !defined($udate)) { die "unable to str2time($d) (from " . Dumper($quotes[-1]) .")"; } return { 'date' => $udate, 'value' => $close, 'currency' => 'USD' } } 1;