Determining your Twitter followers and friends

In the Twitter social network, users are labeled either as followers or friends relative to a particular user. Your friends are the people that you follow and your followers are the people that follow you. In this recipe, we determine who your friends are, who your followers are, and how much overlap there is in each group.

Getting ready

For this recipe, we will be using the results of the previous two recipes and the twitter_oauth_login() function. Also, we will be working in IPython or the default Python REPL, if you prefer that instead. Feel free to use an editor in order to start capturing and modifying the code as it grows in complexity.

How to do it...

The following steps will allow you to determine all of your Twitter friends and followers:

  1. In IPython or your favorite REPL, enter the following:
    twitter = twitter_oauth_login()
    
    friends_ids = twitter.get_friends_ids(count=5000)
    friends_ids = friends_ids['ids']
    
    followers_ids = twitter.get_followers_ids(count=5000)
    followers_ids = followers_ids['ids']
    
  2. With all of your followers' and friends' Twitter IDs collected, let's see how many you have:
    In [26]: len(friends_ids), len(followers_ids)
    Out[26]: (352, 554)
    
  3. We will use Python sets, which are based on the sets that you might have encountered in math class, to examine some properties of our friends and followers:
    friends_set = set(friends_ids)
    followers_set = set(followers_ids)
    
    print('Number of Twitter users who either are our friend or follow you (union):')
    print(len(friends_set.union(followers_set)))
    
    print('Number of Twitter users who follow you and are your friend (intersection):')
    print(len(friends_set & followers_set))
    
    print("Number of Twitter users you follow that don't follow you (set difference):")
    print(len(friends_set - followers_set))
    
    print("Number of Twitter users who follow you that you don't follow (set difference):")
    print(len(followers_set - friends_set))
    

    The preceding snippet should result in the following output:

    How to do it...

Tip

The numbers shown in the preceding screenshot will most likely be different based on the number of friends and followers you have.

How it works...

This recipe demonstrates just how useful the Twython package is and how easy it makes certain tasks. After we logged in using the twitter_oauth_login function, we make two basic calls using the twitter object, one to get friends' IDs and one to get followers' IDs. Note that we set the count parameter to 5000, which is the maximum value allowed by the Twitter API. The twitter object returns a dictionary from which we extract the actual IDs.

One of the nice things about the Twython interface is how closely it mirrors the Twitter API. If you ever have a question about a particular function, just check the Twitter documents.

Once we have collected our list of friends' and followers' IDs, we turn to the Python set type for some quick navel gazing. The Python set type, which has been built into Python since Version 2.4, is an unordered collection of unique objects. The key word for us is the word unique. If we create a set from a list with duplicates, we will get a set with only unique elements; set([1, 2, 2, 3, 3, 3]) will return {1, 2, 3}.

We unite the set of friends' IDs with the followers' IDs to determine the total set of unique IDs of Twitter users that either follow or are followed by us. In the preceding code, we use the union method of the set type, but there are several other ways in which we could have done this:

(friends_set | followers_set)
(set(friends_ids + followers_ids))

There's more...

While Twython's beautiful abstraction hides some of the complexity of using the API, this simplicity or magic can be problematic if we don't have an understanding of what is actually happening behind the scenes. When we call the twitter.get_friends_ids(count=5000) method, we are sending an HTTP GET request to a particular URL. In the case of twitter.get_friends_ids(), the URL is https://api.twitter.com/1.1/friends/ids.json.

The count=5000 input parameter to the function call shows up as field-value pairs in the URL and as such, the URL becomes https://api.twitter.com/1.1/friends/ids.json?count=5000.

Now, the actual API endpoint requires some default parameter values that Twython fills in for us, as shown in the following URL for clarity:

https://api.twitter.com/1.1/friends/ids.json?cursor=-1&screen_name=sayhitosean&count=5000

The Twitter v1.1 API requires all requests to be authenticated using OAuth. The required information is actually embedded in the header of the GET request, and the process of constructing the appropriate header is extensive (for more information, go to https://dev.twitter.com/docs/auth/authorizing-request). Thus, Twython not only forms the proper URL for making the request, but also handles the relatively painful Open Authorization so that we don't have to. If you are interested, you can go down a level lower and construct your own GET requests using the excellent request library or an alternative of your choosing. We leave this for the reader to explore.

Note

Note that different Twitter API endpoints have different rate limits. In the case of GET friends/IDs, we are only allowed 15 calls over a 15 minute period for Version 1.1 of the API as of May 2014. Other endpoints are less stingy with their data.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.167.114