In the previous lecture, we learned how to work with the Twitter API.
To close out this section, you're going to apply your Twitter API skills and also your ability to look
through the documentation and code samples to complete the section project.
It's not incredibly complicated, but it will allow you to apply what you've learned and to make something
interesting.
What you're going to do is you're going to make a basic Twitter client that displays the tweets from
your timeline, but only those that have been favorited at least 100 times or whatever number you pick.
It depends on who you follow and how popular their tweets are.
You may even want to follow a few popular accounts in areas that interest you so that you can have some
tweets to work with.
So get ready and pause the video and give this one your best shot.
Come back when you're done or if you need some help.
How did that go for you?
Were you able to complete the project?
Let's look at it together.
So as you can see, I've copied the API's 10.7 folder in its entirety and just renamed it to APIs 10.8,
which I have opened here in via code.
First, let's comment out the posting command right here because we don't want to keep posting that
every time we run it.
And we'll increase the count on the number of statuses retrieved to 25 instead of the two.
If you had it down to two.
And what's going to happen is this thing, the status is actually returns an object.
So we're going to actually just loop through the object and see what we can get.
So let's try this.
We'll say for statuses or rather for each, let's use a for each statuses as tweet.
So each one is an individual tweet.
We're going to just echo the tweets.
Text.
So the tweets text.
All right.
And maybe put a line break at the bottom.
All right.
Let's upload this and give this a test.
So we're going to go over here.
And make sure we're at the correct location.
API is 10.8.
I'm going to drag that over into the C folder.
D three and we're going to give this a run.
So Dev will go to the one from 10.7 and then change that to an eight.
All right.
Doesn't seem to be working.
Well, I refreshed it and it seems like it is actually showing the data.
That's interesting.
So we do have the text from the last 25 tweets on the timeline.
And the next thing we're going to do is we're going to look a little bit at the API reference, especially
at the example response at the bottom.
So let's do that.
We'll open the API and we're under right here under the standard API tweets and then get tweet timelines.
And we can look here and see all kinds of exciting information that we can grab from here.
And you'll notice that some of the data that's returned are the text that we just used.
And the next thing that we do is down here, we've got a description and you've got a lot of other information.
You'll notice that there are favorite count.
That's another thing we might want to use.
And also there should be some information about the user themselves.
So we do see user, but that also is an object itself and that has a name right there.
So we could grab the name or the screen name or whatever we want and then see what that gives us.
So let's do that.
We're going to update this to now echo the favorite count, and we'll concatenate a couple of spaces,
then tweet user.
But we also have to put name at the end.
Otherwise we're not going to get what we expect.
So that looks good so far.
Let's upload it again to.
The host.
There we go.
10.8.
Right.
And now let's go back and see what we get.
Oh, you know, I think I uploaded the wrong.
File.
Let me make sure.
I mean, ten, eight, ten, 8 to 10.
Eight.
Let's try that again.
Oh, there we go.
That's what we wanted.
I think I grabbed the file from ten seven.
But you can see there's just the usernames now, not the actual tweet content.
The text, because I changed that.
But you notice it says the number of tweets and you'll notice several of these are well under 100.
But you do have some more popular ones like from IGN and Barstool Sports and some other stuff as well.
So we might be able to narrow that down and filter it.
So let's filter it based on the number of favorites.
So we're going to put in this for each.
I'm going to say if the tweet favorite count is greater than or equal to 100, then we'll display it.
So I'll cut that and put it in the if statement and let's see what that looks like.
All right.
So we'll go over here, upload it again and refresh.
And it's only showing the ones and this is getting updated even more because any of these accounts that
I follow could be tweeting any moment now.
So there could be changes even over a couple of minutes.
So but we do see that there are very few that are over 100.
Now.
You can even set it to over 1000 or whatever other number you would like to do or under 100 maybe want
to see those from less popular accounts.
You say under five, whatever you would like.
So definitely giving us the info we want, but we can definitely also make this prettier so we could
do it on our own with some nice CSS and decide what we want to display.
Or we could use a nice feature that Twitter offers calls the called the Embed API, which allows us
to embed tweets in our website.
So all we need is the tweet ID and we're going to be using statuses slash embed bed.
I cheated a bit because I have used this feature before, but we could find it under the documentation
and then under Twitter for websites and then embed.
So if we went under the developer website and the developer portal and.
Under the.
Products or rather the sorry the docs.
I said doc products, the documentation.
We can find the Twitter API over here and you've got premium and standard and you'll notice that there
is a section for the Twitter for websites down here.
This gives information about the embed API, so this can tell you all kinds of information about it.
All kinds of different options that we could take advantage of.
So what we're going to do, though, is we'll go back here and inside here with the favorite count.
I'm actually going to change what's inside here and what's being displayed.
So just see what's in it.
What is what comes back when we request a particular tweet ID.
So we'll say connection and we're going to make another get call using the Twitter OAuth API.
Again, statuses slash O embed.
And then I'm going to give it the ID of the tweet that I'm interested in.
So the particular tweet is not surprising, Tweet ID, So we'll do that.
And then once we get that, we're not sure what we're getting back.
So it may not work with Echo properly.
So I'll just say print status notice that I have status and so statuses, make sure you put the singular
or use a different name.
All right, now let's upload this.
And let's see what we get over here.
Are interesting.
And as it's loading it, it's actually rendering this HTML, which is very, very interesting.
So let's view the page source.
And if we go here, you can see that the information is actually returned as an object.
And you can see that the HTML is in a property called HTML.
So not surprising there, but we can make use of that.
And let's do that.
So we're going to go over to our code again.
And this time we're just going to do some echoing.
So I'm just going to do an echo now that we know what the content has in it.
We'll just say echo the HTML and then maybe an extra break below it just to make it a little bit cleaner.
And let's upload and run again.
Now, I'm not expecting you to have found this, but this is just showing you how you might want to
do something a little bit nicer, a little bit prettier without too much work.
And there we go.
We just have a bunch of displayed tweets that we gathered from the API calls.
So it looks really great.
Probably much better than I could have done.
You can even go to the replies and things like that.
So it includes a whole lot of stuff here.
Excellent.
So it looks great.
I hope you're getting a lot of ideas of cool things that you could do with your own websites and APIs,
maybe even the Twitter API.
That brings us to the end of the API section.
I hope you learned a lot and are excited about experimenting with all these new skills.
So it's Dr. Jay signing off for now.
Keep on learning.