Chapter 5. Interacting with Social Media Using Requests

In this contemporary world, our lives are woven with a lot of interactions and collaborations with social media. The information that is available on the web is very valuable and it is being used by abundant resources. For instance, the news that is trending in the world can be spotted easily from a Twitter hashtag and this can be achieved by interacting with the Twitter API.

Using natural language processing, we can classify emotion of a person by grabbing the Facebook status of an account. All this stuff can be accomplished easily with the help of Requests using the concerned APIs. Requests is a perfect module, if we want to reach out API frequently, as it supports pretty much everything, like caching, redirection, proxies, and so on.

We will cover the following topics in this chapter:

  • Interacting with Twitter
  • Interacting with Facebook
  • Interacting with reddit

API introduction

Before diving into details, let us have a quick look at what exactly is an Application Programming Interface (API).

A web API is a set of rules and specifications. It assists us to communicate with different software. There are different types of APIs, and REST API is the subject matter here. REpresentational State Transfer (REST) is an architecture containing guidelines for building scalable web services. An API which adheres to the guidelines and conforms to the constraints of REST is called a RESTful API. In a nutshell, the constraints are:

  • Client-server
  • Stateless
  • Cacheable
  • Layered system
  • Uniform interface
  • Code on demand

Google Maps API, Twitter API, and GitHub API are various examples RESTful APIs.

Let us understand much more about an API. Take an instance of getting all tweets from Twitter with the hashtag "worldtoday" which includes the process of authenticating, sending requests and receiving responses from different URLs, and dealing with different methods. All the said processes and the procedures will be specified in the API of Twitter. By following these procedures, we can collaborate with the web smoothly.

Getting started with the Twitter API

To get started with Twitter API we should first obtain an API key. It is a code which is passed by the computer programs while calling an API. The basic purpose of the API key is that it uniquely identifies the program that it is trying to interact with. It also serves us in the process of authentication with its token.

The next step involves the process of creating an authentication request which will give us access to the Twitter account. Once we have authenticated successfully, we will be free to deal with tweets, followers, trends, searches, and stuff. Let us get to know more about the steps to follow.

Note

Please note that, we will be using the Twitter API 1.1 version in all the examples.

Obtaining an API Key

Getting an API key is pretty simple. You need to follow the steps prescribed in the following section:

  1. At first, you need to sign into the page https://apps.twitter.com/ with your your Twitter credentials.
  2. Click on Create New App button.
  3. Now, you need to fill the following fields to set up a new application:
    • Name: Specify your application name. This is used to attribute the source of a tweet and in user-facing authorization screens.
    • Description: Enter a short description of your application. This will be shown when a user faces the authorization screens.
    • Website: Specify your fully qualified website URL. A fully qualified URL includes http:// or https:// and will not have a trailing slash in the end (for example: http://example.com or http://www.example.com).
    • Callback URL: This field answers the question—where should we return after successfully authenticating.
    • Developer Agreement: Read the Developer Agreement carefully and then check the checkbox Yes, I agree.
  4. Now, by clicking on Create your Twitter application, a new application will be created for us with the previously specified details.
  5. After the successful creation, we'll be redirected to a page where the Details tab is selected by default. Now, select the Keys and Access Tokens tab. We should click on Create my access token button to generate our access token.
  6. Lastly, make a note of the Consumer Key (API Key), Consumer Secret (API Secret), Access Token and Access Token Secret.

Creating an authentication Request

If we remember the theme of the third chapter, we learned different kinds of authentication with requests, such as Basic authentication, Digest authentication, and OAuth authentication. Time to apply all that stuff in real time!

Now, we will be using OAuth1 authentication to get the access to the Twitter API. In the first step of obtaining a key, we got access to Consumer key, Consumer secret, Access token and Access token secret, now we should use them to authenticate our application. The following commands show how we can accomplish the process:

>>> import requests
>>> from requests_oauthlib import OAuth1
>>> CONSUMER_KEY = 'YOUR_APP_CONSUMER_KEY'
>>> CONSUMER_SECRET = 'YOUR_APP_CONSUMER_SECRET'
>>> ACCESS_TOKEN = 'YOUR_APP_ACCESS_TOKEN'
>>> ACCESS_TOKEN_SECRET = 'YOUR_APP_ACCESS_TOKEN_SECRET'

