
' DesktopWeatherXML by Badboy (badboyKU@hotmail.com)
' v 1.0 - Initial Version
' v 1.1 - Added a set of images called old (which come from DesktopWeather v0.41)

'-------------------------------------------------------------------------------------------
' CREDITS
'-------------------------------------------------------------------------------------------
' Thanks goes out to Judge (samurize@jools.net) for his work on DesktopWeather v0.41. A lot
' of credit goes out to his original piece of work.


'-------------------------------------------------------------------------------------------
' INSTRUCTIONS
'-------------------------------------------------------------------------------------------
' First go to: http://xoap.weather.com/search/search?where=XXXXX
' The "XXXXX" can be anything you are searching for, such as zip code, city, state, or country.
' After loading that web page up you should have a search returned with a "loc id".
' The "loc id" will be used for your weather.


'-------------------------------------------------------------------------------------------
' CONSTANTS - You are welcome to change these to your settings.
'-------------------------------------------------------------------------------------------
const LOCID  = "UKXX0092" ' Set this to your "loc id" from the above instructions.
const UNIT   = "s"     ' Set of units. Standard or Metric (VALUES = [ s or m ]).
const DAYF   = "5"     ' Multi-day forecast information for some or all forecast elements (VALUE = [ 1 to 10 ]). Note: 5 is default for this script.
const IMAGES = "old"   ' Set this to which images you would like to use (VALUES = [ new or old ]).
const RESTART            = TRUE		                     ' If you want to restart the client after a cache file update. (VALUE = [ TRUE or FALSE ]). Note: You do not need to restart samurize, if you do not care about the weather images. Otherwise the images will never change UNLESS you manual reload the config file.
const INSTANCE_NAME      = "WeatherXML2"                ' Name of the Samurize instance to restart.
const SAMURIZE_DIRECTORY = "c:\program files\Samurize" ' Change the path if it is different than yours.


'-------------------------------------------------------------------------------------------
' CODE AFTER THIS, DO NOT CHANGE! UNLESS YOU KNOW WHAT YOU ARE DOING
'-------------------------------------------------------------------------------------------
'  The reason for 2 files is because weather.com has some restrictions on how many times
'  you contact the server in a set time. If you hammer the server too many times, you
'  will be blocked from the server for a small time (usually 30 minutes).
'-------------------------------------------------------------------------------------------
dim x30minURL, x30minFile, x120minURL, x120minFile
Randomize
x30minURL = "http://xoap.weather.com/weather/local/" & LOCID & "?cc=*" & "&par=" & Int(Rnd * 10000) & "&prod=xoap&key=" & Int(Rnd * 10000) & "&unit=" & UNIT
x30minFile = SAMURIZE_DIRECTORY & "\Scripts\DesktopWeather30min2.xml"
x120minURL = "http://xoap.weather.com/weather/local/" & LOCID & "?cc=*&dayf=" & DAYF & "&par=" & Int(Rnd * 10000) & "&prod=xoap&key=" & Int(Rnd * 10000) & "&unit=" & UNIT
x120minFile = SAMURIZE_DIRECTORY & "\Scripts\DesktopWeather120min2.xml"


'-------------------------------------------------------------------------------------------
' Misc Functions
'-------------------------------------------------------------------------------------------
Function validDayNum(dayNum)
  If (CInt(dayNum) > -1) AND (CInt(dayNum) < CInt(DAYF)) Then
    validDayNum = TRUE
  Else
    validDayNum = FALSE
  End If
End Function
Function abbrevDayName(dayName)
  If dayName = "Sunday" Then
    abbrevDayName = "SUN"
  ElseIf dayName = "Monday" Then
    abbrevDayName = "MON"
  ElseIf dayName = "Tuesday" Then
    abbrevDayName = "TUE"
  ElseIf dayName = "Wednesday" Then
    abbrevDayName = "WED"
  ElseIf dayName = "Thursday" Then
    abbrevDayName = "THU"
  ElseIf dayName = "Friday" Then
    abbrevDayName = "FRI"
  ElseIf dayName = "Saturday" Then
    abbrevDayName = "SAT"
  Else
    abbrevDayName = "NA"
  End If
