×

Search anything:

Get trending topics using Trends API of Twitter

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

Reading time: 35 minutes | Coding time: 10 minutes

Trends API is a part of twitter API that is used to return the top 50 trending topics for a specific WOEID location from the twitter server. It is used to extract data from the twitter server about the trending topics in the specified location.

It can be used to return the locations for which twitter server has trending information available. It can be used to return the locations for which twitter server has trending information available closest to a specified location. It requires twitter account authentication before accessing it.

A WOEID stand for Where On Earth IDentifier which is a unique 32-bit reference identifier, that is used to refer to a particular location on earth.

We will understand how to use the Trends API of Twitter API in Python.

Characteristics of Twitter API and Trends API

  • The twitter API uses JSON data format for returning and receiving the data.
  • The twitter API is HTTP-based (over SSL) API meaning we can use get method to retrieve data from twitter,post method to send requests to the twitter server and search method to search the twitter posts.
  • The twitter API limits the number of requests that can be sent to the twitter server per access token or twitter account.This is called twitter rate limit.If you encounter twitter rate limit exceeded error it means that Twitter rejected consecutive attempts to access its API under your Twitter account.The rate limit is different for different methods of the API.
  • The methods of twitter API accepts various parameters which are used to cusotmize the requests according to needs.
  • There are twitter API libraries for almost all programming languages.

Read the Documentation of twitter API from here

Getting Twitter API keys

To start with, we will need to have a Twitter developer account and obtain credentials (i.e. API key, API secret, Access token and Access token secret) on the to access the Twitter API, following these steps:

  1. Create a Twitter developer account https://developer.twitter.com/
  2. Go to https://developer.twitter.com/en/apps and log in with your Twitter user account.
  3. Click “Create an app”
  4. Fill out the form, and click “Create”
  5. A pop up window will appear for reviewing Developer Terms. Click the “Create” button again.
  6. In the next page, click on “Keys and Access Tokens” tab, and copy your “API key” and “API secret” from the Consumer API keys section.
  7. Scroll down to Access token & access token secret section and click “Create”. Then copy your “Access token” and “Access token secret.

What is Requests package?

The requests module allows you to send HTTP requests using Python.
The HTTP request returns a Response Object with all the response data (content, encoding, status, etc).We will be using the post method of requests library to send a request to tweet.
requests-1

Install requests package using command:

pip install requests

Get Requests

The get() method sends a GET request to the specified url.It is used to retrieve data from the server in JSON format.

Syntax

requests.get(url, params={key: value}, args)

Python Code And Explanation

After importing the dependencies, first we want to create variables(consumer_key and consumer_secret) that will authenticate with Twitter.You will find all the required variables in your developer's account dashboard and we can copy and paste each of them as strings.

#importing all dependencies
import numpy as np
import tweepy
import requests
import base64

#Define your keys from the developer portal
consumer_key = 'XXXXXXXXXXXXXXXXXXXXXX'
consumer_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

The twitter API requires a single key that is a string of a base64 encoded version of the two keys separated by a colon so we will encode the consumer keys into base64 which is the usable form.

#Reformat the keys and encode them
key_secret = '{}:{}'.format(consumer_key, consumer_secret_key).encode('ascii')
#Transform from bytes to bytes that can be printed
b64_encoded_key = base64.b64encode(key_secret)
#Transform from bytes back into Unicode
b64_encoded_key = b64_encoded_key.decode('ascii')

Now, We will use requests package of python to post an authentication request using twitter authentication resource URL to the twitter server and store the post response in a variable. We To check and make sure that the request worked , We will print the status code of the request response. If the status code printed is 200 then the request worked successfully.

base_url = 'https://api.twitter.com/'
auth_url = '{}oauth2/token'.format(base_url)
auth_headers = {
    'Authorization': 'Basic {}'.format(b64_encoded_key),
    'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
}
auth_data = {
    'grant_type': 'client_credentials'
}
auth_resp = requests.post(auth_url, headers=auth_headers, data=auth_data)
print(auth_resp.status_code)
access_token = auth_resp.json()['access_token']

Captureauth-1

Now, we have to send a get request to te twitter server which will return the trending topics on twitter. This request requires authentications so we will pass access token into the headers of this request.

Now we will assign the parameters required for this request in JSON format. The parameter of id specifies the WOEID of the location we want to get trending topics from. Value of id in this case is assigned 2282863 for location Delhi, India.

If we want to access the global information we will assign the id parameter as 1.

Now, we will send a get request to the twitter server and store the response. We will print the status code of the request response. If the status code printed is 200 then the request worked successfully.

trend_headers = {
    'Authorization': 'Bearer {}'.format(access_token)    
}