>>> auth = OAuth1(CONSUMER_KEY, CONSUMER_SECRET,
...               ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

In the preceding lines, we have sent our keys and tokens to the API and got ourselves authenticated and stored them in the variable auth. Now, we can do all sorts of interactions with the API using this. Let us start to interact with the Twitter API.

Note

Keep in mind that, all the twitter interacting examples that are depicted after this will be using the "auth" value obtained in the previous section.

Getting your favorite tweet

Let us grab some favorite tweets of the authenticated user first. For this, we should send a request to the Twitter API to access the favorite tweets. The request can be sent with a Resource URL by specifying the parameters. The Resource URL for getting the favorite list looks like this:

https://api.twitter.com/1.1/favorites/list.json

We can also send some optional parameters to the URL like user_id, screen_name, count, since_id, max_id, include_identities to accomplish our needs. Let us get one favorite tweet now.

>>> favorite_tweet = requests.get('https://api.twitter.com/1.1/favorites/list.json?count=1', auth=auth)
>>> favorite_tweet.json()
[{u'contributors': None, u'truncated': False, u'text': u'India has spent $74 mil to reach Mars. Less than the budget of the film u201cGravity,u201d $100 million.

#respect
#ISRO
#Mangalyaan', u'in_reply_to_status_id': None, …}]

In the first step, we sent a get request with the parameter count and the authentication auth to the resource URL. In the next step, we accessed the response within the JSON format which gave us my favorite tweet, and it is that simple.

As we have specified the count parameter as 1 in the request, we happened to see the result with one favorite tweet. By default, if we don't specify the optional parameter count, the request will result in 20 most recent favorite tweets.

Performing a simple search

We shall make a search with a Twitter's API now. For this, we will be making use of Search API of Twitter. The basic URL structure for searching has the following syntax:

https://api.twitter.com/1.1/search/tweets.json?q=%40twitterapi

It has got additional parameters like Result type, Geolocation, language, Iterating in a result set.

>>> search_results = requests.get('https://api.twitter.com/1.1/search/tweets.json?q=%40python', auth=auth)
>>> search_results.json().keys()
[u'search_metadata', u'statuses']
>>> search_results.json()["search_metadata"]
{u'count': 15, u'completed_in': 0.022, u'max_id_str': u'529975076746043392', u'since_id_str': u'0', u'next_results': u'?max_id=527378999857532927&q=%40python&include_entities=1', u'refresh_url': u'?since_id=529975076746043392&q=%40python&include_entities=1', u'since_id': 0, u'query': u'%40python', u'max_id': 529975076746043392}

In the preceding example, we tried to search for tweets with the words python.

Accessing the list of followers

Let us access the followers of a specified user. By default, when we query for the list of followers, it returns the 20 most recent following users. The resource URL looks like this:

https://api.twitter.com/1.1/followers/list.json

It returns a cursored collection of user objects for users following the specified user:

>>> followers = requests.get('https://api.twitter.com/1.1/followers/list.json', auth=auth)
>>> followers.json().keys()
[u'previous_cursor', u'previous_cursor_str', u'next_cursor', u'users', u'next_cursor_str']
>>> followers.json()["users"]
[{u'follow_request_sent': False, u'profile_use_background_image': True, u'profile_text_color': u'333333'... }]

Retweets

A tweet which has been reposted is called a retweet. To access the most recent retweets that have been authored by the authenticated user, we will be using the following URL:

https://api.twitter.com/1.1/statuses/retweets_of_me.json

The optional parameters that can be sent with it are count, since_id, max_id, trim_user, include_entites, include_user_entities

>>> retweets = requests.get('https://api.twitter.com/1.1/statuses/retweets_of_me.json', auth=auth)
>>> len(retweets.json())
16
>>> retweets.json()[0]
{u'contributors': None, u'text': u'Iu2019m now available to take on new #python #django #freelance projects. Reply for more details!', {u'screen_name': u'vabasu', ...}}

Accessing available trends

Twitter trends are hashtag-driven subject matter that is popular at a specific time. Take an instance of getting a location of the available trends in Twitter. For that, we will use the following URL:

https://api.twitter.com/1.1/trends/available.json

The response of the resource URL is an array of locations in encoded form:

>>> available_trends = requests.get('https://api.twitter.com/1.1/trends/available.json', auth=auth)
>>> len(available_trends.json())
467
>>> available_trends.json()[10]
{u'name': u'Blackpool', u'countryCode': u'GB', u'url': u'http://where.yahooapis.com/v1/place/12903', u'country': u'United Kingdom', u'parentid': 23424975, u'placeType': {u'code': 7, u'name': u'Town'}, u'woeid': 12903}

In the preceding lines of code, we searched for the locations of the available_trends. Then, we learned that the number of locations having available_trends is 467. Later, we tried to access the tenth location's data and it resulted in a response with the location information which is encoded with woeid. This is a unique identifier called Where on Earth ID.

Updating user status

To update the authenticated user's current status, which is also known as tweeting, we follow the following procedure.

For each update attempt, the update text is compared with the authenticating user's recent tweets. Any attempt that would result in duplication will be blocked, resulting in a 403 error. Therefore, a user cannot submit the same status twice in a row.

>>> requests.post('https://api.twitter.com/1.1/statuses/update.json?status=This%20is%20a%20Tweet', auth=auth)
..................Content has been hidden....................

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