End Function
Function copyImage(imageName, tempImageName)
  Dim fso
  copyImage = FALSE
  Set fso = CreateObject("Scripting.FileSystemObject")
  If fso.FileExists(SAMURIZE_DIRECTORY & "\Skins\DesktopWeatherXML2" & imageName) Then
    fso.CopyFile SAMURIZE_DIRECTORY & "\Skins\DesktopWeatherXML2" & imageName, SAMURIZE_DIRECTORY & "\Skins\DesktopWeatherXML2\temp\" & tempImageName, TRUE
    copyImage = TRUE
  End If
  Set fso = Nothing
End Function
Function getImage(num)
  If num=0 OR num=3 OR num=4 OR num=17 OR num=35 Then ' Thunderstorms
    getImage = "0"
  ElseIf num=1 OR num=2 Then ' Windy Rain
    getImage = "1"
  ElseIf num=5 Then ' Snow/Rain Mix
    getImage = "5"
  ElseIf num=6 OR num=18 Then ' Hail
    getImage = "6"
  ElseIf num=7 Then ' Snow/Rain Icy Mix
    getImage = "7"
  ElseIf num=8 Then ' Light Rain Icy
    getImage = "8"
  ElseIf num=9 Then ' Light Rain
    getImage = "9"
  ElseIf num=10 Then ' Heavy Rain Icy
    getImage = "10"
  ElseIf num=11 Then ' Rain
    getImage = "11"
  ElseIf num=12 OR num=39 OR num=40 Then ' Heavy Rain
    getImage = "12"
  ElseIf num=13 Then ' Light Snow
    getImage = "13"
  ElseIf num=14 Then ' Snow
    getImage = "14"
  ElseIf num=15 Then ' Snowflakes
    getImage = "15"
  ElseIf num=16 OR num=41 OR num=42 Then ' Heavy Snow
    getImage = "16"
  ElseIf num=19 Then ' Dust
    getImage = "19"
  ElseIf num=20 Then ' Fog
    getImage = "20"
  ElseIf num=21 Then ' Haze
    getImage = "21"
  ElseIf num=22 Then ' Smoke
    getImage = "22"
  ElseIf num=23 OR num=24 Then ' Windy
    getImage = "23"
  ElseIf num=25 Then ' Frigid
    getImage = "25"
  ElseIf num=26 Then ' Cloudy
    getImage = "26"
  ElseIf num=27 Then ' Mostly Cloudy Night
    getImage = "27"
  ElseIf num=28 Then ' Mostly Cloudy Day
    getImage = "28"
  ElseIf num=29 Then ' Partly Cloudy Night
    getImage = "29"
  ElseIf num=30 OR num=44 Then ' Partly Cloudy Day
    getImage = "30"
  ElseIf num=31 OR num=33 Then ' Clear Night (moon)
    getImage = "31"
  ElseIf num=32 Then ' Sunny Day (sun)
    getImage = "32"
  ElseIf num=34 Then ' Light Partly Cloudy Day
    getImage = "34"
  ElseIf num=36 Then ' Hot Sunny Day
    getImage = "36"
  ElseIf num=37 OR num=38 Then ' Lightening
    getImage = "37"
  ElseIf num=43 Then ' Windy/Snowy
    getImage = "43"
  ElseIf num=45 Then ' Rainy Night
    getImage = "45"
  ElseIf num=46 Then ' Snowy Night
    getImage = "46"
  ElseIf num=47 Then ' Thunderstorms Night
    getImage = "47"
  Else
    getImage = "na" ' N/A
  End If
End Function


'-------------------------------------------------------------------------------------------
' 30 Min File Functions
'-------------------------------------------------------------------------------------------
Function getTempUnit()
  getTempUnit = "°" & boundedString("<ut>", "</ut>", x30minFile)
End Function
Function getDistUnit()
  getDistUnit = boundedString("<ud>", "</ud>", x30minFile)
End Function
Function getSpeedUnit()
  getSpeedUnit = boundedString("<us>", "</us>", x30minFile)
End Function
Function getPrecipUnit()
  getPrecipUnit = boundedString("<up>", "</up>", x30minFile)
End Function
Function getPressureUnit()
  getPressureUnit = boundedString("<ur>", "</ur>", x30minFile)
End Function
Function getName()
  getName = boundedString("<dnam>", "</dnam>", x30minFile)
End Function
Function getTime()
  getTime = boundedString("<tm>", "</tm>", x30minFile)
End Function
Function getLatitude()
  getLatitude = boundedString("<lat>", "</lat>", x30minFile)
End Function
Function getLongitude()
  getLongitude = boundedString("<lon>", "</lon>", x30minFile)
End Function
Function getSunrise()
  getSunrise = boundedString("<sunr>", "</sunr>", x30minFile)
End Function
Function getSunset()
  getSunset = boundedString("<suns>", "</suns>", x30minFile)
End Function
Function getTimezone()
  getTimezone = boundedString("<zone>", "</zone>", x30minFile)
End Function
Function getLastUpdate()
  getLastUpdate = boundedString("<lsup>", "</lsup>", x30minFile)
End Function
Function getObservedAt()
  getObservedAt = boundedString("<obst>", "</obst>", x30minFile)
End Function
Function getTemp()
  getTemp = boundedString("<tmp>", "</tmp>", x30minFile)
  If getTemp = "NA" Then
    getTemp = "NA"
  Else
    getTemp = getTemp & getTempUnit()
  End If
End Function
Function getFeelsLike()
  getFeelsLike = boundedString("<flik>", "</flik>", x30minFile)
  If getFeelsLike = "NA" Then
    getFeelsLike = "NA"
  Else
    getFeelsLike = getFeelsLike & getTempUnit()
  End If
End Function
Function getCondition()
  getCondition = boundedString("<t>", "</t>", x30minFile)
End Function
Function getConditionIcon()
  getConditionIcon = boundedString("<icon>", "</icon>", x30minFile)
End Function
Function getBarPressure()
  getBarPressure = boundedString3("<r>", "</r>", 1, boundedString("<bar>", "</bar>", x30minFile))
  If getBarPressure = "NA" Then
    getBarPressure = "NA"
  Else
    getBarPressure = getBarPressure & " " & getPressureUnit()
  End If
End Function
Function getBarPressureDir()
  getBarPressureDir = boundedString3("<d>", "</d>", 1, boundedString("<bar>", "</bar>", x30minFile))
  If getBarPressureDir = "rising" Then
    getBarPressureDir = "h"
  ElseIf getBarPressureDir = "falling" Then
    getBarPressureDir = "i"
  Else 'steady
    getBarPressureDir = ""
  End If
End Function
Function getWindSpeed()
  getWindSpeed = boundedString3("<s>", "</s>", 1, boundedString("<wind>", "</wind>", x30minFile))
  If getWindSpeed = "NA" Then
    getWindSpeed = "NA "
  ElseIf getWindSpeed <> "calm" Then
    getWindSpeed = getWindSpeed & " " & getSpeedUnit()
  End If
End Function
Function getWindDegree()
  getWindDegree = boundedString3("<d>", "</d>", 1, boundedString("<wind>", "</wind>", x30minFile))
End Function
Function getHumidity()
  getHumidity = boundedString("<hmid>", "</hmid>", x30minFile)
  If getHumidity = "NA" Then
    getHumidity = "NA"
  Else
    getHumidity = getHumidity & "%"
  End If
