Weather Watch Sample code
Keywords: IReadData Weather report historical
SAMPLE:
;***************************************************************************
;** WeatherWatch.wbt
;** Get Historical Weather information
;**
;** Purpose: Gather weather info for cities from www.wunderground.com
;** Inputs: date in YYYY:MM:DD format
;** Outputs: File- 'weather{date}.txt'
;** Revisions: 1.0
;** Author: Deana Dahley
;***************************************************************************
;specify city
where = "KBFI";
city = "Seattle, WA"
;where = "KLAX"
;city = "Los Angeles, CA"
;where = "KDAL"
;city = "Dallas, TX"
;where = "KLGA";
;city = "New York, NY"
;where = "PHJH"
;city = "Lahaina, HI"
:dddialog
MyDialogFormat=`WWWDLGED,5.0`
MyDialogCaption=`Get Historical Weather Conditions`
MyDialogX=78
MyDialogY=78
MyDialogWidth=101
MyDialogHeight=104
MyDialogNumControls=9
MyDialog01=`19,84,49,DEFAULT,PUSHBUTTON,DEFAULT,"OK",1`
MyDialog02=`44,36,36,DEFAULT,EDITBOX,year,"2000"`
MyDialog03=`44,52,36,DEFAULT,EDITBOX,month,"01"`
MyDialog04=`44,68,36,DEFAULT,EDITBOX,day,"01"`
MyDialog05=`4,36,36,DEFAULT,STATICTEXT,DEFAULT,"Year:"`
MyDialog06=`4,68,36,DEFAULT,STATICTEXT,DEFAULT,"Day:"`
MyDialog07=`4,52,36,DEFAULT,STATICTEXT,DEFAULT,"Month:"`
MyDialog08=`4,6,89,DEFAULT,STATICTEXT,DEFAULT,"Input date that you want condtions for.."`
MyDialog09=`27,20,51,DEFAULT,VARYTEXT,city,"Some City"`
ButtonPushed=Dialog("MyDialog")
;Define where you want output file
weatherfile= strcat(DirGet(),"weather%year%%month%%day%.txt")
;Load the WinInet extender
AddExtender("WWINT34I.DLL")
; The host name is the first part of the URL, minus the http://
host="www.wunderground.com"
;Handle user input
;make sure month is single digit if start with zero
len=strlen(month)
if len>1
char=strsub(month,1,1)
if char==0
month=strsub(month,2,1)
endif
endif
;part of the URL we wish to GET is *everything* after the host name
geturl="history/airport/%Where%/%year%/%month%/%day%/DailyHistory.html"
;Define temporary file name for downloaded page
tempfile=strcat(DirGet(),"result.html")
; Basic web page fetch script
tophandle=iBegin(0,"","")
connecthandle=iHostConnect(tophandle, host, @HTTP, "", "")
datahandle=iHttpInit(connecthandle,"GET",geturl,"", 0)
rslt = iHttpOpen(datahandle, "", 0, 0);
if rslt!=200
headers=iHttpHeaders(datahandle)
AskItemList("Rslt=%rslt%",headers,@tab,@unsorted,@single)
endif
xx=iReadData(datahandle,tempfile)
iClose(datahandle)
iClose(connecthandle)
iClose(tophandle)
fs=FileSize(tempfile)
buf=BinaryAlloc(fs+100)
BinaryRead(buf,tempfile)
begptr = BinaryIndexEx( buf, 0, "Observed:", @FWDSCAN ,0);find 'Observed:'
if begptr == -1
;Message("Hmmm", "Observed: not found in %tempfile% file")
Message("No historial information found!", "Check the date.%@Crlf%- Make sure the date input is valid%@CRLF%- Not today's date or a future date")
Binaryfree(buf)
exit
else
endptr = BinaryIndexEx( buf,begptr+1, '</center>', @FWDSCAN ,0);find 'Observed:'
endif
;allocate second buffer
buf2=BinaryAlloc(endptr-begptr+Strlen(city)+3)
;inlude City as first line in output file
BinaryPokeStr(Buf2,0,Strcat(city,@CRLF))
;copy to another buffer
BinaryCopy (buf2,Strlen(city)+1,buf,begptr,endptr-begptr)
;Remove html formatting
BinaryReplace(buf2,'<b>','',@FALSE)
BinaryReplace(buf2,'</b>','',@FALSE)
BinaryReplace(buf2,'</td>','',@FALSE)
BinaryReplace(buf2,'</tr>','',@FALSE)
BinaryReplace(buf2,'<td>',' ',@FALSE)
BinaryReplace(buf2,'<tr>',@CRLF,@FALSE)
BinaryReplace(buf2,@lf,'',@FALSE)
BinaryReplace(buf2,@cr,@crlf,@FALSE)
BinaryReplace(buf2,'#176','',@FALSE)
ampersand=num2char(38)
BinaryReplace(buf2,ampersand,'',@FALSE)
semicolon=num2char(59)
BinaryReplace(buf2,semicolon,'',@FALSE)
BinaryReplace(buf2,'nbsp','',@FALSE)
;create file
BinaryWrite(buf2,weatherfile)
;free buffers
BinaryFree(buf)
BinaryFree(buf2)
;delete temporary file
FileDelete(tempfile)
;Display output in notepad
Run("notepad.exe",weatherfile)
exit
Article ID: W14441
Filename: Sample code - Weather Watch.txt