Source language: Translate to:

Collect weather data

General questions about NeoBook

Moderator: Neosoft Support

Collect weather data

Postby smartmedia » Tue Oct 22, 2013 10:39 am

Hi..

I need to collect weather data for 1 or 2 cities from a free service and present it in my app. any ideas and workout. ?
Thanks
User avatar
smartmedia
 
Posts: 889
Joined: Fri Apr 01, 2005 6:50 am
Location: Hellas

Re: Collect wheather data

Postby Gaev » Tue Oct 22, 2013 11:10 am

smartmedia:

The solution/ideas will be determined by the method by which this service makes the data available.

Assuming you request the data via a RESTful url, you could use the WebBrowser object or NeoBook's InternetGet or InternetPost commands.

If the returned data is in JSON/XML format, the WebBrowser object would be the prefered method (unless you can find a plugin that will extract values associated with keys in JSON/XML data).

If this free service has a documented API, please post a reference to their web page.
User avatar
Gaev
 
Posts: 3718
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: Collect weather data

Postby David de Argentina » Tue Oct 22, 2013 11:54 am

Hi SmartMedia,

See:

https://app.box.com/s/ccg034khkkwi86uhay97

Greetings from Buenos Aires,
David de Argentina
User avatar
David de Argentina
 
Posts: 1553
Joined: Mon Apr 04, 2005 4:13 pm
Location: Buenos Aires, Argentina

Re: Collect weather data

Postby smartmedia » Wed Oct 23, 2013 8:05 am

Hi..

Gaev:
I have' found 2-3 sites that offers weather data and most of them are exporting the data in XML. Cause the nature of the application i need those data to be as much accurate as i can get.
I also thinking of using a widget but i it must be optional enough because i need the graphics and text to be big enough for TV screen viewing.
I saw http://www.theweather.com and http://openweathermap.org/api wich gives also xml.

David:
You exe example is something i want but with better big graphics and layout.

The modification of elements that show the weather is a must for me. It will play in a screen of 50 or 60 in.
Thanks for your consideration, I believe npXML plug in will do for this job ?
User avatar
smartmedia
 
Posts: 889
Joined: Fri Apr 01, 2005 6:50 am
Location: Hellas

Re: Collect weather data

Postby Gaev » Wed Oct 23, 2013 10:23 am

smartmedia:

theweather.com website only returns data in XML format ... and they require you to maintain a reference link to their website at all times.

On the other hand, openweathermap.org provides a free service ... and returns data in JSON format as well.

Others may be more experienced in extracting values from XML format, but I find that extracting values for keys within JSON format variables is a lot simpler/easier than from XML format variables.

So you could use the Web Browser object to invoke this url ...
Code: Select all
http://api.openweathermap.org/data/2.5/weather?q=London,uk
... and it would return a result like this ...
Code: Select all
{"coord":{"lon":-0.12574,"lat":51.50853},"sys":{"country":"GB","sunrise":1382510371,"sunset":1382546997},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"base":"global stations","main":{"temp":286.203,"temp_min":286.203,"temp_max":286.203,"pressure":1005.09,"sea_level":1014.99,"grnd_level":1005.09,"humidity":95},"wind":{"speed":6.71,"deg":248.5},"rain":{"3h":0},"clouds":{"all":44},"dt":1382545370,"id":2643743,"name":"London","cod":200}
... then, you can extract the value of the required key into a simple variable string ... and then transfer it to a NeoBook variable.

Which pieces of the returned weather information were you interested in extracting into your NeoBook application ?
User avatar
Gaev
 
Posts: 3718
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: Collect weather data

Postby smartmedia » Wed Oct 23, 2013 12:51 pm

Hi..

temperature - humidity - pressure - wind - sun condition
i believe those are enough
the json look like mesh, how i will get those data ?
User avatar
smartmedia
 
Posts: 889
Joined: Fri Apr 01, 2005 6:50 am
Location: Hellas

Re: Collect weather data

Postby Gaev » Wed Oct 23, 2013 6:07 pm

smartmedia:

1) First, attach this code to a Button object ...
Code: Select all
InternetGet "http://api.openweathermap.org/data/2.5/weather?q=London,uk" "[weather]" ""
alertbox "weather" "[weather]"
.. call javascript function to extract keys
Call "gkExtractWeatherKeys" "[weather]"


2) Second, make a (Javascript) Function file with this code
Code: Select all
//takes JSON object returned by openweathermap.org
//extracts values for specific keys
weather = [%1];
weatherMain = weather.main;
//temperature
weatherTemperature = weatherMain.temp;
nbSetVar("[nbTemperature]", weatherTemperature);
//humidity
weatherHumidity = weatherMain.humidity;
nbSetVar("[nbHumidity]", weatherHumidity);
//pressure
weatherPressure = weatherMain.pressure;
nbSetVar("[nbPressure]", weatherPressure);
//wind
weatherWind = weather.wind;
weatherWindSpeed = weatherWind.speed;
nbSetVar("[nbWindSpeed]", weatherWindSpeed);
... save it as gkExtractWeatherKeys

3) Setup a Text object with this content ...
Code: Select all
Temperature = [nbTemperature]
Humidity = [nbHumidity]
Pressure = [nbPressure]
Wind Speed = [nbWindSpeed]


Finally, run the publication.