End Function
Function getVisibility()
  getVisibility = boundedString("<vis>", "</vis>", x30minFile)
  If getVisibility = "NA" Then
    getVisibility = "NA"
  ElseIf getVisibility <> "Unlimited" Then
    getVisibility = getVisibility & " " & getDistUnit()
  End If
End Function
Function getUVindex()
  getUVindex = boundedString3("<i>", "</i>", 1, boundedString("<uv>", "</uv>", x30minFile))
  If getUVindex = "NA" Then
    getUVindex = "NA"
  Else
    getUVindex = getUVindex & " " & getUVindexText()
  End If
End Function
Function getUVindexText()
  getUVindexText = boundedString3("<t>", "</t>", 1, boundedString("<uv>", "</uv>", x30minFile))
End Function
Function getDewPoint()
  getDewPoint = boundedString("<dewp>", "</dewp>", x30minFile)
  If getDewPoint = "NA" Then
    getDewPoint = "NA"
  Else
    getDewPoint = getDewPoint & getTempUnit()
  End If
End Function


'-------------------------------------------------------------------------------------------
' 120 Min File Functions
'-------------------------------------------------------------------------------------------
Function getDayName(dayNum) 'dayNum values [0...9]
  If validDayNum(dayNum) Then
    If CInt(dayNum) = 0 Then
      If getDayHigh(dayNum) = "NA" Then
        getDayName = "NA"
      ElseIf getDayHigh(dayNum) = "N/A" Then 'its tonight
        getDayName = "Tonight"
      Else 'its today
        getDayName = "Today"
      End If
    Else
      getDayName = abbrevDayName(boundedString("<day d=" & Chr(34) & dayNum & Chr(34) & " t=" & Chr(34), Chr(34), x120minFile))
    End If
  Else
    getDayName = "NA"
  End If
End Function
Function getDayDate(dayNum) 'dayNum values [0...9]
  If validDayNum(dayNum) Then
    getDayDate = boundedString3("dt=" & Chr(34), Chr(34), 1, boundedString("<day d=" & Chr(34) & dayNum & Chr(34), "</day>", x120minFile))
  Else
    getDayDate = "NA"
  End If
End Function
Function getDayHigh(dayNum) 'dayNum values [0...9]
  If validDayNum(dayNum) Then
    getDayHigh = boundedString3("<hi>", "</hi>", 1, boundedString("<day d=" & Chr(34) & dayNum & Chr(34), "</day>", x120minFile))
  Else
    getDayHigh = "NA"
  End If
End Function
Function getDayLow(dayNum) 'dayNum values [0...9]
  If validDayNum(dayNum) Then
    getDayLow = boundedString3("<low>", "</low>", 1, boundedString("<day d=" & Chr(34) & dayNum & Chr(34), "</day>", x120minFile))
  Else
    getDayLow = "NA"
  End If
End Function
Function getDayHighLow(dayNum) 'dayNum values [0...9]
  Dim high, low
  If validDayNum(dayNum) Then
    high = getDayHigh(dayNum)
    low = getDayLow(dayNum)
    If high = "NA" Then
      getDayHighLow = "NA"
    ElseIf high = "N/A" Then
      getDayHighLow = low & "°"
    Else
      getDayHighLow = high & "°" & "/ " & low & "°"
    End If
  Else
    getDayHighLow = "NA"
  End If
End Function
Function getDaySunrise(dayNum) 'dayNum values [0...9]
  getDaySunrise = boundedString3("<sunr>", "</sunr>", 1, boundedString("<day d=" & Chr(34) & dayNum & Chr(34), "</day>", x120minFile))
End Function
Function getDaySunset(dayNum) 'dayNum values [0...9]
  getDaySunset = boundedString3("<suns>", "</suns>", 1, boundedString("<day d=" & Chr(34) & dayNum & Chr(34), "</day>", x120minFile))
