#NoTrayIcon #include #include #Include #include AutoItSetOption("MustDeclareVars", 1) _IEErrorHandlerRegister() Global $oIE, $GUIActiveX, $txtZipcode, $btnZipCode, $btnCurrent, $btnForecast Global $zipCode, $winWidth, $winHeight, $waitTime, $initTime, $mainFrame, $which $zipCode = "36701" $waitTime = 300000 $winWidth = 250 $winHeight = 250 $which = 0 ; The GUI elements GUICreate("Weather", $winWidth, $winHeight, _ (@DesktopWidth - $winWidth) / 2, (@DesktopHeight - $winHeight) / 2, _ BitOr($WS_OVERLAPPEDWINDOW, $WS_VISIBLE, $WS_CLIPSIBLINGS, $WS_CLIPCHILDREN)) $oIE = _IECreateEmbedded() $GUIActiveX = GUICtrlCreateObj($oIE, 0, 25, $winWidth, $winHeight - 65) $txtZipcode = GUICtrlCreateInput($zipCode, 5, 0, 145, 25) $btnZipCode = GUICtrlCreateButton("Zip Code", 155, 0, 90, 25) $btnCurrent = GUICtrlCreateButton("Current", 5, 215, 120, 30) $btnForecast = GUICtrlCreateButton("Forecast", 125, 215, 120, 30) GUISetState() ; The initial screen _StartPage() $mainFrame = _IEFrameGetObjByName($oIE, "mainFrame") $zipCode = GUICtrlRead($txtZipcode) _IEBodyWriteHTML($mainFrame, _BuildPage(_GetCondition($zipCode))) ; Set the start time $initTime = _Timer_Init() ; The main event loop While 1 Local $msg = GUIGetMsg() Switch $msg Case $GUI_EVENT_CLOSE ExitLoop Case $btnZipcode $zipCode = GUICtrlRead($txtZipcode) _IEBodyWriteHTML($mainFrame, _BuildPage(_GetCondition($zipCode))) Case $btnCurrent _IEBodyWriteHTML($mainFrame, _BuildPage(_GetCondition($zipCode))) $which = 0 Case $btnForecast _IEBodyWriteHTML($mainFrame, _BuildPage(_GetForecast($zipCode))) $which = 1 EndSwitch If _Timer_Diff($initTime) > $waitTime Then If $which = 0 Then _IEBodyWriteHTML($mainFrame, _BuildPage(_GetCondition($zipCode))) Else _IEBodyWriteHTML($mainFrame, _BuildPage(_GetForecast($zipCode))) EndIf $initTime = _Timer_Init() EndIf WEnd ; Clean everything up and shut down _IEQuit($oIE) GUIDelete() Exit ; Sets up the initial frame Func _StartPage() Local $shtml = "" $shtml &= "" & @CR $shtml &= "" & @CR $shtml &= "Weather" & @CR $shtml &= "" & @CR $shtml &= "" & @CR $shtml &= "" & @CR $shtml &= "" & @CR $shtml &= "" & @CR _IENavigate($oIE, "about:blank") _IEDocWriteHTML($oIE, $shtml) _IEAction ($oIE, "refresh") EndFunc ; Builds the body of the page Func _BuildPage($body) Local $html = "" $html &= "" & @CR $html &= $body & @CR $html &= "" & @CR Return $html EndFunc ; Fetches the current condition lines from the RSS feed Func _GetCondition($zip) Local $xml = _GetWeather($zip) Local $el = _FindElement("yweather:condition", $xml) For $e In $el Local $line = $e, $text = "", $code = "", $date = "" $line = _FormatElement("yweather:condition", $line) Dim $parts = StringSplit($line, "|") For $p In $parts Dim $attr = StringSplit($p, "=", 1) Switch $attr[1] Case "text" $text &= "

" $text &= $attr[2] & "

" Case "code" $code &= "

" $code &= "

" Case "date" $date &= "

" & $attr[2] & "

" EndSwitch Next Next Return $code & $text & $date EndFunc ; Fetches the forecast lines from the RSS feed Func _GetForecast($zip) Local $xml = _GetWeather($zip) Local $el = _FindElement("yweather:forecast", $xml) Local $html = "" For $e In $el Local $line = $e, $text = "", $day = "", $low = "", $high = "", $code = "" $line = _FormatElement("yweather:forecast", $line) Dim $parts = StringSplit($line, "|") For $p In $parts Dim $attr = StringSplit($p, "=", 1) Switch $attr[1] Case "text" $text &= $attr[2] Case "day" $day &= "" $day &= $attr[2] & "" Case "low" $low &= "Low: " & $attr[2] Case "high" $high &= "High: " & $attr[2] Case "code" $code &= "" EndSwitch Next $html &= "
" & $code $html &= "

" $html &= $day & ": " & $text & "
" & $low & "
" & $high & "

" Next Return $html EndFunc ; Formats the URL and fetches the RSS Func _GetWeather($zip) Local $url = "http://weather.yahooapis.com/forecastrss?p=" & $zip Local $xml = _FetchFeed($url) If StringLen($xml) = 0 Then _ErrorMsg("No xml") Return $xml EndFunc ; Pulls out the lines that correspond to the $el value. Func _FindElement($el, $xml) Local $ycount = 0 Dim $yweather[25] Dim $lines = StringSplit($xml, @CRLF) For $line In $lines If StringRegExp($line, $el, 0) Then $yweather[$ycount] = $line $ycount = $ycount + 1 EndIf Next ReDim $yweather[$ycount] Return $yweather EndFunc ; Cleans up the RSS lines for proper formatting Func _FormatElement($el, $line) $line = StringReplace($line, " ", " ") $line = StringReplace($line, "<" & $el & " ", "") $line = StringReplace($line, "/>", "") $line = StringReplace($line, "=""", "=") $line = StringReplace($line, """ ", "|") Return $line EndFunc ; Goes out and gets the RSS feed Func _FetchFeed($url) Local $oHttp = _CreateMSXMLObj() If $oHttp = 0 Then _ErrorMsg("Could not create MSXML object") If Not IsObj($oHttp) Then Exit; $oHttp.Open("GET", $url, False) $oHttp.Send() Return $oHttp.responseText EndFunc ; Ganked and modified from: http://www.autoitscript.com/forum/index.php?showtopic=77155 Func _CreateMSXMLObj() Dim $xmlObj = ObjCreate("Msxml2.XMLHTTP.6.0") If IsObj($xmlObj) Then Return $xmlObj $xmlObj = ObjCreate("Msxml2.XMLHTTP.3.0") If IsObj($xmlObj) Then Return $xmlObj $xmlObj = ObjCreate("Msxml2.XMLHTTP") If IsObj($xmlObj) Then Return $xmlObj $xmlObj = ObjCreate("Microsoft.XMLHTTP") If IsObj($xmlObj) Then Return $xmlObj Return 0 EndFunc ; This is here so if there's an error, the program exits Func _ErrorMsg($msg) MsgBox(0, "Error", $msg) Exit EndFunc