1 00:00:00,860 --> 00:00:03,260 - In this video, we're going to determine 2 00:00:03,260 --> 00:00:05,880 the NASA account's followers by using 3 00:00:05,880 --> 00:00:08,690 the API object's followers method. 4 00:00:08,690 --> 00:00:11,820 Now this method is going to call from the Twitter API, 5 00:00:11,820 --> 00:00:16,210 the method named followers/list and that method 6 00:00:16,210 --> 00:00:19,599 enables you by default to get up to 20 7 00:00:19,599 --> 00:00:23,610 user objects representing followers of a given account 8 00:00:23,610 --> 00:00:27,690 at a time, but you can request as many as 200 at a time 9 00:00:27,690 --> 00:00:28,890 if you would like. 10 00:00:28,890 --> 00:00:31,300 For demo purposes here, we're only going to work 11 00:00:31,300 --> 00:00:35,190 with ten user account objects just to show you 12 00:00:35,190 --> 00:00:39,830 how to set up a cursor and how to use the cursor as well. 13 00:00:39,830 --> 00:00:42,180 So, I've already gone ahead and put in 14 00:00:42,180 --> 00:00:44,810 a couple more snippets into the current session 15 00:00:44,810 --> 00:00:46,330 that we're working in. 16 00:00:46,330 --> 00:00:50,650 I've created here in snippet 16 a followers list 17 00:00:50,650 --> 00:00:51,970 that's currently empty. 18 00:00:51,970 --> 00:00:55,290 We'll be appending each of the followers to that list. 19 00:00:55,290 --> 00:00:58,030 And here in snippet 17 we demonstrate 20 00:00:58,030 --> 00:01:00,840 how to set up a tweepy cursor. 21 00:01:00,840 --> 00:01:04,680 So the tweepy cursor's first argument is going to be 22 00:01:04,680 --> 00:01:07,870 the API object method to call. 23 00:01:07,870 --> 00:01:09,920 In this case it's the followers method 24 00:01:09,920 --> 00:01:12,380 which is going to give us back user objects 25 00:01:12,380 --> 00:01:15,430 for a specific twitter account. 26 00:01:15,430 --> 00:01:18,160 And those again are going to represent followers 27 00:01:18,160 --> 00:01:20,030 of that specific account. 28 00:01:20,030 --> 00:01:22,860 And then the second argument in this case 29 00:01:22,860 --> 00:01:25,130 is a keyword argument screen-name. 30 00:01:25,130 --> 00:01:28,000 That's going to be passed as an argument 31 00:01:28,000 --> 00:01:33,000 into the followers method and the followers method in turn 32 00:01:33,410 --> 00:01:35,780 will send that information over to twitter 33 00:01:35,780 --> 00:01:37,560 and we will start getting back 34 00:01:37,560 --> 00:01:41,775 followers when we iterate though the cursors results. 35 00:01:41,775 --> 00:01:43,130 So, cursors are lazy. 36 00:01:43,130 --> 00:01:46,350 They don't give you results until you request them. 37 00:01:46,350 --> 00:01:50,200 Now here in snippet 18, I have not executed this yet. 38 00:01:50,200 --> 00:01:53,350 We have a for loop that shows you how to iterate 39 00:01:53,350 --> 00:01:55,110 over cursor results. 40 00:01:55,110 --> 00:01:57,610 Now the items method that we're using on 41 00:01:57,610 --> 00:02:01,470 the cursor object here is specifying how many results 42 00:02:01,470 --> 00:02:02,340 we would like to get. 43 00:02:02,340 --> 00:02:04,020 In this case, only ten. 44 00:02:04,020 --> 00:02:07,350 If you wanted 1000 results, you could put 1000 there. 45 00:02:07,350 --> 00:02:10,870 And by the way, if you don't put a number there, 46 00:02:10,870 --> 00:02:15,870 it's going to by default try to get all of the followers 47 00:02:15,980 --> 00:02:19,310 for the screen name NASA that we set up in 48 00:02:19,310 --> 00:02:21,130 the cursor object up above. 49 00:02:21,130 --> 00:02:24,040 And remember, they have 30 plus million followers, 50 00:02:24,040 --> 00:02:27,390 31 plus million followers according to snippet 14. 51 00:02:27,390 --> 00:02:32,390 So, based on the limits of the twitter API with respect 52 00:02:32,960 --> 00:02:35,820 to getting followers on a specific account, 53 00:02:35,820 --> 00:02:38,940 it would take an enormous amount of time 54 00:02:38,940 --> 00:02:42,937 to iterate through all of the followers for NASA 55 00:02:45,140 --> 00:02:47,360 because of the rate limits involved. 56 00:02:47,360 --> 00:02:50,580 So, after we get through these couple of snippets, 57 00:02:50,580 --> 00:02:52,130 I'm going to go back to the slides and 58 00:02:52,130 --> 00:02:54,160 talk about a way that you can get more 59 00:02:54,160 --> 00:02:57,280 users at a time if in fact you had the need 60 00:02:57,280 --> 00:02:58,640 to do that. 61 00:02:58,640 --> 00:03:01,930 So, I'm going to, as I iterate through this cursor, 62 00:03:01,930 --> 00:03:04,900 say to the followers list that we'd like to append 63 00:03:04,900 --> 00:03:07,780 just the user objects screen names. 64 00:03:07,780 --> 00:03:12,210 So each account that we get back is a user object in tweepy 65 00:03:12,210 --> 00:03:14,490 and the screen name attribute will give me 66 00:03:14,490 --> 00:03:18,150 that user's twitter handle that I can then go ahead 67 00:03:18,150 --> 00:03:21,350 and attach to the list. 68 00:03:21,350 --> 00:03:25,130 So, at this point, it has made the call-out to twitter, 69 00:03:25,130 --> 00:03:29,140 gotten back the ten results and I've appended those results 70 00:03:29,140 --> 00:03:30,280 to my list. 71 00:03:30,280 --> 00:03:32,120 Now if we wanted to display those results, 72 00:03:32,120 --> 00:03:33,870 we have lots of ways to do that. 73 00:03:33,870 --> 00:03:36,670 Here, I have a little print statement in which I'm going 74 00:03:36,670 --> 00:03:39,490 to print the string followers with a colon 75 00:03:39,490 --> 00:03:43,880 and then I'm going to use string method join 76 00:03:43,880 --> 00:03:48,310 to join together all of those followers separated by spaces. 77 00:03:48,310 --> 00:03:51,650 So remember you call join on the separator string. 78 00:03:51,650 --> 00:03:55,150 And we decided to sort that followers list 79 00:03:55,150 --> 00:03:57,180 and we're sorting that based on 80 00:03:57,180 --> 00:04:01,350 the lower case representations of each of the screen-names. 81 00:04:01,350 --> 00:04:04,400 That way we get alphabetical order for the screen-names 82 00:04:05,320 --> 00:04:08,900 because we're only comparing lowercase letters overall. 83 00:04:08,900 --> 00:04:12,190 Now remember that when you do sorting of strings in Python, 84 00:04:12,190 --> 00:04:15,780 you get lexicographical comparison by default. 85 00:04:15,780 --> 00:04:19,650 So when I execute that, we can see that it didn't 86 00:04:19,650 --> 00:04:20,610 modify the screen-names by the way, 87 00:04:20,610 --> 00:04:22,270 it doesn't make them lowercase, 88 00:04:22,270 --> 00:04:25,110 it just compares them as lowercase strings. 89 00:04:25,110 --> 00:04:29,230 But these are just ten followers of the NASA account 90 00:04:29,230 --> 00:04:33,370 that we're processing in this video. 91 00:04:33,370 --> 00:04:37,690 Again, you have a pretty limited total number of users 92 00:04:37,690 --> 00:04:39,700 you can get this way. 93 00:04:39,700 --> 00:04:42,270 But there are other options for you. 94 00:04:42,270 --> 00:04:45,420 So let's go back over the slides for a second here. 95 00:04:45,420 --> 00:04:48,440 So again this particular followers method only 96 00:04:48,440 --> 00:04:50,630 allows you to get over 200 at a time. 97 00:04:50,630 --> 00:04:52,920 It gets 20 at at time by default 98 00:04:52,920 --> 00:04:55,560 unless you request fewer than that. 99 00:04:55,560 --> 00:04:59,380 And if you did want to get the full 200 at a time, 100 00:04:59,380 --> 00:05:00,850 you could set up the cursor 101 00:05:00,850 --> 00:05:03,610 with the additional keyword argument count. 102 00:05:03,610 --> 00:05:05,730 In which case, both the screen-name 103 00:05:05,730 --> 00:05:09,910 and the count will be passed to the followers method. 104 00:05:09,910 --> 00:05:13,310 And the maximum value for count is 200, 105 00:05:13,310 --> 00:05:16,130 so you get to choose how many you want to get at a time 106 00:05:16,130 --> 00:05:18,180 up to that maximum number. 107 00:05:18,180 --> 00:05:21,290 Again, if you do call items on the cursor object 108 00:05:21,290 --> 00:05:23,730 to start iterating through it 109 00:05:23,730 --> 00:05:25,440 and you don't give it an argument, 110 00:05:25,440 --> 00:05:28,490 it is going to try to get all of the followers. 111 00:05:28,490 --> 00:05:30,743 But the followers list method only allows you 112 00:05:30,743 --> 00:05:33,030 only 15 calls every 15 minutes. 113 00:05:33,030 --> 00:05:36,390 Which means, with a maximum of 200 per call, 114 00:05:36,390 --> 00:05:40,810 you can only get 3000 followers every 15 minutes. 115 00:05:40,810 --> 00:05:43,940 And with actually over 31 million followers, 116 00:05:43,940 --> 00:05:46,320 you can just imagine how long it would take 117 00:05:46,320 --> 00:05:50,360 to get only 3000 at a time and work your way 118 00:05:50,360 --> 00:05:52,150 through every single follower. 119 00:05:52,150 --> 00:05:55,750 Now there are other API methods that allow you to do more. 120 00:05:55,750 --> 00:06:00,450 There's a followers IDs which allows you to call the 121 00:06:00,450 --> 00:06:04,160 twitter followers/ids method. 122 00:06:04,160 --> 00:06:06,630 This method can give you up to 5000 123 00:06:06,630 --> 00:06:09,460 twitter account ID numbers at a time. 124 00:06:09,460 --> 00:06:12,370 And with it's rate limits, you can get a total 125 00:06:12,370 --> 00:06:17,370 of 75,000 account ID numbers per 15 minute interval. 126 00:06:17,610 --> 00:06:21,100 So, you can get a much larger number at a time. 127 00:06:21,100 --> 00:06:24,250 And that particular set of data is useful 128 00:06:24,250 --> 00:06:28,170 if you then combine it with the lookup users method. 129 00:06:28,170 --> 00:06:32,050 This method in turn calls twitter's users/lookup method. 130 00:06:32,050 --> 00:06:35,560 And that can give you up to 100 user objects per call 131 00:06:35,560 --> 00:06:39,110 and you can call it 300 times per 15 minutes. 132 00:06:39,110 --> 00:06:43,970 So you can get up to 30,000 user objects this way 133 00:06:43,970 --> 00:06:47,940 per rate-limit interval versus only 3,000 134 00:06:47,940 --> 00:06:51,423 the way that I just showed you in the proceeding snippets.