My first post on the forum. I have an Excel spreadsheet which I set up to download current stock market data from the Yahoo site for managing my 401(k). I want to convert this to a stand-alone program, and post it here so others can use it or port it to different systems.
Since I know essentially nothing about Internet programming, and the Excel VBA macro offers few clues, I searched the Web for examples and found the following method on a Russian site (whose url I have unfortunately misplaced). I replaced their example download url with a shortened version of my Yahoo one and it worked on the first try. Love when that happens. If I can find that Russian site again I’ll thank them. (I tried the HttpGet.bas example that came with FB but couldn’t get it to work on the Yahoo download although it worked on others).
I’ve sprinkled a few of my own comments here and there in the listing. Before I go any further with this program, I’m wondering if the Internet experts on this board would be willing look at it and suggest improvements. For my simple purpose, I think some of the code could be deleted. For one thing, the HttpQueryInfo function returns an error. Am I correct in guessing this is because the data is not actually in a file? When Yahoo gets my query they gather the info and zap it back to me as a stream of CSVs (Comma-Separated Values), and until that point no one can say what the length of the “file” is? Also, the listing mentions “IE 4.0 or better” but is IE even necessary? I certainly didn’t have to start up mine to make this work; just connected on my rural dial-up and ran the program. Or am I missing the point?
Any suggestions would be much appreciated. I’m running FB 0.21.1 and Windows XP. I downloaded a bunch of Wininet documentation off the MS website which I’ll try to wade through in quest of enlightenment. I only recently moved to FreeBasic, but I’ve been hobbying with various BASICs for many years. Just never did any web stuff.
Thanks -
Jim
Code: Select all
'download files, Windows only (Russian note)
' requires IE 4.0 or better (Russian note)
' tested using fb v.16b, (Russian note)
' and fb v.17b from CVS (11/2006) (Russian note)
#include ONCE "windows.bi"
#INCLUDE ONCE "win\wininet.bi"
DECLARE FUNCTION dlfile(sURL AS STRING,LFileName AS STRING) AS INTEGER
DIM AS STRING sURL,LFileName
DIM AS INTEGER res
'''This is the download url for Yahoo's current/closing stock data in CSV format, for 3 stocks:
sURL = "http://download.finance.yahoo.com/d/quotes.csv?s=GE+IBM+PG&f=l1p2p5b4m3edn"
LFileName = EXEPATH & "\" & "YahDld02.txt" 'Location of downloaded data file on user's computer.
SCREENRES 640, 150, 24, 1
res = dlfile(sURL,LFileName)
IF res > 0 THEN
LOCATE 1, 1
PRINT LFileName; " ...downloaded"
PRINT " Size:"; res; " bytes"
ELSE
'''I Get following error on stock download try - but the CSV data comes in OK...
PRINT "Error encountered, res not > 0..."; res
ENDIF
PRINT
PRINT " Sleeping...press a key to exit."
BEEP
SLEEP
END
FUNCTION dlfile(sURL AS STRING,LFileName AS STRING) AS INTEGER
DIM AS HINTERNET hOpen, hFile
DIM AS INTEGER ctot, nret, res, tbsize, t 'ctot is total number of bytes returned.
'nret is number of bytes returned (in each line?)???
DIM AS STRING tbuff, ts
DIM AS STRING scUserAgent = "Zippy" '''Can this be any name I choose?
DIM AS BYTE PTR mybuff
mybuff = ALLOCATE(1024)
'Create an internet connection.
LOCATE 8, 26
PRINT " Getting Internet Connection";
hOpen = INTERNETOPEN(scUserAgent,internet_open_type_direct,NULL,NULL,0)
IF hOpen = 0 THEN RETURN -1 'failed.
'Open the url.
LOCATE 8, 1
PRINT SPACE(79);
LOCATE 8, 35
PRINT " Opening URL";
hFile = INTERNETOPENURL(hOpen,sURL,NULL,0,internet_flag_reload,0)
IF hFile = 0 THEN RETURN -2 'failed.
'Let's get the file size.
' I think this requires IE 4.0 engine, not 3.0 (Russian note)
tbuff = SPACE(32)
tbsize = 32
res = HTTPQUERYINFO(hFile,_
http_query_content_length,_
STRPTR(tbuff),_
@tbsize,_
NULL)
tbsize=VAL(TRIM(tbuff))
'if res = false or tbsize < 1 then return -3 'failed, but don't care.. (Russian note)
IF tbsize > 0 THEN
t = 40 - (LEN(sURL)/2)
t = IIF(t < 1,1,t)
LOCATE 8, t
PRINT LEFT(sURL,80);
t = 40 - (LEN(LFileName)/2)
t = IIF(t < 1,1,t)
LOCATE 14, t
PRINT LFileName;
COLOR RGB(0,0,255), 0
LOCATE 10, 4
PRINT CHR(221);
LOCATE 10, 76
PRINT CHR(222);
COLOR RGB(0,255,0), 0
ENDIF
OPEN LFileName FOR BINARY ACCESS write AS #1
nret = 99
while nret > 0
res = INTERNETREADFILE(hFile,mybuff,1024,@nret)
IF nret > 0 THEN
PUT #1, , mybuff[0], nret
ctot += nret
IF tbsize > 0 THEN
LOCATE 10, 5 + INT(ctot/tbsize*70)
PRINT CHR(219);
ts = STR(ctot) & " bytes of " & STR(tbsize)
LOCATE 12, (40 - LEN(ts)/2)
PRINT ts;
ELSE
LOCATE 1, 1
PRINT " Retrieved "; ctot; " bytes ";
ENDIF
ENDIF
WEND
CLOSE #1
INTERNETCLOSEHANDLE(hFile)
INTERNETCLOSEHANDLE(hOpen)
DEALLOCATE mybuff
RETURN tbsize
END FUNCTION