#!/usr/bin/perl -w # Name : Perl Stock Quote # Author : Terrence Ma # Email : terrence@terrence.com # Web : http://www.terrence.com # Date : V1.2 08/06/2002, V1.0 08/15/2000 # Modified : FreeStuff Online - http://fsonline.piff.de/stockmarket/ ######################################################################## # Stock Quote Script by Mukul Sabharwal (mukulsabharwal@yahoo.com # # Summary : Connects to Yahoo! Finance (quote.yahoo.com) and retrieves # # basic quotes & in this update (4-March-2000) the ability to view # # charts. I would appreciate if you add a link to my site. # # This script is protected by copyright law (1997). I made this script # # for personal use on 12th October 1997. I updated the script in 2000 # # for it's compatability with NT and also making it a cgi program. Now # # on (4-March-2000) I also added chart viewing facility. # ######################################################################## use 5; use strict; use CGI::Pretty; use CGI::Carp qw(fatalsToBrowser); use Socket; use FileHandle; use Getopt::Std; my $SERVER = 'quote.yahoo.com'; my $PORT = 80; my $TIMEOUT = 15; my $raw = 0; my $ipaddr; my $proto; sub init { my @hostinfo; my @protoinfo; @hostinfo = gethostbyname($SERVER) or die "gethostbyname error: $!\n"; @protoinfo = getprotobyname('tcp') or die "getprotobyname error: $!\n"; $ipaddr = $hostinfo[4]; $proto = $protoinfo[2]; } sub get_result { my $inbody = 0; my @results; for (@_) { chomp; s/\s+$//; if ($inbody) { push @results, $_; next; } if (/^$/) { $inbody = 1; next; } } @results; } sub parse_csv { my $text = shift; my @fields = (); while ($text =~ m/"([^"\\]*(\\.[^"\\]*)*)",?|([^,]+),?|,/g) { push(@fields, defined($1) ? $1 : $3); } push(@fields, undef) if $text =~ m/,$/; @fields; } sub getquote { my $q = shift; my @securities = @_; my @text; my $there = pack('Sna4x8', AF_INET, $PORT, $ipaddr); my $ipstr = join('.', map { ord($_) } split('', $ipaddr)); my $sock = new FileHandle; socket $sock, PF_INET, SOCK_STREAM, $proto or die "socket error: $!\n"; connect $sock, $there or die "connect error: $!\n"; $sock->autoflush(1); my $query = "/d/quotes.csv?s=" . join('+', @securities) . "&f=sl1d1t1c1ohgv&e=.csv"; print $sock <; if ($raw) { print @text; } else { @text = get_result @text; my $tablestr; my $fontcolor = "#000000"; for (@text) { my @fields = parse_csv $_; if ($fields[2] eq "N/A") { print $q->p, "No such ticker '$fields[0]'.

"; } # check division by zero error elsif ($fields[1] == $fields[4]) { my $change = 0; } else { my $change = sprintf "%.2f", (0 + $fields[4]) / ($fields[1] - (0 + $fields[4])) * 100; print <

Perl Stock Quote

Ticker : $fields[0]
Last Trade : $fields[1]
Last Trade At : $fields[2] $fields[3]
Change : $fields[4] (change%)
Opened At : $fields[5]
Day Range : $fields[7] - $fields[6]
Volume : $fields[8]

Chart : $fields[0]
Dow Jones Composite
Nasdaq Composite
Chart for ticker : $fields[0]
Dow Jones
Nasdaq

EOM } } if ($tablestr) { print $q->table({-border => 0}, $q->Tr({-align => 'center'}, $q->th([ 'Ticker', 'Last', 'Last date/time', 'Change', 'Open', 'Day Range', 'Volume' ])), $tablestr); } } $sock->close; } sub showform { my $cgi = shift; print $cgi->startform(-method => 'GET'); print "
\n"; print "\n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print "
\n"; print " \n"; print " Perl Stock Quote\n"; print " \n"; print "
\n"; print " \n"; print " \n"; print "
\n"; print "
\n"; print $cgi->endform; } # init variable my $cgi = new CGI; # print header print $cgi->header, $cgi->start_html(-title => "Perl Stock Quote"); print $cgi->center($cgi->h1("Perl Stock Quote")), $cgi->hr(), $cgi->br(); if ($cgi->param('tickers')) { my @tickers = split ' ', $cgi->param('tickers'); init; getquote $cgi, @tickers; } else { showform $cgi; } # print footer print $cgi->hr(), $cgi->a({-href=>"http://www.terrence.com/perl/stock/stock.txt"}, "Program Source"), $cgi->br(); print $cgi->end_html;