- In this video, we're going to determine
the NASA account's followers by using
the API object's followers method.
Now this method is going to call from the Twitter API,
the method named followers/list and that method
enables you by default to get up to 20
user objects representing followers of a given account
at a time, but you can request as many as 200 at a time
if you would like.
For demo purposes here, we're only going to work
with ten user account objects just to show you
how to set up a cursor and how to use the cursor as well.
So, I've already gone ahead and put in
a couple more snippets into the current session
that we're working in.
I've created here in snippet 16 a followers list
that's currently empty.
We'll be appending each of the followers to that list.
And here in snippet 17 we demonstrate
how to set up a tweepy cursor.
So the tweepy cursor's first argument is going to be
the API object method to call.
In this case it's the followers method
which is going to give us back user objects
for a specific twitter account.
And those again are going to represent followers
of that specific account.
And then the second argument in this case
is a keyword argument screen-name.
That's going to be passed as an argument
into the followers method and the followers method in turn
will send that information over to twitter
and we will start getting back
followers when we iterate though the cursors results.
So, cursors are lazy.
They don't give you results until you request them.
Now here in snippet 18, I have not executed this yet.
We have a for loop that shows you how to iterate
over cursor results.
Now the items method that we're using on
the cursor object here is specifying how many results
we would like to get.
In this case, only ten.
If you wanted 1000 results, you could put 1000 there.
And by the way, if you don't put a number there,
it's going to by default try to get all of the followers
for the screen name NASA that we set up in
the cursor object up above.
And remember, they have 30 plus million followers,
31 plus million followers according to snippet 14.
So, based on the limits of the twitter API with respect
to getting followers on a specific account,
it would take an enormous amount of time
to iterate through all of the followers for NASA
because of the rate limits involved.
So, after we get through these couple of snippets,
I'm going to go back to the slides and
talk about a way that you can get more
users at a time if in fact you had the need
to do that.
So, I'm going to, as I iterate through this cursor,
say to the followers list that we'd like to append
just the user objects screen names.
So each account that we get back is a user object in tweepy
and the screen name attribute will give me
that user's twitter handle that I can then go ahead
and attach to the list.
So, at this point, it has made the call-out to twitter,
gotten back the ten results and I've appended those results
to my list.
Now if we wanted to display those results,
we have lots of ways to do that.
Here, I have a little print statement in which I'm going
to print the string followers with a colon
and then I'm going to use string method join
to join together all of those followers separated by spaces.
So remember you call join on the separator string.
And we decided to sort that followers list
and we're sorting that based on
the lower case representations of each of the screen-names.
That way we get alphabetical order for the screen-names
because we're only comparing lowercase letters overall.
Now remember that when you do sorting of strings in Python,
you get lexicographical comparison by default.
So when I execute that, we can see that it didn't
modify the screen-names by the way,
it doesn't make them lowercase,
it just compares them as lowercase strings.
But these are just ten followers of the NASA account
that we're processing in this video.
Again, you have a pretty limited total number of users
you can get this way.
But there are other options for you.
So let's go back over the slides for a second here.
So again this particular followers method only
allows you to get over 200 at a time.
It gets 20 at at time by default
unless you request fewer than that.
And if you did want to get the full 200 at a time,
you could set up the cursor
with the additional keyword argument count.
In which case, both the screen-name
and the count will be passed to the followers method.
And the maximum value for count is 200,
so you get to choose how many you want to get at a time
up to that maximum number.
Again, if you do call items on the cursor object
to start iterating through it
and you don't give it an argument,
it is going to try to get all of the followers.
But the followers list method only allows you
only 15 calls every 15 minutes.
Which means, with a maximum of 200 per call,
you can only get 3000 followers every 15 minutes.
And with actually over 31 million followers,
you can just imagine how long it would take
to get only 3000 at a time and work your way
through every single follower.
Now there are other API methods that allow you to do more.
There's a followers IDs which allows you to call the
twitter followers/ids method.
This method can give you up to 5000
twitter account ID numbers at a time.
And with it's rate limits, you can get a total
of 75,000 account ID numbers per 15 minute interval.
So, you can get a much larger number at a time.
And that particular set of data is useful
if you then combine it with the lookup users method.
This method in turn calls twitter's users/lookup method.
And that can give you up to 100 user objects per call
and you can call it 300 times per 15 minutes.
So you can get up to 30,000 user objects this way
per rate-limit interval versus only 3,000
the way that I just showed you in the proceeding snippets.