Jump to content

Recommended Posts

Posted (edited)

this is taken from an xml

<description><![CDATA[
<img src="http://l.yimg.com/us.yimg.com/i/us/we/52/30.gif"/><br />

how do i extract the image location?

i tried this with no luck:

$WeatherIcon = _XMLGetAttrib("/rss/description", "img src")

Edited by onelowzx6r
Posted

You must be trying the code I posted in your other topic:

http://www.autoitscript.com/forum/index.ph...05&hl=yahoo

The problem here is that the contents of a CDATA section are not parsed. You will have to use StringBetween or StringRegExp to get the url.

You can add this code to your script to see the CDATA content:

;Dump cdata
$aNodes = _XMLGetValue("/rss/channel/item/description")
If NOT @ERROR AND IsArray($aNodes) Then
    ConsoleWrite("Description: " & $aNodes[0] & @CRLF)
    For $X = 0 to Ubound($aNodes)-1
        ConsoleWrite("[" & $X & "]: " & $aNodes[$X] & @CRLF)
    Next
    ConsoleWrite(@CRLF)
EndIf
Posted

Here is a complete script:

#include <_XMLDomWrapper.au3>
#include <Inet.au3>

Dim $aKeys[1], $aValues[1]
Const $Zip = 85259

;METHOD 1 - Existing file
;$sXMLFile = "12345.xml"
;$result = _XMLFileOpen($sXMLFile, 'xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"')
;If $result = 0 Then Exit

;METHOD 2 - 
$WeatherXML = _INetGetSource("http://weather.yahooapis.com/forecastrss?p=" & $Zip)
_XMLLoadXML($WeatherXML, 'xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"')
If @ERROR Then
    MsgBox(0,"",@ERROR)
    Exit
EndIf

;Retrieve city (single attribute access)
$sCity = _XMLGetAttrib("/rss/channel/yweather:location", "city")
ConsoleWrite("City: " & $sCity & @CRLF)

;Retrieve region (single attribute access)
$sRegion = _XMLGetAttrib("/rss/channel/yweather:location", "region")
ConsoleWrite("Region: " & $sRegion & @CRLF)

ConsoleWrite(@CRLF)

;Dump location attributes
$aNodes = _XMLGetAllAttrib("/rss/channel/yweather:location", $aKeys, $aValues)
If IsArray($aNodes) Then
    ConsoleWrite("Location Attributes: " & $aNodes[0][0] & @CRLF)
    For $X = 0 to Ubound($aKeys)-1
        ConsoleWrite("[" & $X & "]: " & $aKeys[$X] & ": " & $aValues[$X] & @CRLF)
    Next
    ConsoleWrite(@CRLF)
EndIf

;Dump condition attributes
$aNodes = _XMLGetAllAttrib("/rss/channel/item/yweather:condition", $aKeys, $aValues)
If IsArray($aNodes) Then
    ConsoleWrite("Condition Attributes: " & $aNodes[0][0] & @CRLF)
    For $X = 0 to Ubound($aKeys)-1
        ConsoleWrite("[" & $X & "]: " & $aKeys[$X] & ": " & $aValues[$X] & @CRLF)
    Next
    ConsoleWrite(@CRLF)
EndIf

;Dump CDATA
$sCDATA = _GetFirstValue("/rss/channel/item/description")
ConsoleWrite("CDATA: " & $sCDATA & @CRLF)

;Dump image url
$aMatches = StringRegExp($sCDATA, 'img src="(.+)"', 3)
ConsoleWrite("Image URL: " & $aMatches[0] & @CRLF)
ConsoleWrite(@CRLF)

;Dump ALL forecast attributes
$count = _XMLGetNodeCount("/rss/channel/item/yweather:forecast")
For $X = 1 to $count

    $aNodes = _XMLGetAllAttrib("/rss/channel/item/yweather:forecast["&$X&"]", $aKeys, $aValues)
    ConsoleWrite("Forecast Attributes: " & $aNodes[0][0] & @CRLF)

    For $Y = 0 to Ubound($aKeys)-1
        ConsoleWrite("[" & $Y & "]: " & $aKeys[$Y] & ": " & $aValues[$Y] & @CRLF)
    Next
    ConsoleWrite(@CRLF)
Next

;Get the first real value returned from the _XMLGetValue() return array. REQUIRED
Func _GetFirstValue($node)
    $ret_val = _XMLGetValue($node)
    If IsArray($ret_val) Then
        Return ($ret_val[1])
    Else
        Return SetError(1,3,0)
    EndIf
EndFunc
Posted

yes except the script is completely different from that now. Thanks so much weaponx for replying unfortunately, how do i insert your code into this?

You can see where i have commented out to get the weather icon, and lower where is should display the icon

#Region;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=Wall06.ico
#AutoIt3Wrapper_outfile=MotoWeather\MotoWeather.exe
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_Res_Fileversion=1.2.0.8
#AutoIt3Wrapper_Res_Fileversion_AutoIncrement=y
#AutoIt3Wrapper_Res_Language=1033
#EndRegion;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Run_Debug_Mode=Y
#include <IE.au3>
_IEErrorHandlerRegister()
#include <_XMLDomWrapper.au3>
#include <Array.au3>
#include <INet.au3>
#include <GUIConstants.au3>

Dim $aKeys[1], $aValues[1]
Local $City, $Region, $DayToday, $DateToday, $LowToday, $HighToday, $ConditionToday, $PercentMatch
Local $DayTomorrow, $DateTomorrow, $LowTomorrow, $HighTomorrow, $ConditionTomorrow, $Percent, $Condition, $count
Local $LowTemp, $HighTemp, $Zip, $lowTempTest, $highTempTest, $zipTest

#Region ### START Koda GUI section ### Form=
$MotoForm = GUICreate("MotoWeather", 349, 409, -1, -1)
$Progress1 = GUICtrlCreateProgress(64, 8, 150, 16)
$btnStart = GUICtrlCreateButton("Check Weather", 248, 8, 91, 25)
$Label1 = GUICtrlCreateLabel("Progress:", 8, 8, 48, 17)
$Label2 = GUICtrlCreateLabel("High Today:", 8, 72, 62, 17)
$lblHigh = GUICtrlCreateLabel("", 80, 72, 59, 17)
$Label3 = GUICtrlCreateLabel("Low Today:", 8, 104, 60, 17)
$lblLow = GUICtrlCreateLabel("", 80, 104, 59, 17)
$btnProperties = GUICtrlCreateButton("Properties", 248, 36, 91, 25)
$btnRadar = GUICtrlCreateButton("Radar", 248, 64, 91, 25)
$btnHourly = GUICtrlCreateButton("Hourly Forecast", 248, 92, 91, 25)
$Label5 = GUICtrlCreateLabel("Precipitation:", 8, 208, 76, 17)
$lblPercent = GUICtrlCreateLabel("", 96, 208, 59, 17)
$lblDateLoc = GUICtrlCreateLabel("Please Press 'Check Weather'", 8, 40, 227, 17)
$Group1 = GUICtrlCreateGroup("Conditions", 8, 136, 329, 57)
$lblCondition = GUICtrlCreateLabel("", 16, 154, 316, 33)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("Suggestion", 8, 240, 329, 73)
$lblSuggest = GUICtrlCreateLabel("", 16, 258, 316, 49)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group3 = GUICtrlCreateGroup("Tomorrow's Weather", 8, 328, 329, 73)
$lblTomorrowWeather = GUICtrlCreateLabel("", 16, 346, 316, 49)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        
        Case $btnStart
            If Not FileExists('C:\Program Files\MotoWeather\Config.ini') Then; create ini dir if it doesn't exist
                DirCreate('C:\Program Files\MotoWeather')
                _ModifyIni(); populate ini file
            EndIf

            $Zip = IniRead('C:\Program Files\MotoWeather\Config.ini', 'Zip', 'Code', ""); get zip from ini file
            $LowTemp = IniRead('C:\Program Files\MotoWeather\Config.ini', 'TempLimits', 'Low', ""); get low temp from ini file
            $HighTemp = IniRead('C:\Program Files\MotoWeather\Config.ini', 'TempLimits', 'High', ""); get high temp from ini file
            $temp = VariableTest()
            If ($temp <> 0) Then _ModifyIni()

            GUICtrlSetData($Progress1, 10)

            $window = _IECreate("http://www.rssweather.com/hw3.php?pands=" & $Zip, 0, 0); create IE Window set to invisble
            $text = _IEBodyReadText($window); read the text off the page
            $split = StringSplit($text, @CR); split the text found on the website into an array
            _IEQuit($window); closes the IE object and the process associated with it

            For $i = 0 To UBound($split); loop through the array
                $temp = StringInStr($split[$i], "Updated:", 0); get to the point where it says updated
                If ($temp <> 0) Then
                    $temp = $i + 1
                    ExitLoop; exit once Updated: is found
                EndIf
            Next

            GUICtrlSetData($Progress1, 25)

            $Storm = StringInStr($split[$temp], "storm", 0); check for storm
            $Snow = StringInStr($split[$temp], "snow", 0); check for snow
            $Rain = StringInStr($split[$temp], "rain", 0); check for rain
            $Showers = StringInStr($split[$temp], "showers", 0); check for showers
            If ($Storm > 0 Or $Snow > 0 Or $Rain > 0 Or $Showers > 0) Then
                $BadWeather = $split[$temp]; get the bad weather condition string
                $BadWeather = StringStripCR($BadWeather)
                $temp += 1; go down to the next line
                $PercentLoc = StringInStr($split[$temp], "Percentage:", 0); look for percentage
                If ($PercentLoc > 0) Then; if percentage is found grab the number
                    $Length = StringLen($split[$temp]); get the string length
                    $Percent = StringRight($split[$temp], ($Length - ($PercentLoc + 11))); grab the rest of the string after the Percentage: location
                    $Percent = StringStripWS($Percent, 3); remove leading and trailing spaces
                Else
                    $Percent = "N/A"; if percent not found just put N/A out there
                EndIf
            Else
                $Percent = "N/A"; if not bad weather just put N/A out there
            EndIf
            GUICtrlSetData($Progress1, 40)

        ;connect to the Yahoo RSS and retrieve the weather information
            $WeatherXML = _INetGetSource("http://weather.yahooapis.com/forecastrss?p=" & $Zip)
            _XMLLoadXML($WeatherXML, 'xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"')
            If @error Then
                MsgBox(0, "", @error)
                Exit
            EndIf

            GUICtrlSetData($Progress1, 75)
        ;Retrieve city
            $City = _XMLGetAttrib("/rss/channel/yweather:location", "city")
        ;Retrieve region
            $Region = _XMLGetAttrib("/rss/channel/yweather:location", "region")
        ;Retrieve icon

        ; Loop through to grab todays and tomorrows weather
            $count = _XMLGetNodeCount("/rss/channel/item/yweather:forecast")
            For $X = 1 To $count
                $aNodes = _XMLGetAllAttrib("/rss/channel/item/yweather:forecast[" & $X & "]", $aKeys, $aValues)
                For $Y = 0 To UBound($aKeys) - 1
                    If ($X = 1) Then
                        Switch $aKeys[$Y]
                            Case "day"
                                $DayToday = $aValues[$Y]
                            Case "date"
                                $DateToday = $aValues[$Y]
                            Case "low"
                                $LowToday = $aValues[$Y]
                            Case "high"
                                $HighToday = $aValues[$Y]
                            Case "text"
                                $ConditionToday = $aValues[$Y]
                        EndSwitch
                    EndIf

                    If ($X = 2) Then
                        Switch $aKeys[$Y]
                            Case "day"
                                $DayTomorrow = $aValues[$Y]
                            Case "date"
                                $DateTomorrow = $aValues[$Y]
                            Case "low"
                                $LowTomorrow = $aValues[$Y]
                            Case "high"
                                $HighTomorrow = $aValues[$Y]
                            Case "text"
                                $ConditionTomorrow = $aValues[$Y]
                        EndSwitch
                    EndIf
                Next
            Next

            GUICtrlSetData($Progress1, 95)

            $Tornado = StringInStr($ConditionToday, "tornado", 0)
            $Flood = StringInStr($ConditionToday, "flood", 0)
            $Fog = StringInStr($ConditionToday, "fog", 0)
            $Heat = StringInStr($ConditionToday, "heat", 0)

            If ($Tornado > 0 Or $Flood > 0 Or $Fog > 0 Or $Heat > 0 Or $Storm > 0 Or $Snow > 0 Or $Rain > 0 Or $Showers > 0) Then
                $Condition = "Bad"
                If ($BadWeather <> "") Then $ConditionToday = $ConditionToday & " / " & $BadWeather; use both if bad weather was set to something
            Else
                $Condition = "Good"
            EndIf
            GuiCtrlCreatePic($WeatherIcon , 0, 0, 250, 70)
            GUICtrlSetData($lblHigh, $HighToday)
            GUICtrlSetData($lblLow, $LowToday)
            GUICtrlSetData($lblCondition, $ConditionToday)
            GUICtrlSetData($lblDateLoc, $DayToday & " " & $DateToday & @TAB & $City & ", " & $Region)
            GUICtrlSetData($lblPercent, $Percent)
            GUICtrlSetData($lblTomorrowWeather, $DayTomorrow & " " & $DateTomorrow & @CRLF & "High: " & $HighTomorrow & "  Low: " & $LowTomorrow & @CRLF & $ConditionTomorrow)
            
            If ($LowToday > $LowTemp And $HighToday < $HighTemp And $Condition = "Good") Then
                GUICtrlSetData($lblSuggest, "Today Should Be A Great Ride!" & @CRLF & $ConditionToday & @CRLF & "High: " & $HighToday & " " & "Low: " & $LowToday)
            ElseIf ($LowToday > $LowTemp And $HighToday < $HighTemp And $Condition = "Bad") Then
                GUICtrlSetData($lblSuggest, "CAUTION! - Bad Weather Predicted" & @CRLF & $ConditionToday)
            ElseIf (($LowToday <= $LowTemp Or $HighToday >= $HighTemp) And $Condition = "Good") Then
                GUICtrlSetData($lblSuggest, 'CAUTION! - Out of Temperature Range' & @CRLF & "High: " & $HighToday & " " & "Low: " & $LowToday)
            ElseIf (($LowToday <= $LowTemp Or $HighToday >= $HighTemp) And $Condition = "Bad") Then
                GUICtrlSetData($lblSuggest, 'EXTEME CAUTION! - Riding Not Advised!' & @CRLF & $ConditionToday & @CRLF & "High: " & $HighToday & " " & "Low: " & $LowToday)
            EndIf
        Case $btnProperties
            If Not FileExists('C:\Program Files\MotoWeather\Config.ini') Then DirCreate('C:\Program Files\MotoWeather'); create ini file if it doesn't exist
            _ModifyIni(); populate ini file
        Case $btnHourly
            $Zip = IniRead('C:\Program Files\MotoWeather\Config.ini', 'Zip', 'Code', ""); get zip from ini file
            $Hourly = _IECreate('http://www.accuweather.com/forecast-accupop.asp?partner=accuweather&traveler=0&zipChg=1&zipcode=' & $Zip, 1, 1, 0, 1)
        Case $btnRadar
            $Zip = IniRead('C:\Program Files\MotoWeather\Config.ini', 'Zip', 'Code', ""); get zip from ini file
            $Radar = _IECreate('http://www.accuweather.com/radar-state.asp?partner=accuweather&traveler=0&zipcode=' & $Zip, 1, 1, 0, 1)
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd


Func _ModifyIni()
    $winPos = WinGetPos("MotoWeather", "Check Weather"); gets the window position places the coordinates into an array
    $xPos = $winPos[0]; the x position is in the first spot in the array (zero means first)
    $yPos = $winPos[1]; the y position is in the second spot in the array
    #Region ### START Koda GUI section ### Form=
    $frmProperities = GUICreate("MotoWeather Properties", 237, 164, $xPos, $yPos, -1, -1, $MotoForm)
    $Label2 = GUICtrlCreateLabel("Highest Temperature:", 8, 16, 106, 17)
    $inHigh = GUICtrlCreateInput("", 128, 8, 65, 21)
    $inLow = GUICtrlCreateInput("", 128, 48, 65, 21)
    $Label3 = GUICtrlCreateLabel("Lowest Temperature:", 8, 56, 104, 17)
    $Label1 = GUICtrlCreateLabel("Zip Code:", 60, 96, 50, 17)
    $inZip = GUICtrlCreateInput("", 128, 88, 65, 21)
    $btnSubmit = GUICtrlCreateButton("Save", 72, 128, 75, 25, 0)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                GUIDelete($frmProperities); remove properties window
                ExitLoop; ExitLoop exits out of the loop and does the code after the WEnd, which is nothing
            Case $btnSubmit
                $LowTemp = GUICtrlRead($inLow); read the input from user for low temp
                $HighTemp = GUICtrlRead($inHigh); read the input from user for high temp
                $Zip = GUICtrlRead($inZip); read the input from user for zip code
                $temp = VariableTest()
                Switch $temp
                    Case 0
                        WriteIni(); write to the ini file
                        GUIDelete($frmProperities); remove properties window
                        ExitLoop; Exit Loop will exit the while loop, in this case goes back to the main window
                    Case 1
                        MsgBox(48, "Error", "Low Temperature is higher than High Temperature, Try Again")
                        ContinueLoop; Continue Loop ignores all logic after this point and returns to the While 1 statement in this function
                    Case 2
                        MsgBox(48, "Error", "Zip Code must be a 5 digit number, Try Again")
                        ContinueLoop; Continue Loop ignores all logic after this point and returns to the While 1 statement in this function
                    Case 3
                        MsgBox(48, "Error", "Low Temperature must be a 1 to 3 digit number, Try Again")
                        ContinueLoop; Continue Loop ignores all logic after this point and returns to the While 1 statement in this function
                    Case 4
                        MsgBox(48, "Error", "High Temperature must be a 1 to 3 digit number, Try Again")
                        ContinueLoop; Continue Loop ignores all logic after this point and returns to the While 1 statement in this function
                EndSwitch
        EndSwitch
    WEnd
EndFunc  ;==>_ModifyIni

Func WriteIni()
    If Not FileExists('C:\Program Files\MotoWeather\Config.ini') Then DirCreate('C:\Program Files\MotoWeather'); create ini file directory if it doesn't exist
    IniWrite('C:\Program Files\MotoWeather\Config.ini', 'TempLimits', 'Low', $LowTemp); write the low temp to ini file
    IniWrite('C:\Program Files\MotoWeather\Config.ini', 'TempLimits', 'High', $HighTemp); write the high temp to ini file
    IniWrite('C:\Program Files\MotoWeather\Config.ini', 'Zip', 'Code', $Zip); write the zip code to ini file
EndFunc  ;==>WriteIni

Func VariableTest()
    $zipTest = StringRegExp($Zip, "\d{5}"); check to see if it is a 5 digit number
    $lowTempTest = StringRegExp($LowTemp, "\d{1,3}"); check to see if it is a 1-3 digit number
    $highTempTest = StringRegExp($LowTemp, "\d{1,3}"); check to see if it is a 1-3 digit number
    If ($zipTest = 1 And $lowTempTest = 1 And $highTempTest = 1) Then; check to see if all variables match pattern
        If ($LowTemp < $HighTemp) Then; if Low Temperature is less than High Temperature
            Return 0; return 0 (or success in this case)
        Else
            Return 1; return 1 (or error in this case, low temp > high temp)
        EndIf
    ElseIf ($zipTest <> 1) Then
        Return 2; return 2 (or error in this case, zip code does not match pattern)
    ElseIf ($lowTempTest <> 1) Then
        Return 3; return 3 (or error in this case, low temp does not match pattern)
    ElseIf ($highTempTest <> 1) Then
        Return 4; return 4 (or error in this case, high temp does not match pattern)
    EndIf
EndFunc  ;==>VariableTest
Posted

Whats the problem? The script I posted is pretty much modular, meaning the commented sections should all work by themselves. Anyways...this is the part that you need.

;Dump CDATA
$sCDATA = _GetFirstValue("/rss/channel/item/description")
ConsoleWrite("CDATA: " & $sCDATA & @CRLF)

;Dump image url
$aMatches = StringRegExp($sCDATA, 'img src="(.+)"', 3)
ConsoleWrite("Image URL: " & $aMatches[0] & @CRLF)
ConsoleWrite(@CRLF)oÝ÷ Ù8b²·ªº*Þ±§îËb¢v®¶­sc´vWBFRf'7B&VÂfÇVR&WGW&æVBg&öÒFRõÔÄvWEfÇVR&WGW&â'&â$UT$T@¤gVæ2ôvWDf'7EfÇVRb33c¶æöFR¢b33c·&WE÷fÂÒõÔÄvWEfÇVRb33c¶æöFR¢b4'&b33c·&WE÷fÂFVà¢&WGW&âb33c·&WE÷fųҢVÇ6P¢&WGW&â6WDW'&÷"Ã2âVæD`¤VæDgVæ

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...