How can I important Yahoo weather RSS Feed into LiveCode?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
archer2009BUSknbj
Posts: 72
Joined: Sat Apr 06, 2013 8:09 am
Location: London

How can I important Yahoo weather RSS Feed into LiveCode?

Post by archer2009BUSknbj » Fri Apr 26, 2013 7:36 pm

I'm looking at creating a weather App for my Android mobile phone and want to use Yahoo Weather

I looked up some LiveCode lesson on RSS Feeds but the downloadable example code didn't do anything.
LiveCode Lessons » Features, concepts and aspects of LiveCode » Intermediate: RSS Feeds

So I can't really learn anything from that lesson as the example file given doesn't work (RSS.livecode).

This is the kind of output I get from the Yahoo Weather RSS Feed - but I don't actually know how to go about extracting just a few of those things and putting that into variables that I can display.

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

Code: Select all

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<rss xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" version="2.0">
<channel>
<title>Yahoo! Weather - London, GB</title>
<link>
http://us.rd.yahoo.com/dailynews/rss/weather/London__GB/*http://weather.yahoo.com/forecast/UKXX0085_c.html
</link>
<description>Yahoo! Weather for London, GB</description>
<language>en-us</language>
<lastBuildDate>Fri, 26 Apr 2013 6:49 pm BST</lastBuildDate>
<ttl>60</ttl>
<yweather:location city="London" region="" country="United Kingdom"/>
<yweather:units temperature="C" distance="km" pressure="mb" speed="km/h"/>
<yweather:wind chill="11" direction="320" speed="16.09"/>
<yweather:atmosphere humidity="35" visibility="9.99" pressure="982.05" rising="0"/>
<yweather:astronomy sunrise="5:42 am" sunset="8:12 pm"/>
<image>
<title>Yahoo! Weather</title>
<width>142</width>
<height>18</height>
<link>http://weather.yahoo.com</link>
<url>
http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif
</url>
</image>
<item>
<title>Conditions for London, GB at 6:49 pm BST</title>
<geo:lat>51.51</geo:lat>
<geo:long>-0.13</geo:long>
<link>
http://us.rd.yahoo.com/dailynews/rss/weather/London__GB/*http://weather.yahoo.com/forecast/UKXX0085_c.html
</link>
<pubDate>Fri, 26 Apr 2013 6:49 pm BST</pubDate>
<yweather:condition text="Partly Cloudy" code="30" temp="11" date="Fri, 26 Apr 2013 6:49 pm BST"/>
<description>
<![CDATA[
<img src="http://l.yimg.com/a/i/us/we/52/30.gif"/><br /> <b>Current Conditions:</b><br /> Partly Cloudy, 11 C<BR /> <BR /><b>Forecast:</b><BR /> Fri - Showers Early. High: 12 Low: 2<br /> Sat - PM Showers. High: 12 Low: 1<br /> <br /> <a href="http://us.rd.yahoo.com/dailynews/rss/weather/London__GB/*http://weather.yahoo.com/forecast/UKXX0085_c.html">Full Forecast at Yahoo! Weather</a><BR/><BR/> (provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>
]]>
</description>
<yweather:forecast day="Fri" date="26 Apr 2013" low="2" high="12" text="Showers Early" code="45"/>
<yweather:forecast day="Sat" date="27 Apr 2013" low="1" high="12" text="PM Showers" code="39"/>
<guid isPermaLink="false">UKXX0085_2013_04_27_7_00_BST</guid>
</item>
</channel>
</rss>
<!--
 api5.weather.ch1.yahoo.com Fri Apr 26 18:27:13 PST 2013 
-->

All I need is an example of how to take the temperature for example and display that on a Label.
Like an example where there is just a single button and a single label and when I push the button it goes to the internet and retrieves the latest temperature and displays that on the label.

Once I'm able to see a single example that works then I can usually figure things out for myself :D

CoffeeCone
Posts: 64
Joined: Fri Apr 26, 2013 8:38 am

Re: How can I important Yahoo weather RSS Feed into LiveCode

Post by CoffeeCone » Fri Apr 26, 2013 11:27 pm

Something like this? I made it based on what I read in the Dictionary.
Attachments
rss test.livecode.zip
RSS Test
(2.04 KiB) Downloaded 382 times
Learning LiveCode, one step at a time.

archer2009BUSknbj
Posts: 72
Joined: Sat Apr 06, 2013 8:09 am
Location: London

Re: How can I important Yahoo weather RSS Feed into LiveCode

Post by archer2009BUSknbj » Sat Apr 27, 2013 7:35 am

CoffeeCone wrote:Something like this? I made it based on what I read in the Dictionary.
ThankYou, I just ran that and it works perfectly.. I also just had a look at the Code and I can follow how most of it works :D

This is so much better an example then the RSS Lesson which for me didn't even run. :D

CoffeeCone
Posts: 64
Joined: Fri Apr 26, 2013 8:38 am

Re: How can I important Yahoo weather RSS Feed into LiveCode

Post by CoffeeCone » Sat Apr 27, 2013 10:45 am

archer2009BUSknbj wrote:
CoffeeCone wrote:Something like this? I made it based on what I read in the Dictionary.
ThankYou, I just ran that and it works perfectly.. I also just had a look at the Code and I can follow how most of it works :D

This is so much better an example then the RSS Lesson which for me didn't even run. :D

You're welcome. It's the first time I made one too xDI just used the XML section in the Dictionary.
Learning LiveCode, one step at a time.

archer2009BUSknbj
Posts: 72
Joined: Sat Apr 06, 2013 8:09 am
Location: London

Re: How can I important Yahoo weather RSS Feed into LiveCode

Post by archer2009BUSknbj » Sat Apr 27, 2013 10:53 am

I think I spotted a slight error in the code - every time I ran the script it said the temperature was 3C in London which didn't seem quite right

So when I looked at the code it seems to be giving the wind Chill factor as the outdoor temperature.

I thought it was an easy fix, so just created a new box to display the temperature. But keep getting an error

Code: Select all

put revXMLAttribute( tRSS, "rss/channel/condition", "temp") && "°" & revXMLAttribute( tRSS, "rss/channel/units", "temperature") into field "temperature"
put revXMLAttribute( tRSS, "rss/channel/wind", "chill") && "°" & revXMLAttribute( tRSS, "rss/channel/units", "temperature") into field "windchill"
WindChill works perfectly but Temperature gives the following error xmlerr, can't find element C

Not sure what I'm doing wrong. Full code below thanks to CoffeCone

Code: Select all

on mouseUp
   local tRSS
   
   disable me
   
   get url "http://weather.yahooapis.com/forecastrss?w=44418&u=c"
   put revCreateXMLTree( it, true, true, false) into tRSS
   
   if tRSS begins with "xmlerr" then
      answer warning tRSS titled "Error"
   else
      put revXMLNodeContents( tRSS, "rss/channel/title") into field "title"
      put revXMLNodeContents( tRSS, "rss/channel/link") into field "link"
      put revXMLNodeContents( tRSS, "rss/channel/description") into field "desc"
      put revXMLAttribute( tRSS, "rss/channel/location", "city") & ", " & revXMLAttribute( tRSS, "rss/channel/location", "country") into field "loc"
      put revXMLAttribute( tRSS, "rss/channel/condition", "temp") && "°" & revXMLAttribute( tRSS, "rss/channel/units", "temperature") into field "temperature"
      put revXMLAttribute( tRSS, "rss/channel/wind", "chill") && "°" & revXMLAttribute( tRSS, "rss/channel/units", "temperature") into field "windchill"
      revDeleteXMLTree tRSS
      
      answer info "Got the RSS!"
   end if
   
   enable me
end mouseUp

CoffeeCone
Posts: 64
Joined: Fri Apr 26, 2013 8:38 am

Re: How can I important Yahoo weather RSS Feed into LiveCode

Post by CoffeeCone » Sat Apr 27, 2013 11:12 am

Try replacing "rss/channel/condition" with "rss/channel/item/condition".

That's because <yweather:condition> is under <item>
Learning LiveCode, one step at a time.

archer2009BUSknbj
Posts: 72
Joined: Sat Apr 06, 2013 8:09 am
Location: London

Re: How can I important Yahoo weather RSS Feed into LiveCode

Post by archer2009BUSknbj » Sat Apr 27, 2013 11:14 am

CoffeeCone wrote:Try replacing "rss/channel/condition" with "rss/channel/item/condition".

That's because <yweather:condition> is under <item>
Perfect that worked :D .. ThankYou so much.

archer2009BUSknbj
Posts: 72
Joined: Sat Apr 06, 2013 8:09 am
Location: London

Re: How can I important Yahoo weather RSS Feed into LiveCode

Post by archer2009BUSknbj » Sat Apr 27, 2013 10:59 pm

I've managed to get every single field working and displaying on the screen now except the two for Lat and Long

Code: Select all

<geo:lat>51.51</geo:lat>
<geo:long>-0.13</geo:long>
I'm not sure what I'm doing wrong for those two. Neither of these work

Code: Select all

      put revXMLAttribute( tRSS, "rss/channel/geo", "lat") into field "lat"
      put revXMLAttribute( tRSS, "rss/channel/item/geo", "long") into field "long"

CoffeeCone
Posts: 64
Joined: Fri Apr 26, 2013 8:38 am

Re: How can I important Yahoo weather RSS Feed into LiveCode

Post by CoffeeCone » Sat Apr 27, 2013 11:06 pm

archer2009BUSknbj wrote:I've managed to get every single field working and displaying on the screen now except the two for Lat and Long

Code: Select all

<geo:lat>51.51</geo:lat>
<geo:long>-0.13</geo:long>
I'm not sure what I'm doing wrong for those two. Neither of these work

Code: Select all

      put revXMLAttribute( tRSS, "rss/channel/geo", "lat") into field "lat"
      put revXMLAttribute( tRSS, "rss/channel/item/geo", "long") into field "long"
I think you need to do it like this

Code: Select all

put revXMLNodeContents( tRSS, "rss/channel/item/lat") into field "lat"
put revXMLNodeContents( tRSS, "rss/channel/item/long") into field "long"
geo: is a namespace just like yweather: so I ignore it. lat and long are the node names. :)
Learning LiveCode, one step at a time.

archer2009BUSknbj
Posts: 72
Joined: Sat Apr 06, 2013 8:09 am
Location: London

Re: How can I important Yahoo weather RSS Feed into LiveCode

Post by archer2009BUSknbj » Sun Apr 28, 2013 3:20 am

Thanks again that worked just fine

This might seem like a really dumb question but I'm stuck on something which should be so simple

I've now gotten everything to display perfectly, now I'm trying to do something useful with the data

I'm trying to put some key values into variables like Temperature, Code

and then use if statements to do things like if Temperature > 7 Then display Message A else display Message B

The bit I'm finding hard is simply trying to declare a variable I can use on the entire card with other buttons and put the data into the variable

I'm not sure if I should be using the local command or global and where to declare those variables,on the card, outside of the button handler?

I'm reading the official RunRev LiveCode user manual and it's so complicated.

I've created buttons to pull the weather data from different cities all over the world so I can see what happens with different Temperatures, etc

But I can't believe I'm finding it so hard to simply create a few variables and put some basic values into it.

CoffeeCone
Posts: 64
Joined: Fri Apr 26, 2013 8:38 am

Re: How can I important Yahoo weather RSS Feed into LiveCode

Post by CoffeeCone » Sun Apr 28, 2013 7:40 am

archer2009BUSknbj wrote:Thanks again that worked just fine

This might seem like a really dumb question but I'm stuck on something which should be so simple

I've now gotten everything to display perfectly, now I'm trying to do something useful with the data

I'm trying to put some key values into variables like Temperature, Code

and then use if statements to do things like if Temperature > 7 Then display Message A else display Message B

The bit I'm finding hard is simply trying to declare a variable I can use on the entire card with other buttons and put the data into the variable

I'm not sure if I should be using the local command or global and where to declare those variables,on the card, outside of the button handler?

I'm reading the official RunRev LiveCode user manual and it's so complicated.

I've created buttons to pull the weather data from different cities all over the world so I can see what happens with different Temperatures, etc

But I can't believe I'm finding it so hard to simply create a few variables and put some basic values into it.
Yeah I had trouble with variables at first too. You have to declare a global variable. You need to declare it on every script that you want to use it in. So for example, if you have the button "get info", inside it you do something like this:

Code: Select all

global gMyVariable

on mouseUp
  // your code
end mouseUp
So if you want to place variables into the global variable gMyVariable, you use this in the mouseUp handler:

Code: Select all

put "somoe value" into gMyVariable
Now if, say, you want to use that same value in another button or card, let's say you have button "another button", you also have to declare the global variable in the topmost part of the script for that object. Like so:

Code: Select all

global gMyVariable

on mouseUp
  answer info "This is another button"
end mouseUp
The value of gMyVariable is now shared betwen the two buttons. If you need to use it on other objects or cards, you just do the same thing.

I suggest you go to Preferences > Script Editor and then put a check mark on Strict Compilation Mode. This will require you to declare local and global variables so you don't accidentally use a misspelled variable name and have a hard time figuring out where you went wrong. Declaring local variables are the same as global, but instead of placing it outside of the handler, you place it in.

Code: Select all

global gMyGlobalVariable1,gMyGlobalVariable2

on mouseUp
  local tMyLocalVariable1,tMyLocalVariable2

  // code here
end mouseUp
If you may notice, I place "g" in front of my global variable names to easily identify between each other. The reason I use "t" in local variables is because it is only temporary in that, once the handler (mouseUp) is finished using that variable, it's no longer available and is discarded so you declare it everytime you use a handler. You can use local variables with the same name in different handlers and their value will still not overlap with each other.

Of course, these are all just things I do for uniformity's sake. You can name your global and local variables however you want. I hope this isn't too long of an explanation. xD
Learning LiveCode, one step at a time.

archer2009BUSknbj
Posts: 72
Joined: Sat Apr 06, 2013 8:09 am
Location: London

Re: How can I important Yahoo weather RSS Feed into LiveCode

Post by archer2009BUSknbj » Fri May 03, 2013 10:19 pm

Anyway I have to say thank you very much for all your help.

I managed to create something which was able to read the weather data reliably, and do something with that data. That in itself is something I'd have struggled to do with any other kind of mobile phone app creation system.

It was nice to create an App that actually ran on my mobile phone.
I think my next focus will be to try and figure out if it's at all possible to create Mobile Phone Apps that display well across a range of mobile phone screens.
I'm guessing with LiveCode 6 this is beyond my programming ability. I might have to wait for LiveCode 7 to come out.

I can see how useful LiveCode can be to create Desktop Apps, which you interact with using a mouse and keyboard.

I'm just not sure if it will ever translate to a delightful mobile phone experience. I tried using MobGUI but I don't think it worked that well for me.
I might just have to wait until LiveCode has responsive design included as standard, although I'm not sure how that's even possible when everything is designed on cards.

I've designed several Wordpress websites using various themes which would display responsively on a Mobile device, just not sure how the same effect would work in LiveCode.

If there are any great examples of Android Apps designed on LiveCode that are able to adapt to different screen sizes, I'd love to see them.

Anyway once again thank you so much for your help.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: How can I important Yahoo weather RSS Feed into LiveCode

Post by Simon » Fri May 03, 2013 10:26 pm

Here is my "quick" way:
http://forums.runrev.com/phpBB2/viewtop ... =8&t=14859
The links at the bottom show you much more advanced ways of currently handling differing resolution.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”