If you want to understand the Javascript ... especially how to extract values of specified keys ... do some google research ... as more and more client/server data transfers are using the JSON format ... basically, an object is represented by what is between curly brackets ... inside it, there are key/value pairs separated by the colon character ... finally, values can be objects as well (with their own key/value pairs) ... you extract (to a simple javascript variable) the value for a particular key by referencing it as object.key.
User avatar
Gaev
 
Posts: 3718
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: Collect weather data

Postby smartmedia » Wed Oct 23, 2013 10:39 pm

Hi..

Gaev,
i did it but i receive an error when i run the pub in this line
weather = [%1];
so i don't get any results in the variables.
User avatar
smartmedia
 
Posts: 889
Joined: Fri Apr 01, 2005 6:50 am
Location: Hellas

Re: Collect weather data

Postby David de Argentina » Wed Oct 23, 2013 11:28 pm

Take a look of this:

http://weather.yahooapis.com/forecastrss?w=467005&u=c

Pure XML

Cheers,
David de Argentina
User avatar
David de Argentina
 
Posts: 1553
Joined: Mon Apr 04, 2005 4:13 pm
Location: Buenos Aires, Argentina

Re: Collect weather data

Postby Gaev » Thu Oct 24, 2013 4:38 am

smartmedia:
i did it but i receive an error when i run the pub in this line
weather = [%1];
so I don't get any results in the variables.

1) Did the AlertBox command display the JSON object returned by the website ? ... while repeated testing, I received an Error response one time ... of course, if/when you decide to use this, you have to inject the usual error-checking at all stages of the process.

2) Did you specify in the Function that it expects this one parameter ? ... your file contents should look like ...
Code: Select all
{NeoBook Function}
Version=5.80
Language=JScript
Param=[%1]|Text|WeatherDataObject
{End}
//takes JSON object returned by openweathermap.org
//extracts values for specific keys
weather = [%1];
weatherMain = weather.main;
//temperature
weatherTemperature = weatherMain.temp;
nbSetVar("[nbTemperature]", weatherTemperature);
//humidity
weatherHumidity = weatherMain.humidity;
nbSetVar("[nbHumidity]", weatherHumidity);
//pressure
weatherPressure = weatherMain.pressure;
nbSetVar("[nbPressure]", weatherPressure);
//wind
weatherWind = weather.wind;
weatherWindSpeed = weatherWind.speed;
nbSetVar("[nbWindSpeed]", weatherWindSpeed);


3) What was the error that was reported ?
User avatar
Gaev
 
Posts: 3718
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: Collect weather data

Postby CN_Iceman » Thu Oct 24, 2013 8:00 am

The error is in the line:
Param=[%1]|Text|WeatherDataObject

When you create the function, you must also create the variable [%1].

If you do not know how to do it, edit the file gkExtractWeatherKeys with a text editor and add this line:
Param=[%1]|Text|WeatherDataObject
right after Language=JScript and before {End}.

Now the program will work perfectly.

Thank's to Gaev for the very interesting function.

Greetings.
Greetings/Saludos, Jose.
www.icemansoft.es
User avatar
CN_Iceman
 
Posts: 297
Joined: Tue Mar 01, 2011 11:04 am
Location: España

Re: Collect weather data

Postby smartmedia » Thu Oct 24, 2013 12:22 pm

Hi..

I finally made it work, Param=[%1] was missing. I am still looking to find an accurate service because the data will be visible to thousand people. I also look at codecanyon.net and found some very interesting scripts for that job. Thanks all for your help. If i will have any more question i be back.
User avatar
smartmedia
 
Posts: 889
Joined: Fri Apr 01, 2005 6:50 am
Location: Hellas

Re: Collect weather data

Postby CN_Iceman » Fri Oct 25, 2013 6:13 am

Only for info...

Yesterday this thread give me an idea, so... this is the result.

Image

It's a very, very pre-alpha version, but the idea here is to learn how to handle JSON or XML files, extract data and use them. At first I used the function provided by Gaev, now I'm using my own routines, and it seems to work. I also had to include some routines to convert ASCII text to UTF-8 format, in this way, you can use special characters like accents and Spanish Ñ for example in the search...

I get the data from OpenWeatherMap, but I am also thinking of looking this data in a more precise site. I've been comparing data with some Meteorological Agencies and noticed that there are many differences.

Anyway thanks for the ideas.

Greetings.
Greetings/Saludos, Jose.
www.icemansoft.es
User avatar
CN_Iceman
 
Posts: 297
Joined: Tue Mar 01, 2011 11:04 am
Location: España

Re: Collect weather data

Postby schmutly » Fri Oct 25, 2013 6:24 am

Hi,
i got that same error but the variable seemed to be in there
as well as alias..what am i doing wrong?
Gaev updated his function i can see but still cant get it
working..syntax error on weather = [%1];
..would like to try this function out.
Rob
schmutly
 
Posts: 217
Joined: Fri Feb 20, 2009 4:16 pm

Re: Collect weather data

Postby Gaev » Fri Oct 25, 2013 8:58 am

Rob:
Gaev updated his function i can see but still cant get it working
I did not update the function ... merely posted the way the Function file should look like ... for the benefit of those that are not experienced enough with the creation of Functions.

i got that same error but the variable seemed to be in there
syntax error on weather = [%1];
1) confirm that the Alertbox displays the JSON object.

2)using notepad.exe, open the Function file ... copy its content to the clipboard ... then post it here (inside "quote" tags).
User avatar
Gaev
 
Posts: 3718
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Next

Return to General NeoBook Discussions

Who is online

Users browsing this forum: No registered users and 2 guests