Get Stock Information Code
#!/bin/sh
#\
exec tclsh "$0" "$@"
# gesi: get stock information
# Copyright (C) 2000 Luis F. Guzmán
#
# e-mail: luis.guzman@att.net
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
set msg \
"Usage: gesi ?-s? ?-l? ?-d? ?-t? ?-c? ?-o? ?-h? ?-g? ?-v? symbol ?symbol...?
where:
s = stock symbol
l = last price
d = last price date
t = last price time
c = change since yesterday
o = open
h = high for today
g = low for today
v = volume"
set HEADER_LINES 4
set options "sl1d1t1c1ohgv" ;# default options
# if customized options, get them
set or "false" ;# options variable has not been reset
while {$argc > 0 && [string range [lindex $argv 0] 0 0] == "-"} {
if {$or == "false"} {
set options ""
set or "true"
}
switch [string range [lindex $argv 0] 1 1] {
s {append options "s"}
p {append options "l1"}
d {append options "d1"}
t {append options "t1"}
c {append options "c1"}
o {append options "o"}
h {append options "h"}
l {append options "g"}
v {append options "v"}
default {
puts "gesi ERROR: invalid argument!"
puts $msg
exit
}
}
set argv [lreplace $argv 0 0]
set argc [expr $argc - 1]
}
if {$argc < 1} { ;# there has to be at least one stock symbol
puts $msg
exit
}
# get stock symbols
set first "true"
while {$argc > 0} {
if {$first == "true"} {
set first "false"
} else {
append sym "+"
}
append sym [lindex $argv 0]
set argv [lreplace $argv 0 0]
set argc [expr $argc - 1]
}
set sym [string toupper $sym]
if {[catch {socket quote.yahoo.com 80} so]} { ;# connect to server
puts "(gesi)ERROR: couldn't open socket: host is unreachable!"
exit -1
}
set q "/d/quotes.csv?s=$sym&f=$options" ;# construct query str
puts $so "GET $q HTTP/1.0\n\n" ;# send request
flush $so
set i 0
while {1} {
gets $so line
if {[eof $so]} { ;# don't print the last blank line
exit
} elseif {[expr $i > $HEADER_LINES] && ![regexp {^$} $line]} { ;# or header
puts $line
}
incr i
}