#! /usr/bin/perl -w # A perl script to get and read nasdaq.com stock quotes from the internet given a list of URLs from a file. # TCP/IP must already be open. # modified for use on navajo RAW 9/18/99. require strict; use LWP::Simple; $BASEDIR='internet/stocks'; $PGDIR='/usr/local/pgsql/bin'; $URLfile=$ARGV[0]; $INSfile=$ARGV[1]; $DBname=$ARGV[2]; $DBtable=$ARGV[3]; $SQLfile=$ARGV[4]; $head=$ARGV[5]; $body=$ARGV[6]; $tail=$ARGV[7]; $page=$ARGV[8]; $logfile=$ARGV[9]; $goturls=0; $recsent=0; open (URL,"$BASEDIR/$URLfile") || die "ERROR: stocksession can't open file $URLfile for read."; open (PSQLDATA,">$BASEDIR/$INSfile") || die "ERROR: stocksession can't open file $INSfile for write."; while () { chomp; @doc=get "$_"; ++$goturls; %record=(); # Destroy %record each time through to avoid repeating lines that don't match. $BIGSTRING=""; foreach $dc (@doc) # form one big long string { $BIGSTRING=$BIGSTRING.$dc } if ($BIGSTRING =~ /symbol=(\w{1,5}).+selected/) {$record{"symbol"}=$1;} if ($BIGSTRING =~ /Stock Type:.+>(.{3}\.\s\d{1,2},\s\d{4}).+Market Closed/) {$record{"sdate"}=$1;} if ($BIGSTRING =~ /Last Sale:.+\$\s(-?\d*\.?,?\d+).+Net Change:/s) { $a1=$1; # insert a leading zero $a1=~s/^\./0./; $record{"last"}=$a1; } if ($BIGSTRING =~ /Volume:.+b>(\d*,?\d+,?\d00)<\/b.+Previous Close:/s) { $a1=$1; # take out kilo commas $a1=~s/,//g; $record{"volume"}=$a1; } if ($BIGSTRING =~ /Today's High:.+\$\s(-?\d*\.?,?\d+).+Today's Low:/s) { $a1=$1; # insert a leading zero $a1=~s/^\./0./; $record{"high"}=$a1; } if ($BIGSTRING =~ /Today's Low:.+\$\s(-?\d*\.?,?\d+).+>Best Bid:/s) { $a1=$1; # insert a leading zero $a1=~s/^\./0./; $record{"low"}=$a1; } if ($BIGSTRING =~ /Previous Close:.+\$\s(-?\d*.?,?\d+).+>Stock Type:/s) { $a1=$1; # insert a leading zero $a1=~s/^\./0./; $record{"prvclose"}=$a1; } print "symbol=$record{symbol}, sdate=$record{sdate}, last=$record{last}, low=$record{low}, high=$record{high}, prvclose=$record{prvclose}, volume=$record{volume}\n"; if (%record) { ++$recsent; print PSQLDATA "insert into $DBtable (symbol,sdate,last,low,high,prvclose,volume,tstamp) values ('$record{symbol}','$record{sdate}',$record{last},$record{low},$record{high},$record{prvclose},$record{volume},'now');\n"; } # end - if %record } # end - while URL close(URL);close(PSQLDATA); print "insert data into db.\n"; `$PGDIR/psql $DBname -f $BASEDIR/$INSfile >>$BASEDIR/$logfile`; print "perform query and output html text.\n"; `$PGDIR/psql -o $BASEDIR/html/$body -f $BASEDIR/programs/$SQLfile -d $DBname >>$BASEDIR/$logfile`; print "cat html output file.\n"; `cat $BASEDIR/html/$head $BASEDIR/html/$body $BASEDIR/html/$tail >$BASEDIR/html/$page`; print "post report.\n"; `cp -f $BASEDIR/html/$page /e/www/renegade/public_html/stock`;