End Function
Function getDayIcon(dayNum) 'dayNum values [0...9]
  If getDayHigh(dayNum) = "N/A" Then 'get icon for night
    getDayIcon = boundedString3("<icon>", "</icon>", 1, boundedString3("<part p=" & Chr(34) & "n" & Chr(34) & ">", "</part>", 1, boundedString("<day d=" & Chr(34) & dayNum & Chr(34), "</day>", x120minFile)))
  Else 'get icon for day
    getDayIcon = boundedString3("<icon>", "</icon>", 1, boundedString3("<part p=" & Chr(34) & "d" & Chr(34) & ">", "</part>", 1, boundedString("<day d=" & Chr(34) & dayNum & Chr(34), "</day>", x120minFile)))
  End If
End Function
Function getDayPrecip(dayNum) 'dayNum values [0...9]
  If getDayHigh(dayNum) = "N/A" Then 'get precip percentage for night
    getDayPrecip = boundedString3("<ppcp>", "</ppcp>", 1, boundedString3("<part p=" & Chr(34) & "n" & Chr(34) & ">", "</part>", 1, boundedString("<day d=" & Chr(34) & dayNum & Chr(34), "</day>", x120minFile)))
    If getDayPrecip = "NA" Then
      getDayPrecip = "NA"
    Else
      getDayPrecip = getDayPrecip & "%"
    End If
  Else 'get precip percentage for day
    getDayPrecip = boundedString3("<ppcp>", "</ppcp>", 1, boundedString3("<part p=" & Chr(34) & "d" & Chr(34) & ">", "</part>", 1, boundedString("<day d=" & Chr(34) & dayNum & Chr(34), "</day>", x120minFile)))
    If getDayPrecip = "NA" Then
      getDayPrecip = "NA"
    Else
      getDayPrecip = getDayPrecip & "%"
    End If
  End If
End Function
Function getDayHumidity(dayNum) 'dayNum values [0...9]
  If getDayHigh(dayNum) = "N/A" Then 'get humidity for night
    getDayHumidity = boundedString3("<hmid>", "</hmid>", 1, boundedString3("<part p=" & Chr(34) & "n" & Chr(34) & ">", "</part>", 1, boundedString("<day d=" & Chr(34) & dayNum & Chr(34), "</day>", x120minFile)))
    If getDayHumidity = "NA" Then
      getDayHumidity = "NA"
    Else
      getDayHumidity = getDayHumidity & "%"
    End If
  Else 'get humidity for day
    getDayHumidity = boundedString3("<hmid>", "</hmid>", 1, boundedString3("<part p=" & Chr(34) & "d" & Chr(34) & ">", "</part>", 1, boundedString("<day d=" & Chr(34) & dayNum & Chr(34), "</day>", x120minFile)))
    If getDayHumidity = "NA" Then
      getDayHumidity = "NA"
    Else
      getDayHumidity = getDayHumidity & "%"
    End If
  End If
End Function



