Welcome back.
Let's build our Twitter bot.
The first thing we want to do is import tweet library.
And obviously, this isn't a built in module in Python, so we'll have to install it by saying PIP three.
Install.
TB.
And this is going to install the toy library.
Awesome.
Now this library has really good documentation.
So when we go to read the docs, it'll actually show you, hey, how to get started and give you some
quick start up and setup that we need to do.
In my case, I'm just going to copy the hello to AP code that they provided for us and just paste it
in here.
Now there's a few things happening here.
Obviously, we don't need the double imports.
What we can see here is that we need to authenticate.
That is we use the toI p oauth handler and we have to give it something called a consumer key and a
consumer secret.
And then we also have to set access token with the access token and access token secret.
Now what this is, is this is what the Twitter API uses to verify our account.
You remember how we had to create an account here and get authenticated because not just anybody can
use the Twitter API.
Well, this is what they're asking for.
They're saying, Hey, I want you to verify that this is you if you're accessing the Twitter API.
I want to know who's accessing that data.
So in your case, after you've created an app, you just click on details and you see here that we have
keys and tokens.
These keys, I'm actually going to regenerate after this because you don't want to share this with anybody.
But I'll at least show you how these works so that for yourself you'll have different keys and API keys
and secrets that you have that you'll have to copy and paste.
Luckily, it tells us exactly what they are.
We have the API key which.
Is the consumer key.
We'll post it here as a string and then we have the API secret key.
Which is the consumer secret.
Which will paste it in there.
Let's make this a little bit bigger so you can see.
And then we have the access token and the access token secret.
Again, we copy the access token.
Paste it in here.
And the secret.
So again, these are the keys that Twitter API is going to be able to use to indentify you, but you
don't want to share these with anybody, even though I'm showing it to everybody.
Like I said after this video, I'm just going to regenerate so that.
Well, you won't be able to use my keys.
After we set up this auth object.
We then use the IP dot api to authenticate and we now have access to the API.
So let's check this out.
I have my public tweets and I can grab those by saying API, dot home timeline and then I'm going to
loop over these tweets, the public tweets and just print tweet, dot, text.
So let's try this out.
Things never work the first time around, so we'll see.
I'm going to run.
Python three.
And say, tweet, tweet, dot pie.
All right.
Look at that.
We've actually nowheres gotten the tweets from the home timeline.
And specifically for my Twitter home timeline.
So we see it here.
We have let's see what we have.
We have a retweet, an overview of the best newsletters that I know of for front end developers, including
general frontend JavaScript, CSS.
I hope this person included my newsletter.
By the way, I have a newsletter, in case you're wondering, every month.
But let's check this if I go to my home.
Hey, look at that.
An overview of the best newsletters that I know of for frontend developers.
Brian Rinaldi I hope my newsletter is included, but there you go.
That's our timeline right there.
And we're able to access the Twitter API.
That was pretty easy, right?
Not that.
Again, I keep saying this, I know I'm repetitive, but this is the power of Python.
There are just so many libraries, such a large community, and it's so easy to create things already.
Just by looking at this teepee library, you probably can think of different things that you can do
with Twitter.
Like I said, the world is your oyster.
That's why Python is such a popular language.
Let's get back to this.
So we have some tweets, but let's do something exciting here.
What else does the Twitter API give us?
Well, let's remove these public tweets for now and just print out.
Let's say API.
Dot me.
And this is actually going to give the information about myself.
So let's actually give this a variable saying user.
So and I'm going to say API dot me.
And this is going to give information about the user.
In my case, let's just do user dot name.
And again, this is something that you can check for yourself on their documentation.
And if I run this.
There you go.
That's my name.
That's my Twitter username.
What else do we have?
We have.
Well, the actual.
Screen name, which will be my Twitter handle.
Right here, Andre Nagoya with no spaces.
And then let's just do what else can we do?
We can also do followers count.
Again, these are all on the Tweet API that you can check for yourself.
If I do this.
Hey, look at that.
It's actually pretty crazy that I have 10,000 followers not trying to show off or anything, but it's
crazy.
I barely tweet and somehow 10,000.
So thank you for the follow up.
Not sure how useful my tweets are, but I appreciate the follows.
That's pretty cool.
But you know what?
I want to start building something useful with my Twitter bot.
So in the next video, we're going to do just that.
I'll see you in that one.
Bye bye.