Welcome back.
It's time to do something.
Well, actually productive with our Twitter bot.
So what can we do here?
Well, I want to create a bot.
This bot is going to be well, you want to be really nice to your followers and you want to follow back
people.
If you actually look, I'm I'm pretty, pretty horrible.
I only have six people that I'm following.
But if I had to go through all my followers and follow every single one of them, that's that's over
10,000 clicks.
That's a pretty manual task.
Can we do that with with our bot?
Well, yeah, you can.
So let's do a super generous bot.
So this generous bot is always going to follow back.
How can you build this?
Well, we simply loop and say for follower.
In.
And then here, T.P. gives us something called a cursor.
Now, what is this cursor?
If we go to the documentation here and I type in cursor.
We'll get well, an actual cursor tutorial.
If we read here, we see that we use pagination in a lot of Twitter API.
So whether we're going through timelines or user lists or messages.
We have to click through different pages, right?
Usually there's a lot of followers or a lot of timeline information.
So the cursor allows us to, well, essentially in an easy way, go through everything on Twitter,
whether it's through an entire timeline or through an entire followers list, and they even give you
a nice tutorial of how to use it.
You can see over here, for example, let's make this a little bit bigger.
We see that we can use the precursor to do API user timeline items to grab all the items on the timeline.
So with the cursor we can actually do api dot.
Followers to grab all the followers.
And then we simply do dot items.
So that we can loop through all the followers.
Now, with this followers.
Well, let's just print them out.
Now I'm going to print followers or follower in this case because we're looping through here and then
just say dot name.
Now, the problem here is that when we do something like this, when let's say we're looping through
a lot of things, we're going to be hitting the API over and over and over.
And Twitter is not going to like that.
I mean, this is a simple example, but when we constantly hit the Twitter API that's hitting the Twitter
servers, which Twitter has to pay for, and if everybody in the world hit the Twitter API over and
over and over fast with, let's say Python, that, as we know on machines runs really fast.
Well, it's going to overload the servers and it's going to cost them a lot of money or crash the servers.
So they actually implement something called a rate limit where I'm only allowed to request the Twitter
API, let's say every couple of milliseconds so that it doesn't crash the server and actually allows
us to work with this Twitter rate limit.
So if I type in here rate limiter.
There's actually an error.
You can see over here the rate limit error, which is raised when the API method fails due to hitting
the Twitter's rate limit.
Tweet B over here allows us to use the rate limit error to say, Hey, if we hit this error, let's
do something.
So I'm going to create a little helper function.
I'm going to say Define limit handle.
So we're going to handle here the limit and we're going to pass it the cursor, which is, again, the
cursor that's going to passionate or go through the entire data of API followers.
And we're going to do a wild loop that says while this is true.
Just keep yielding.
Cursor.
Dot next.
Because cursor is a generator.
However, whenever we keep calling next here, which, by the way, internally this is what the cursor
is doing, is just constantly calling next and just keeps going.
Keeps going until we finish our followers.
However.
If this is not the case.
Well, in that case, I'm going to say.
Except.
And we're going to wrap this in a tri block, actually.
So I'm going to say try here so we can catch this error.
So try while true.
And then.
Except.
If we get a teapot rate limit error.
Well, in that case, I want to pause and not hit the Twitter API so we don't get in trouble and come
back to it when we have finally gave it a bit of time.
So we can actually do that with a built in module in Python called time and time allows us to do something
really useful here.
I can do something called sleep which essentially says, Hey, just pause this, whatever it's happening,
just stay on this line for a few milliseconds.
So if we do 1000, that's a 1000 milliseconds or 1/2.
So now we have a limit handle or we can call it limit handler.
And if we wrap the limit handler, which receives a cursor, so that is we just wrap this entire thing.
Like so.
Let's make this a little bit bigger.
There you go.
We now are able to not abuse and make sure that if we hit the raid limit, we take a bit of a pause
instead of just getting errors.
So let's see if that worked.
I'm going to save.
And run our piece of code.
Let's clear here and run this.
All right.
D.C., how it's pausing right now.
So as soon as we hit the rate limit, it pauses.
And it just keeps going.
And there you go.
It's printing out all the followers that I have.
That's pretty cool, right?
Now I'm actually going to stop this because, well, it's still going through it.
But what we can do now is we can actually follow back each of these individual people.
Now, in my case, I'm not on Twitter that much, so let's just follow somebody here at random.
Let's follow this user right here.
I'm going to copy this user's name like this, and I'm going to do something fun here.
I'm going to say if.
Follower dot name is equal to this person.
Well, then, in that case, I want to follow this person.
So I'm going to say.
Follow her dot.
Follow, which allows me to follow the user.
If I save this and run my code, it's running.
It's running, it's running.
Like I say, it takes a bit of time because it has to go through the entire list.
But I'm going to pause the video and when this is done, we're going to see if this worked.