'////////////////////////////////////////////////////////////////////////
'// WEATHER SCRIPT FOR 9.9
'// WRITTEN BY: Chriso
'// This script will return the temperature in your location.
'////////////////////////////////////////////////////////////////////////

'// Website location of RSS forecast can be found by:
'// 1. Navigating to http://weather.yahoo.com/
'// 2. Entering city/zip code and clicking Go
'// 3. Clicking the orange [RSS] icon to the right of the page
'// 4. This is the URL value we are looking for so enter it below...
Const URL = "http://weather.yahooapis.com/forecastrss?p=ASXX0219&u=c"

'// Return Script Information
Sub Script(Name, Major, Minor, Build, Author, Commands, Description)
	Name = "Weather"
	Major = 1
	Minor = 0
	Build = 0
	Author = "Chriso"
	Commands = "today, weather"
	Description = "This script will return the temperature in the location specified above."
End Sub

'// Called when the script is loaded for a particular bot
Sub Event_Load()
	CreateCommand "today", "Weather", "Command_Today", "", "", "Get weather forecast for today only", 0, True
	CreateCommand "weather", "Weather", "Command_Weather", "", "", "Get weather forecast", 0, True
End Sub

'// Callback for "Weather" Command
Sub Command_Weather(CS)
	If scInet.StillExecuting Then
		CS.Reply "Previous request is still being retrieved."
	Else
		Dim Current, Forecast, Days, Location, I
		Dim Content, ToFind
		Content = scInet.OpenURL(URL)
		ToFind = "<title>"
		If InStr(Content, ToFind) Then
			Location = Split(Content, ToFind)(1)
			Location = Split(Location, "</title>")(0)
			Location = Replace(Location, "Yahoo! Weather - ", "")
			CS.Reply Location & ":"
		End If
		ToFind = "<b>Current Conditions:</b><br />"
		If InStr(Content, ToFind) Then
			Content = Split(Content, ToFind)(1)
			Current = Split(Content, "<BR />")(0)
			CS.Reply "- Today - " & Current
			ToFind = "<b>Forecast:</b><BR />"
			If InStr(Content, ToFind) Then
				Forecast = Split(Content, ToFind)(1)
				Forecast = Split(Forecast, "<a href=")(0)
				Days = Split(Forecast, "<br />")
				For I = 0 To UBound(Days)
					If Len(Days(I)) > 2 Then
						CS.Reply "- " & Days(I)
					End If
				Next
			End If
		End If
	End If
End Sub

'// Callback for "Today" Command
Sub Command_Today(CS)
	If scInet.StillExecuting Then
		CS.Reply "Previous request is still being retrieved."
	Else
		Dim Current, Location
		Dim Content, ToFind
		Content = scInet.OpenURL(URL)
		ToFind = "<title>"
		If InStr(Content, ToFind) Then
			Location = Split(Content, ToFind)(1)
			Location = Split(Location, "</title>")(0)
			Location = Replace(Location, "Yahoo! Weather - ", "")
		End If
		ToFind = "<b>Current Conditions:</b><br />"
		If InStr(Content, ToFind) Then
			Content = Split(Content, ToFind)(1)
			Current = Split(Content, "<BR />")(0)
			CS.Reply Location & ": " & Current
		End If
	End If
End Sub