'-------------------------------------------------------------------------------------------
' Get File Functions
'-------------------------------------------------------------------------------------------
Function getCacheFiles()
  On Error Resume Next
  Dim file1, file2, mins, copy
  mins = 30 ' Do not change time or you will be blocked by weather.com server!
  file1 = getCacheFile(mins, x30minURL, x30minFile)
  mins = 120 ' Do not change time or you will be blocked by weather.com server!
  file2 = getCacheFile(mins, x120minURL, x120minFile)
  If file1 Then
    copy = copyImage("\images\" & IMAGES & "\" & getImage(getConditionIcon()) & ".png", "now.png")
  End If
  If file2 Then
    For i = 0 To (DAYF-1)
      copy =  copyImage("\images\" & IMAGES & "\" & getImage(getDayIcon(i)) & ".png", "day" & i & ".png")
    Next
  End If
  If file1 OR file2 Then
    restartClient
  End If
End Function
Function getCacheFile(mins, fileURL, filename)
  Dim oldFile, fs
  Set fs = CreateObject("Scripting.FileSystemObject")
  If (Not fs.FileExists(filename)) Then
    oldFile = True
  Else
    Set cacheFile = fs.GetFile(filename)
    oldFile = False
    If CInt(DateDiff("n", cacheFile.DateLastModified, Now())) >= CInt(mins) Then
      oldFile = True
    End If
  End If
  If oldFile Then
    Dim Http, HtmlResult, f
    Set Http = CreateObject("Microsoft.XMLhttp")
    Http.Open "GET", fileURL, False
    Http.Send
    HtmlResult = Http.ResponseText
    Set Http = Nothing
    Set f = fs.CreateTextFile(filename, true, true)
    f.write(HtmlResult)
    f.close
    Set f = nothing
  End If
  Set fs = nothing
  getCacheFile = oldFile
End Function


'-------------------------------------------------------------------------------------------
' String Extraction Functions
'-------------------------------------------------------------------------------------------
Function boundedString(LeftBound, RightBound, fileName)
  boundedString = boundedString2(LeftBound, RightBound, 1, fileName)
End Function
Function boundedString2(LeftBound, RightBound, count, fileName)
  Dim xmlResult
  Set file_obj = CreateObject ("Scripting.FileSystemObject")
  If (file_obj.FileExists(fileName)) Then
    Set file_path = file_obj.GetFile(fileName) ' Creates the connection to the file
    Set file = file_path.OpenAsTextStream (1, -2) ' Opens the file for reading
    xmlResult = file.readall
    File.Close ' Closes the file
    Set file_path = nothing ' Resets variable
    Set file_obj = nothing ' Resets variable
    boundedString2 = boundedString3(LeftBound, RightBound, count, xmlResult)
  Else
    boundedString2 = "NA"
  End If
End Function
Function boundedString3(LeftBound, RightBound, count, xmlResult)
  Dim xmlStart, xmlEnd
  xmlStart = InStr(1, xmlResult, LeftBound, 1) + Len(LeftBound)
  xmlEnd = InStr(xmlStart, xmlResult, RightBound, 1)
  For counter = 2 To count
    xmlStart = InStr(xmlEnd + Len(RightBound), xmlResult, LeftBound, 1) + Len(LeftBound) 
    xmlEnd = InStr(xmlStart, xmlResult, RightBound, 1)
  Next
  If xmlStart > 0 AND xmlEnd > 0 Then
    boundedString3 = Mid(xmlResult, xmlStart, xmlEnd - xmlStart)
  Else
    boundedString3 = "NA"
  End If
End Function


'-------------------------------------------------------------------------------------------
' Restart Functions
'-------------------------------------------------------------------------------------------
Function restartClient
  Dim objWMIService, colProcessList, objProcess
  If RESTART Then
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    Set colProcessList = objWMIService.ExecQuery ("select * from Win32_Process where CommandLine LIKE '%Client.exe%" & INSTANCE_NAME & "%'")
    ' Refresh this instance of samurize
    startClient
    restartClient = "terminating process"
    ' Now kill our caller
    For Each objProcess in colProcessList
      killProcess(objProcess)
    Next
  End If
End Function

Function startClient
  Dim WshShell, oExec
  Set WshShell = createobject("wscript.shell")
  Set oExec = WshShell.Exec(SAMURIZE_DIRECTORY & "\Client.exe " & INSTANCE_NAME)
End Function

Function killProcess(objProcess)
  objProcess.terminate(0)
End Function


Function ReSTARTM
Dim WshShell, oExec
 If RESTART then
 Set WshShell = createobject("wscript.shell")
 WshShell.run "taskkill /IM Client.exe", 0, TRUE
 Set oExec = WshShell.Exec("""C:\Program Files\Samurize\client.exe"" ""WeatherXML2""") ' Path & Instance Name
 End If
End Function
