ASP December 13th, 2006
I’m currently working on a project that requires live weather data to be displayed on the homepage. Having searched around for a quick ASP script to do the work for me, I was left disappointed.
I ended up writing my own after finding that Yahoo! offer a free developers’ weather service here: http://developer.yahoo.com/weather.
The finished class is shown below - but requires functions “writeCache” and “readCache” which I might post another day. For now, you could just write your own or set “useCache” to false in the class.
Instructions on usage are in the comments.
Enjoy.
' =============================================================================================================
' @name Weather Class
'
' @author James Crooke
' james@fish-media.net
'
' @copyright Fish Media Ltd 2006
'
' @desc retrieves latest weather from weather.yahoo.com
'
' @usage dim objWeather
' set objWeather = new weather
' with objWeather
' .location = "SPXX0015" '(Spain)
' .celsius = true '(true uses celsius, false uses fahrenheit)
' .fetch()
' end with
'
' @notes Find location at http://weather.yahoo.com/regional/EUROPEX.html
'
' @requires readCache, writeCache functions
' =============================================================================================================
class weather
private parseError
public condition
public forecast
private p_location
private p_celsius
private useCache
private sub class_initialize()
set objXML = Server.CreateObject("Microsoft.XMLDOM")
set objLst = Server.CreateObject("Microsoft.XMLDOM")
celsius = false
useCache = true
end sub
public property let location(str)
p_location = str
end property
public property let celsius(str)
p_celsius = str
end property
private sub class_terminate()
set objXML = nothing
set objLst = nothing
end sub
public sub fetch()
cacheEx = ""
weatherCache = ""
url = "http://xml.weather.yahoo.com/forecastrss?p=" & p_location
if p_celsius then
url = url & "&u=c"
cacheEx = "cel"
end if
if useCache then
weatherCache = readCache("weather" & p_location & cacheEx, 1) '1 day cache
end if
if weatherCache = "" then
sourceXML = getXML(url)
objXML.async = False
objXML.loadXML(sourceXML)
If objXML.parseError.errorCode <> 0 Then
parseError = true
else
condition = getCondition("yweather:condition")
forecast = getForecast("yweather:forecast")
if useCache then
cacheStr = condition(0) & "|" & condition(1) & "|" & condition(2) & "###" & forecast(0) & "|" & forecast(1)
call writeCache("weather" & p_location & cacheEx, cacheStr)
end if
end if
else
weatherList = split(weatherCache, "###")
condition = split(weatherList(0), "|")
forecast = split(weatherList(1), "|")
end if
end sub
private function getCondition(inElem)
dim result(2)
Set elemList = objXML.getElementsByTagName(inElem)
For i=0 To (elemList.length -1)
result(0) = elemList.item(i).getAttribute("temp")
result(1) = elemList.item(i).getAttribute("text")
result(2) = elemList.item(i).getAttribute("code")
Next
getCondition = result
end function
private function getForecast(inElem)
dim result(1)
Set elemList = objXML.getElementsByTagName(inElem)
For i=0 To (elemList.length -1)
' if the day is today...
if lcase(elemList.item(i).getAttribute("day")) = lcase(weekdayName(weekday(date),true)) then
result(0) = elemList.item(i).getAttribute("low")
result(1) = elemList.item(i).getAttribute("high")
end if
Next
' return the array result
getForecast = result
end function
public function getXML(sourceFile)
dim styleFile
dim xmlDoc
Dim xmlhttp
Set xmlhttp = Server.CreateObject("Microsoft.XMLHTTP")
xmlhttp.Open "GET", sourceFile, false
xmlhttp.Send
getXML = xmlhttp.ResponseText
set xmlhttp = nothing
end function
end class
Example Usage:
' (after including the class)...
dim objWeather
set objWeather = new weather
with objWeather
.location = "SPXX0015"
.celsius = true
.fetch()
end with
response.write("<ul>")
response.write("<li>Temperature: " & objWeather.condition(0) & "</li>")
response.write("<li>Condition: " & objWeather.condition(1) & "</li>")
response.write("<li>Low: " & objWeather.forecast(0) & "</li>")
response.write("<li>High: " & objWeather.forecast(1) & "</li>")
response.write("</ul>")
Download “ASP Weather Class” here.