Wednesday, September 10, 2008

Grab HTML source code from website, save it to file


<!--#include file="../include/myFunctions.asp"-->
<%
Dim strHTML

Dim fileNameToSave
Dim linkToGrab

fileNameToSave = "D:\Sites\Danny_Testing\tmp\test.txt"
linkToGrab = "http://192.168.100.236/tmp/test.html"

updateFileFromWeb fileNameToSave, linkToGrab ' call the updateFileFrom

Function updateFileFromWeb( fileNameToSave, linkeToGrab )
lastModifiedDate = ShowDateModified( fileNameToSave )
currentDate = Now()

If DateDiff("n", lastModifiedDate, currentDate) > 1 Then ' If the file is one day old, we update it with new content from FirsTrade website (FirstTrade).
strHTML = GrabHTMLPageV1( linkToGrab )

strHTMLResult = ParseBlockString(strHTML,"<!--NAIStart-->", "<!--NAIEnd-->") ' We use the html comment tag for parsing the data.

writeToFile strHTMLResult, fileNameToSave ' Write to file

'Response.Write strHTMLResult
Response.Write "the file just updated."
End if
End Function

Function ShowDateModified(filespec)
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
ShowDateModified = f.DateLastModified
End Function

Function ShowDateCreated(filespec)
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
ShowDateCreated = f.DateCreated
End Function

Sub writeToFile( str, fileName )
Dim Stuff, myFSO, WriteStuff, dateStamp
dateStamp = Date()

Set myFSO = CreateObject("Scripting.FileSystemObject")
Set WriteStuff = myFSO.OpenTextFile( fileName, 2, True)

WriteStuff.WriteLine( str )
WriteStuff.Close

SET WriteStuff = NOTHING
SET myFSO = Nothing
End Sub

Function ParseBlockString( strFull, strStart, strEnd )
strStartPosition = InStr(1, strFull, strStart)
strEndPosition = InStr(1, strFull, strEnd) + Len(strEnd)

strLength = strEndPosition - strStartPosition // The length between the strStart to strEnd

if strStartPosition = 0 Or strEndPosition = 0 Then
ParseBlockString = "One of Strings (strStartPosition, strEndPosition) not found!"
ElseIf strLength < 1 then // It is not right if the the strStartPosition is bigger than strEndPosition, coz strStartPosition should come first, then strEndPosition comes next.
ParseBlockString = "One of Strings (strStartPosition, strEndPosition) not found!"
Else
ParseBlockString = Mid(strHTML, strStartPosition, strLength)
// ParseBlockString = Server.HTMLEncode( Mid(strHTML, strStartPosition, strLength) ) ' If you would like to encode the html code, use this line
End if
End Function


Function GrabHTMLPageV1(strURL) ' This one works for older ASP
Dim objXmlHttp
Set objXmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
objXmlHttp.Open "GET", strURL, False
objXmlHttp.Send


If Err.Number = 0 Then
If objXmlHttp.Status = 200 Then ' Return header code must be equal to 200
strHTML = objXmlHttp.ResponseText
'GrabHTMLPageV1 = Server.HTMLEncode(strHTML) ' This is for replacing < to < and > to >
GrabHTMLPageV1 = strHTML
Else ' There is an erro
GrabHTMLPageV1 = "either Incorrect URL or " & objXmlHttp.statusText
End if
Else
GrabHTMLPageV1 = Err.Description
End If

Set objXmlHttp = Nothing
End Function

Function GrabHTMLPageV2(strURL) ' This one works for newer ASP
Dim objXML
Set objXML = Server.CreateObject("Microsoft.XMLHTTP")
objXML.Open "GET" , strURL , False ,"",""
objXML.Send

If Err.Number = 0 Then
If objXML.Status = 200 then ' Return header code must be equal to 200
GrabHTMLPageV2 = objXML.ResponseText
Else ' There is an erro
GrabHTMLPageV2 = "Incorrect URL"
End if
Else
GrabHTMLPageV2 = Err.Description
End If
Set objXML = Nothing
End Function

%>

No comments: