ASP Weather Class

ASP No Comments »

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.

Cron for Windows IIS

General No Comments »

First, a lesson in the real implementation of cron

The crontab command, found in Unix operating systems, is used to schedule commands to be executed periodically. It reads commands and collects them into a file also known as a “crontab” which is later read by the operating system which carries out the instructions contained within.

One of the most useful cron commands, particularly from a web developers point of view, is the ability to call up a webpage silently, in the background, at intervals or at a specified time. Think of the possibilites:

  • Running a routine check on users in a database (deleting inactive accounts for example)
  • Scheduling an email, several days after a user has purchased an item from a site
  • Checking a site is functioning correctly

So how can you get Windows to perform cron-like tasks?

It’s very simple once you know how - which I do, you’ll be happy to learn. You have probably found that several web sites offer programs that cost around $100 that promise to emulate cron. Don’t bother; especially if all you want to do is call up a web address.

This is how you do it…

  1. Download wget (GnuWin32 is a good, clean, harmless one)
  2. Install wget
  3. Fire up the command line (Start > Run > then type “cmd”)
  4. Run a command as shown below:

schtasks /create /tn "Test Cron" /tr "C:\wget.exe http://www.site.com/cron.asp" /sc minute /mo 5 /ru "System"

Where

“Test Cron” is the name of your cron job (useful for reference purposes)
“C:\wget.exe” is the location of the wget.exe file
“http://www.site.com/cron.asp” is the URL for wget to fetch

More Examples

Let’s take a look at some more examples, that way you will understand the different options available…

Run a cron job every week

schtasks /create /tn "Test Cron" /tr "C:\wget.exe http://www.site.com/cron.asp" /sc weekly /ru "System"

Run a cron every week on a Friday

schtasks /create /tn "Test Cron" /tr "C:\wget.exe http://www.site.com/cron.asp" /sc weekly /d FRI /ru "System"

Run a cron every friday at 11:00am

schtasks /create /tn "Test Cron" /tr "C:\wget.exe http://www.site.com/cron.asp" /sc weekly /st 11:00:00 /d FRI /ru "System"

Run a cron every day at 17:00pm

schtasks /create /tn "Test Cron" /tr "C:\wget.exe http://www.site.com/cron.asp" /sc daily /st 17:00:00 /ru "System"

Run a cron every hour, on the hour

schtasks /create /tn "Test Cron" /tr "C:\wget.exe http://www.site.com/cron.asp" /sc hourly /st 00:00:00 /ru "System"

So what’s happening exactly?

Well, all your are doing is utilising Windows’ “Scheduled Tasks” program - using a command line interface, which I find easier to use. If you have installed wget and ran one of the commands above, you will be able to see your cron in action by going to “Start > Programs > Accessories > System Tools > Scheduled Tasks”.

And of course, your not just limited to fetching URL’s (using wget) - you can also execute a whole load of other programs - providing you know the right commands (or parameters, or both)!

The best thing about this Windows Cron method is you don’t need to have access to your web server for it to work - just any PC that is connected to the Internet (note: it needs to be connected when the Cron is scheduled to “fire”!). It’s also free and, because it runs as standard in Windows, it doesn’t use up any additional memory resources.

I hope this has helped you out! :)

The alternative of course is to host on a linux server, like this great host offers


© James Crooke 2000-2008
Entries RSS Login