trend_params = {
    'id': 1,
}

trend_url = 'https://api.twitter.com/1.1/trends/place.json'  
trend_resp = requests.get(trend_url, headers=trend_headers, params=trend_params)

trendresponse

Now , we will save the response in json format and print it. The response contains the trending topics tweets and its various parameters in JSON format.

tweet_data = trend_resp.json()
print(tweet_data)

tweetJSON

Now, we shall parse through the JSON response to print the top trending tweets.

for i in range(0,10):
  print(tweet_data[0]['trends'][i])

We get the output as the top tending tweets:

{'name': 'Asim', 'url': 'http://twitter.com/search?q=Asim', 'promoted_content': None, 'query': 'Asim', 'tweet_volume': 481954}
{'name': 'Pakistan', 'url': 'http://twitter.com/search?q=Pakistan', 'promoted_content': None, 'query': 'Pakistan', 'tweet_volume': 253674}
{'name': 'श्री राम', 'url': 'http://twitter.com/search?q=%22%E0%A4%B6%E0%A5%8D%E0%A4%B0%E0%A5%80+%E0%A4%B0%E0%A4%BE%E0%A4%AE%22', 'promoted_content': None, 'query': '%22%E0%A4%B6%E0%A5%8D%E0%A4%B0%E0%A5%80+%E0%A4%B0%E0%A4%BE%E0%A4%AE%22', 'tweet_volume': 25951}
{'name': 'Bhai', 'url': 'http://twitter.com/search?q=Bhai', 'promoted_content': None, 'query': 'Bhai', 'tweet_volume': 117732}
{'name': '#Thalapathy64update', 'url': 'http://twitter.com/search?q=%23Thalapathy64update', 'promoted_content': None, 'query': '%23Thalapathy64update', 'tweet_volume': 45406}
{'name': '#AlwaysByYourSideSid', 'url': 'http://twitter.com/search?q=%23AlwaysByYourSideSid', 'promoted_content': None, 'query': '%23AlwaysByYourSideSid', 'tweet_volume': 804472}
{'name': '#CongressFoundationDay', 'url': 'http://twitter.com/search?q=%23CongressFoundationDay', 'promoted_content': None, 'query': '%23CongressFoundationDay', 'tweet_volume': 40756}
{'name': 'Tom Curran', 'url': 'http://twitter.com/search?q=%22Tom+Curran%22', 'promoted_content': None, 'query': '%22Tom+Curran%22', 'tweet_volume': None}
{'name': '#AskSushanth', 'url': 'http://twitter.com/search?q=%23AskSushanth', 'promoted_content': None, 'query': '%23AskSushanth', 'tweet_volume': None}
{'name': '#PattasAudio', 'url': 'http://twitter.com/search?q=%23PattasAudio', 'promoted_content': None, 'query': '%23PattasAudio', 'tweet_volume': 19773}

Sample JSON response of Trends API

[ 
   { 
      'trends':[ 
         { 
            'name':'Asim',
            'url':'http://twitter.com/search?q=Asim',
            'promoted_content':None,
            'query':'Asim',
            'tweet_volume':481954
         },
         { 
            'name':'Pakistan',
            'url':'http://twitter.com/search?q=Pakistan',
            'promoted_content':None,
            'query':'Pakistan',
            'tweet_volume':253674
         },
         { 
            'name':'श्री राम',
            'url':'http://twitter.com/search?q=%22%E0%A4%B6%E0%A5%8D%E0%A4%B0%E0%A5%80+%E0%A4%B0%E0%A4%BE%E0%A4%AE%22',
            'promoted_content':None,
            'query':'%22%E0%A4%B6%E0%A5%8D%E0%A4%B0%E0%A5%80+%E0%A4%B0%E0%A4%BE%E0%A4%AE%22',
            'tweet_volume':25951
         },
         
         ...
         
         { 
            'name':'#MaryKom',
            'url':'http://twitter.com/search?q=%23MaryKom',
            'promoted_content':None,
            'query':'%23MaryKom',
            'tweet_volume':None
         },
         { 
            'name':'#HappyBirthdaySalmanKhan',
            'url':'http://twitter.com/search?q=%23HappyBirthdaySalmanKhan',
            'promoted_content':None,
            'query':'%23HappyBirthdaySalmanKhan',
            'tweet_volume':67197
         }
      ],
      'as_of':'2019-12-28T11:50:43Z',
      'created_at':'2019-12-28T11:46:12Z',
      'locations':[ 
         { 
            'name':'Delhi',
            'woeid':20070458
         }
      ]
   }
]

With this, you will have the complete idea of using the Trends API of Twitter. Enjoy.

Get trending topics using Trends API of Twitter
Share this