import requests
import pandas as pd
import matplotlib.pyplot as plt
Set up your Twitter API v2 bearer token
#use your bearer_token to get trends.
bearer_token = ‘AAAAAAAAAAAAAAAAAAAAAIuNpQEAAAAA4JVtHGOOE1txNbiZTsBEMZACMwk%3DhorVyY4nVoMUYozKWm4qwIe34ip46fT2iXhlleLrKoKXtLlWi4’
Define the API endpoint for fetching trends
trends_endpoint = “https://api.twitter.com/2/tweets/search/recent”
Define query parameters for the request
query_params = {
“query”: “place_country:IN”,
“max_results”: 10 # Get top 10 trends
}
Set up the headers with the bearer token
headers = {
“Authorization”: f"Bearer {bearer_token}"
}
Send the GET request to fetch the top trends
response = requests.get(trends_endpoint, params=query_params, headers=headers)
Parse the response JSON
data = response.json()
Extract and display the top 10 trending topics
top_trends = [t[‘text’] for t in data.get(‘data’, [])]
print(top_trends)
print(“Top 10 Twitter Trending Topics in India:”)
for index, trend in enumerate(top_trends, start=1):
print(f"{index}. {trend}")
Visualize the top trending topics using a bar chart
plt.figure(figsize=(10, 6))
below line is commented because data in new created account empty if above beareer taken is old or have data the below line work.
plt.barh(range(1, 11), top_trends[::-1]) # Reversed for better visualization
plt.xlabel(‘Trend’)
plt.ylabel(‘Rank’)
plt.title(‘Top 10 Twitter Trending Topics in India’)
plt.show()