WARNING: This UDF has been deprecated due to Google closing the doors on the (unsupported) Weather API. I have left the code as it is so others may learn from it.

GUI Example of using the _GoogleWeather UDF
UDF:
#include-once ; #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 ; #INDEX# ======================================================================================================================= ; Title .........: _GoogleWeather ; AutoIt Version : v3.3.2.0 or higher ; Language ......: English - uses https://www.google.com ; Description ...: Retrieves details about the weather in a specific region, using the 'undocumented' Google Weather API. ; Note ..........: ; Author(s) .....: guinness ; Remarks .......: ; =============================================================================================================================== ; #INCLUDES# ==================================================================================================================== ; None ; #GLOBAL VARIABLES# ============================================================================================================ ; None ; #CURRENT# ===================================================================================================================== ; _GoogleWeather_Current: Retrieves details about the current weather status in a specific region. ; _GoogleWeather_Forecast: Retrieves details about the 4 day forecast for a specific region. ; _GoogleWeather_Get: Retrieves all data that will be parsed from the Google Weather API. ; _GoogleWeather_Information: Retrieves details about the general forecast information. ; =============================================================================================================================== ; #INTERNAL_USE_ONLY#============================================================================================================ ; __GoogleWeather_CelsiusToFahrenheit ......; Converts from Celsius to Fahrenheit ; __GoogleWeather_FahrenheitToCelsius ......; Converts from Fahrenheit to Celsius ; __GoogleWeather_GetIPLocation ............; Retrieves the IP City location ; =============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name ..........: _GoogleWeather_Current ; Description ...: Retrieves details about the current weather status in a specific region. ; Syntax ........: _GoogleWeather_Current([$sRead = -1]) ; Parameters ....: $sRead - [optional] Only data returned from _GoogleWeather_Get(). Default is IP location. ; Return values .: Success - Returns a 1D Array. $aArray[6] = [Number of Items, Condition data, Temp in F, Temp in C, Icon URL, Wind condition] ; Failure - Returns -1 & sets @error to non-zero ; Author ........: guinness ; Example .......: Yes ; =============================================================================================================================== Func _GoogleWeather_Current($sRead = -1) Local $aXML[7] = [6, "condition data", _ "temp_f data", _ "temp_c data", _ "humidity data", _ "icon data", _ "wind_condition data"] Local $sConditions = "current_conditions", $sReturn If $sRead = -1 Then $sRead = _GoogleWeather_Get() EndIf If @error Then Return SetError(1, 0, -1) EndIf $sReturn = StringRegExp($sRead, "<(?i)" & $sConditions & ">(.*?)</(?i)" & $sConditions & ">", 3) If @error Then Return SetError(1, 0, -1) EndIf $sRead = $sReturn[0] For $A = 1 To $aXML[0] $sReturn = StringRegExp($sRead, '(?i)<' & $aXML[$A] & '="(.*?)"/>', 3) If @error Then Return SetError(1, 0, 0) EndIf If $aXML[$A] = $aXML[2] Then $sReturn[0] = $sReturn[0] & ChrW(176) & "F" EndIf If $aXML[$A] = $aXML[3] Then $sReturn[0] = $sReturn[0] & ChrW(176) & "C" EndIf If $aXML[$A] = $aXML[5] Then $sReturn[0] = "https://www.google.com" & $sReturn[0] EndIf $aXML[$A] = $sReturn[0] Next If @error = 0 Then Return $aXML EndIf Return SetError(1, 0, -1) EndFunc ;==>_GoogleWeather_Current ; #FUNCTION# ==================================================================================================================== ; Name ..........: _GoogleWeather_Forecast ; Description ...: Retrieves details about the 4 day forecast for a specific region. ; Syntax ........: _GoogleWeather_Forecast([$sRead = -1[, $iTemperature = 1]]) ; Parameters ....: $sRead - [optional] Only data returned from _GoogleWeather_Get(). Default is IP location. ; $iTemperature - [optional] Use Fahrenheit (0) or Celsius (1). Default is 1. ; Return values .: Success - Returns a 2D Array. $aArray[5][5] = $aArray[0][0] - Number of Items ; [0][1] - Number Of Columns ; ; $Array[A][0] - Day of the week ; [A][1] - Temp high in C/F ; [A][2] - Temp low in C/F ; [A][3] - Icon URL ; [A][4] - Condition data ; Failure - Returns -1 & sets @error to non-zero ; Author ........: guinness ; Example .......: Yes ; =============================================================================================================================== Func _GoogleWeather_Forecast($sRead = -1, $iTemperature = 1) Local $aXML[5] = ["day_of_week data", _ "high data", _ "low data", _ "icon data", _ "condition data"] Local $iSize, $sConditions = "forecast_conditions", $sReturn, $sReturnTest If $sRead = -1 Then $sRead = _GoogleWeather_Get() EndIf If @error Then Return SetError(1, 0, -1) EndIf $sReturn = StringRegExp($sRead, "<(?i)" & $sConditions & ">(.*?)</(?i)" & $sConditions & ">", 3) $iSize = UBound($sReturn, 1) Local $aReturn[$iSize + 1][5] = [[$iSize, 5]] For $A = 0 To $iSize - 1 $sRead = $sReturn[$A] For $B = 0 To $aReturn[0][1] - 1 $sReturnTest = StringRegExp($sRead, '(?i)<' & $aXML[$B] & '="(.*?)"/>', 3) If $aXML[$B] = $aXML[1] Or $aXML[$B] = $aXML[2] Then Switch $iTemperature Case 0 $sReturnTest[0] = $sReturnTest[0] & ChrW(176) & "F" Case 1 $sReturnTest[0] = __GoogleWeather_FahrenheitToCelsius($sReturnTest[0]) EndSwitch EndIf If $aXML[$B] = $aXML[3] Then $sReturnTest[0] = "https://www.google.com" & $sReturnTest[0] EndIf $aReturn[$A + 1][$B] = $sReturnTest[0] Next Next If @error = 0 Then Return $aReturn EndIf Return SetError(1, 0, -1) EndFunc ;==>_GoogleWeather_Forecast ; #FUNCTION# ==================================================================================================================== ; Name ..........: _GoogleWeather_Get ; Description ...: Retrieves all data that will be parsed from the Google Weather API. ; Syntax ........: _GoogleWeather_Get([$sLocation = -1]) ; Parameters ....: $sLocation - [optional] Specify a location to retrieve the weather information for. Default is IP location. ; Return values .: Success - Returns data retrieved from the Google Weather API. ; Failure - Returns -1 & sets @error to non-zero ; Author ........: guinness ; Example .......: Yes ; =============================================================================================================================== Func _GoogleWeather_Get($sLocation = -1) Local $bRead, $sRead If $sLocation = -1 Then $sLocation = __GoogleWeather_GetIPLocation() EndIf $bRead = InetRead("https://www.google.com/ig/api?weather=" & $sLocation) If @error Then Return SetError(1, 0, -1) EndIf $sRead = BinaryToString($bRead) Return $sRead EndFunc ;==>_GoogleWeather_Get ; #FUNCTION# ==================================================================================================================== ; Name ..........: _GoogleWeather_Information ; Description ...: Retrieves details about the forecast information. ; Syntax ........: _GoogleWeather_Information([$sRead = -1]) ; Parameters ....: $sRead - [optional] Only data returned from _GoogleWeather_Get(). Default is IP location. ; Return values .: Success - Returns a 1D Array. $aArray[6] = [Number of Items, City data, Postal data, Forecast data, Current time, Unit system] ; Failure - Returns -1 & sets @error to non-zero ; Author ........: guinness ; Example .......: Yes ; =============================================================================================================================== Func _GoogleWeather_Information($sRead = -1) Local $aXML[6] = [5, "city data", _ "postal_code data", _ "forecast_date data", _ "current_date_time data", _ "unit_system data"] Local $sReturn If $sRead = -1 Then $sRead = _GoogleWeather_Get() EndIf If @error Then Return SetError(1, 0, -1) EndIf For $A = 1 To $aXML[0] $sReturn = StringRegExp($sRead, '(?i)<' & $aXML[$A] & '="(.*?)"/>', 3) If @error Then Return SetError(1, 0, 0) EndIf $aXML[$A] = $sReturn[0] Next If @error = 0 Then Return $aXML EndIf Return SetError(1, 0, -1) EndFunc ;==>_GoogleWeather_Information ; #INTERNAL_USE_ONLY#============================================================================================================ Func __GoogleWeather_CelsiusToFahrenheit($iTemperature) Return Round($iTemperature * (9 / 5) + 32) & ChrW(176) & "F" EndFunc ;==>__GoogleWeather_CelsiusToFahrenheit Func __GoogleWeather_FahrenheitToCelsius($iTemperature) Return Round(($iTemperature - 32) * (5 / 9)) & ChrW(176) & "C" EndFunc ;==>__GoogleWeather_FahrenheitToCelsius Func __GoogleWeather_GetIPLocation() Local $aXML[5] = [4], $bRead, $sRead, $sReturn $bRead = InetRead("http://ip.xxoo.net/") $sRead = BinaryToString($bRead) $sReturn = StringRegExp($sRead, '(?s)(?i)<B>IP Address:</B>(.*?)<script type="text/javascript">', 3) If @error Then Return SetError(1, 0, -1) EndIf $sRead = $sReturn[0] $sReturn = StringRegExp($sRead, '(?s)(?i)<div align="left">(.*?)</div>', 3) If @error Then Return SetError(1, 0, -1) EndIf $sRead = $sReturn For $A = 1 To $aXML[0] If $A - 1 = 1 Then $sReturn = StringRegExp($sRead[$A - 1], '(?s)(?i)A(.*?) ', 3) If @error Then ContinueLoop EndIf $sRead[$A - 1] = $sReturn[0] EndIf $aXML[$A] = $sRead[$A - 1] Next If @error = 0 And $aXML[2] <> "" Then Return $aXML[2] EndIf Return SetError(1, 0, -1) EndFunc ;==>__GoogleWeather_GetIPLocation
Example use of Function:
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #include <Array.au3> ; Required only for _ArrayDisplay(), but not the UDF. #include "_GoogleWeather.au3" ; Include the _GoogleWeather.au3 UDF. Example_2() Func Example_2() ; Get the weather information for New York. Local $sLocation = _GoogleWeather_Get("New York") ; Retrieve information for the current weather, passing the data returned by _GoogleWeather_Get. Local $aReturn = _GoogleWeather_Current($sLocation) _ArrayDisplay($aReturn, "_GoogleWeather_Current()") ; Retrieve the forecast for the next 4 days, passing the data returned by _GoogleWeather_Get. $aReturn = _GoogleWeather_Forecast($sLocation) _ArrayDisplay($aReturn, "_GoogleWeather_Forecast()") ; Retrieve general information about the current location and forecast, passing the data returned by _GoogleWeather_Get. $aReturn = _GoogleWeather_Information($sLocation) _ArrayDisplay($aReturn, "_GoogleWeather_Information()") EndFunc ;==>Example_2
All of the above has been included in a ZIP file.
Previous downloads: 125
Edited by guinness, 08 October 2012 - 04:07 PM.






