HTTP Request: Request google weather api

From bab-tec.de wiki

Jump to: navigation, search

With a request to the google weather data server ("Google weather API") the eibPort gets an XML-file as response. The response is then filtered by the regular expression and the values are handed over by the outputs objects to the visualisation. For the request the following settings are necessary:

Protokoll: http://
Base-URL: www.google.com
Request Type: GET
GET-URL / Post Data: /ig/api?weather=Dortmund

Note: It is important to ensure, that the response of the request is made in the correct language. Is for example "google.de" used instead of "google.com" the response is given in german language.

With the city name behind "weather=" it is defined from which place the weather data is collected. Theoretically it is been possible to change this value dynamically by the help of the value objects.

If the URL of this request is been copied into the browser line and is requested, the response will be shown as an XML-file in the browser window. In our example we want to give out the current weather conditions, the current temperature in celsius, the current humidity and the maximum temperature for the next day. At first we create the expression for every single output, after this we fit them together.

Note: The expressions which are created are best verified with a tool. There are different tools available online, for example: http://dev.radsoftware.com.au/downloads /Rad.RegexDesigner.Setup.1.4.exe. Thereby it is to be considered that some of the flags have another meaning (e.g. Dot All = Singleline).

Important: In Mozilla Firefox it might be that you have a space sign behind the expression string when you copy it directly into the array of the job. Is this the case the expression won't work, so please check out exactly what is written in your expression array.


Current Weather Conditions:
The current weather data is written in the part "<current_conditions>". After it the information "<condition data=" is following. However this is repeated three times in the information "<forecast conditions>". To avoid that the expression is matching on all "condition data=" the string "<current_conditions>" must be prepent.

<current_conditions>.*?<condition data="([a-zA-ZäÄöÖüÜ\s]+)"

In order to have success with this expression there must be placed a ".*?" (dot - asterik - question mark) between the proper expression and the string "<current_conditions>" with the flag "DotAll" enabled. The background is, that this two strings are placed in different lines. By the help of the flag "DotAll" the expression is also taking care of the line ends (otherwise it would try to match line by line) and with the ".*?" the behaviour of the expression is "ungreedy".

This is the first group matching, thats why the output opbject 1 is defined as EIS 15 text.


Current temperature in celsius
The output of the current temperature is written behind the string "<temp_c data=". This appears only one time in the whole text. According to that the expression is:

temp_c data="([\-\+]?[0-9]+)

There must no flag be enabled. Because this is an temperature value the output object is been formatted in EIS 5. The data configuration is "ASCII – Unsigned long deicmal".


Current Humidity
The output of the current humidity is following behind the string "Humidity:". Should the the response be given in german language at this place is written "Feuchtigkeit" and the expression is not working (equally with other languages).

Humidity: ([0-9]+)

No flag is enabled for this. This is an percent value, thats why the output object is formatted EIS 6. The data configuration is "ASCII – Unsigned long deicmal".


Maximum temperature of the next day
The forecast for the upcoming days can be found in the section "<forecast_conditions>". There exists an information "high data=" in which the maximum temperature of the respective day is been given out. Certainly this section is existing altogether four times, for each of the following four days. In the recent case we are only interessested into the temperature of tomorrow, that means the first entry of "high data=".

high data="([\-\+]?[0-9]+)

All flags are disabled. This expression is giving out the first entry of altogether 4 matches. As it is a temperature value the output object is again formatted as EIS 5. The data configuration is "ASCII – Unsigned long deicmal".


All outputs in one regular expression
For the reason that 4 outputs objects can be used in one job, it is possible to filter out all above mentioned information with one regular expression and place them by help of the group configuration into the four objects. Thereby it is to be considered that the flag "DotAll" is been enabled, because the expression has to detect end of line. Between the different expression a ".*" is placed each.

<current_conditions>.*?<condition data="([a-zA-ZäÄöÖüÜ\s]+)".*temp_c data="([\-\+]?[0-9]+).*Humidity: ([0-9]+).*?high data="([\-\+]?[0-9]+)

Note: Between the expression for the humidity and the maximum temperature beside the ".*" a question mark must be placed to get the expression "ungreedy".

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox