Connecting To A Feed
We look at the structure of a Twitter RSS feed, import it into flash and prepare it to be used as a resource for our Twitter translator game.
Video Transcript
00:01In this quickie, we're going to look at how to use RSS feeds to create an application.
00:07So I'm just going to start with a quick sample of an RSS feed that actually is a language RSS feed,
00:13that brings in words in French and English.
00:17And I've built a little small application that you could drag around
00:21and you could highlight, find the word that's connected to thank you.
00:26Thank you. That would be...
00:28Au revoir. Au revoir.
00:30Goodbye.
00:32Hello, goodbye would be bonjour.
00:34So let's grab the bonjour.
00:36And no is no.
00:39So that was our little application.
00:41You could see I was really slow, so my score is zero and I could play it again.
00:45But let's jump in and actually see how to start making that connection
00:50and connecting the pieces together to starting up an application like this.
00:54So to do that, I'm just going to close my slow file
00:58and go to my browser for a minute and show you where I'm getting the RSS feed.
01:02So I've created a new account on Twitter called WordFrench.
01:06And WordFrench will, whenever I learn a few new words in French,
01:10will just upload it to WordFrench,
01:12or you could always call me out with a word and I will add it in
01:17if it'd be more than happy to learn words in French.
01:22So this is the Twitter account, and if you'll notice on the bottom right side,
01:25you'll see the RSS feed for WordFrench tweets.
01:28And if you click on it, you'll see actually the link,
01:31which it's the HTTPS, Twitter, and so on.
01:34But you could get rid of the word feed,
01:36and you could get rid of HTTPS because we don't need it to be secured.
01:40And we could just grab that URL and take it directly into our Flash.
01:45So if we go back into our Flash file,
01:47we're going to see that...
01:50I'm just going to swap to my regular main.
01:54And we'll see that all we have here at this point in time
01:58is just the link to that RSS feed and an empty new blank application.
02:04So the first thing that we'd want to do is actually load that RSS feed.
02:07And because I'm working with CS5,
02:09it's going to automatically import the packages for me
02:12that I am going to be using.
02:14So let's start.
02:15First of all, I want to create a new URL loader,
02:19which will be of a type URL loader.
02:23And notice how the import automatically imports,
02:26so we don't need to worry about that.
02:28And we want to create a new URL loader
02:32and a new URL request.
02:35And send to the URL request our RSS feed.
02:39Now that we have our new URL loader,
02:43we want to make sure that we are listening to our URL loader
02:48for when it completes the load,
02:50which for that we will use the event.complete.
02:54Go up. All uppercase, right?
02:57On RSS load.
03:03Well, let's just keep with the terminology there,
03:06so we'll just call it on RSS complete.
03:10And let's create our little function.
03:12Notice how I didn't import yet my event
03:14because I'm working with CS5.
03:19And I know that in a moment when I continue typing,
03:23I will see when I load in my event,
03:28it will automatically add in the event.
03:31So I did not need to import anything.
03:34So let's see what happened so far.
03:36First of all, before we even do anything,
03:39the first thing that we always want to do
03:41is we want to get rid of the event that we just listened to.
03:44But unfortunately, we didn't really set
03:48a variable, a global variable,
03:50because we knew we were only going to use this once.
03:52And to get rid of this event,
03:54I could just call directly the target
03:57and then remove event listener.
04:00I don't need to save that variable.
04:02I could just directly approach it through the event
04:05because the event dispatcher sends me the target.
04:07And then I could just send the same parameters
04:10that I've set to my add event listener here and done.
04:14We've just removed our event,
04:16making sure that we don't have a memory leak.
04:18And now if we go to our target again and check our data
04:21and just run our app one more time,
04:23we're going to see that we're already getting in our RSS feed.
04:27Now the next item that we want to do
04:30is we want to break this RSS feed down
04:32and kind of see what we actually want out of it.
04:34So if I'll just copy this out
04:36and I'll just throw it into a new file,
04:38I'm just going to create a new class,
04:41just random name, just delete all that
04:43and just paste it in there so we could look
04:45at the RSS feed itself.
04:47So we'll see first we have our channel information
04:49that has our title, our links, and so on.
04:51And then we have items.
04:53Item, item, item, and so on.
04:57So basically we know if we're going to use our E4X,
05:00we could do now, let's see, first of all.
05:04So first before we do anything,
05:06we want to create a new XML file, right?
05:08Private var XML, which is a type of XML.
05:13And we want to set the value inside of our XML
05:16to be an XML with the data value that we've just traced out.
05:22So now we know we have an XML
05:24and now we could start interacting with it.
05:27So first of all, we know we want to go into our channel.
05:30And we could see that up there, so channel.
05:32And we want to go, right now we don't care about all this,
05:35we want to go into our item.
05:38And if we check this out, we're going to see,
05:40first of all, we're going to get an XML list
05:42and it's going to give us all the different items
05:45in our application.
05:46I know it's a list because we could even,
05:48if we wanted to, just check our length.
05:54And we'll see that we have five items so far in our channel.
05:58So we got down to our item and we want to store that
06:02so we could loop through that element.
06:04So I'm just going to create an XML list
06:07and I'm just going to set the value that we had in our trace.
06:17And the next step that we want to do is actually
06:19loop through that XML list and approach the data directly.
06:23Now, if we look at the feed, we're going to see that
06:26in the feed itself, we'll see that all the words
06:28have created a rule, a strict rule,
06:30that every time that there's a French word,
06:32after this combination of letters would happen,
06:36and then the translation into English.
06:39So now, because I know that, we could use that
06:42for our advantage.
06:43So let's see, first of all, let's first get our words.
06:45So if I check, if I'll just create a new four each,
06:55and let's see what would we put inside of our trace now.
06:58So now we want to approach our item directly, right?
07:01And let's look at our item, our item packet has a title
07:06and that's where we want to go into.
07:08So let's type their dot title.
07:10Ooh, a little error here.
07:15We have a little bit too many, or not enough, let's see.
07:22First of all, and we see that we're missing one.
07:26So let's close that bracket.
07:31And here we go, so we have all of our words so far.
07:34And now we have a little problem.
07:35We want to get rid of this intro piece,
07:39and we want to get rid of this element.
07:42We want to break this down into two separate elements.
07:46So to do that, we have a few options.
07:48One, we could grab our word French from our channel itself,
07:51because in the channel, we'll see that in the title channel,
07:54we have our name right here.
07:56Like that, making sure that if we change a channel,
07:58we change a language, we won't have to change anything,
08:01but that URL.
08:03So let's do that.
08:04First let's grab, let's create a new variable here,
08:07and we'll call it user name, which is a string.
08:12And we're going to get the user name from our XML.channel.title.
08:19We're in our channel.title.
08:21And then we want to get rid of all of that.
08:23So I'm just going to copy that.
08:25And the way I'm going to do that,
08:26I'm going to split that string that is going to return.
08:30And then I'm going to join that string,
08:33basically creating just that user name.
