So now that you've learned how to connect to and work with the Twitter API this challenge is going to
be an opportunity to put that into practice.
It's not a particularly complicated one but it will require you to work through the Twitter documentation
for its API and to process the results that come out of it.
Like with the previous challenge I'm not going to show you my output in advance so you have to figure
out how you're going to make a Twitter client look essentially the project that we're going to try to
create is pretty simple.
I want a twitter client that displays the tweets on my timeline but I only want to see the ones that
have been favorite it at least twice.
My aim for this of course is to filter out lots of tweets that I might not be interested in and just
say the most popular ones.
So that's the challenge.
Just before I set you off to do that might be worth having a quick google of Twitter API endpoints.
So an end point is essentially you Arel where you can access Twitter like
status update status is home timeline etc. and once youve done that Google will take you to this page
and you can see we've got all the different you are else here to do all sorts of different things to
do with the Twitter system so that page you'll probably need.
The other thing you'll probably want to do if you've just created a Twitter account is to follow a bunch
of people.
So I'm just going to follow whoever is suggested here.
You'll get a different set of suggestions of course but that should give me a bit of a time line to
work with.
Without that then there won't be much to choose between.
All right.
So that's the challenge of ego.
And best of luck.
All right.
Well the first thing that we're going to need is to see how to get the users timeline that is the set
of tweets that will appear on the user's home page.
So if we have a look down at the various options within the Twitter API.
So if we look back to our code you can see that the API endpoint we used to get the user's tweets with
status slash underscore timeline.
So let's have a quick look.
About one and see what it does.
So home timeline returns a collection of the most recent tweets and retreats posted by the authenticated
user and the users that they follow.
So that's actually what we need to get the tweets themselves.
So if we now have a look at that because we've followed a bunch of people we should have a lot more
status on there than we had before.
So I'm just going to get rid of the instruction to post because we don't want to post that every time
we run our script.
And yep definitely looks like we've got a lot more information than we had before.
So let's have a look.
Wonderful.
Yep so we're definitely getting some tweets from various people there.
Bodleian libraries etc..
Wonderful.
And you can see we've got this very important variable favorite account.
So that's obviously what we're going to be using to tell whether or not a tweet is important enough
for us to feature it in our client.
So let's just tidy things up a little bit and then we'll start processing this.
So first off we're going to want to loop through the array that we've got here so you can see we've
got this zero.
And then all the information for that particular tweet.
And then after all of that we've got a 1 and then the next tweet and then at c et cetera.
So we want to loop 3 array.
So let's do that.
First off we'll do that with a for each
status is as we don't actually need the number.
So I'm just going to the status is as
and then for now we'll just print the tweets.
And so that should give us something pretty similar but it won't have the array at the top because we're
just printing the content.
All right.
So let's try and get the text of each tweet for the moment now you might guess that to get the text
you would do something like
text like that because that's how we normally access an array.
Aha.
But something's gone wrong here.
This is a type SDD class and we're trying to access it like an array.
So we need to use a different formatting because this isn't quite an array.
It's actually an object which works slightly differently.
But never fear.
Let's do a quick google and figure out how we would do it.
So access elements in standard class object BHP was what I was after that they were going to access
standard class object after a specific key value pair.
Let's have a look.
Ha.
So to access that data we actually use an arrow or a dash and a greater than sign and then the name
of the variable that we want.
So again that's because these are different sorts of variables to arrays.
So we have to use a different syntax to extract the data that we want.
So let's give that a try.
We want to get the text and then I'll just put in a line break as well as one after the other.
And I'm also going to open that and I knew tab so that we keep the open open as well.
And there we get.
So we don't actually need to view source because I put a B R in there.
So now we've got the text of each of those tweets and you can see we've got my two there as well.
And then a bunch of other ones.
All right.
So now is the time to work out how many favorites each one has and then only display the ones that have
two or more.
And if we scroll down you can see we've got a favorite count here but that's actually the favorite count
for the user not for that particular tweet.
So that's not what we want.
We keep on going.
Then we've got the favorite counts right there.
Wonderful.
So that's what we need.
Wonderful.
So that's what we need.
So instead of tweet text we'll use tweet and favorite count.
Let's just have a look and check that's returning.
The number that we're after.
Wonderful.
There we go.
So we've got a whole range that I want to have that exciting tweet is.
Let's have a look.
So we want to check to see if they tweet
favorite.
Count is greater than cruelty to then
will carry.
We actually need to use print this time.
It's just a string favorite count
and next to that we'll put the tweet text as well.
And then we'll move a line break into the if statement.
And look something has gone horribly wrong.
Oh sadly I can't spell favorite though I want to.
Fair enough.
There we go.
So now we've got our favorite count and our tweets.
Text if you're interested.
That very popular one was recovered from the Brit Awards.
And then there's another surprisingly popular one about Grimsby as well.
So this is pretty good but it's not the sort of thing I would want to look at every day.
It's not particularly pretty and it doesn't even tell me who made the tweet so I wonder if there's a
way I can display a single tweet.
Nicely so I've cheated slightly because it's not the most obvious one but I know that status is embedded
is what we need there.
So this returns a single tweet specified by the bureau or the ID in an embed format.
So as an HD snippet we can include directly on our page.
So we're going to need to pass that the ID of our tweet and we do that in the ID variable.
So that should be reasonably straightforward.
Let's do that here.
So we get the tweet Id like that and then I'm going to copy
that page that will change this to just status.
And then instead of status slash timeline we want to use status is slash Oh and bed and then we want
to give an ID that I that ID is going to be tweet ID and we want to send this exclude replies variable
and then I'll just close the square bracket that and because we don't know quite what we're going to
get.
Let's print the whole thing.
OK.
Let's take a look.
It's looking good and you can see we're actually getting nice HDMI all of all of these tweets.
Now that looks a lot more pleasant.
So let's have a look.
Where do we get that HDMI out.
It's in the HDMI Melford makes sense.
So let's echo that instead.
So it's going to be status IRA or HCM el.
Let's take a look.
Take a little while to encode them.
But there we go.
So all in a nice pretty HD MLF I format's
and that is only the ones with more than one favorite.
So that's as far as we go.
So that's as far as I'm going to go with this Twitter client.
You're welcome to make it pretty.
Put a nice background and the nav bar up the top if you want but we've done that enough times now for
you to know how to do it.
And it also brings us to the end of the API section of the course.
So we've learned how to get the weather how to access Google maps using our API key and how to log into
the Twitter API file.
So there's three different types of API but all of them very powerful and actually pretty easy to use
once you know how.
So I hope you enjoy that and I hope you're getting a lot of exciting ideas of how you could use these
powerful web sites in your web sites and web apps and we're going to be moving on to see how we can
make actual apps for the App Store and Google Play Store purely out of HD.
We'll see how to do that in the next section.