Live Twitter Data
Twitter data is useful for everything from marketing to NLP. Thankfully, Twitter has a pretty generous API that can be used to retrieve information such as recent Tweets or number of connections.
Generate an API Key
To read data from Twitter, you need to generate a an API key. Read more about how to generate your own key here.
Once you have your token, add it as an environment variable.
#See https://docs.deepnote.com/environment/environment-variables for information on how to add you own API key
import os
twitter_api_token = os.getenv('TWITTER_KEY') # See https://docs.deepnote.com/environment/environment-variables for more information
Number of Mentions
username
Top Tweets Today
You can use the API to keep up with trends and find Tweets that you should follow up on or respond to.
query
starting_date = pd.Timestamp.now() - pd.Timedelta(1,'D')
starting_date = starting_date.floor('S').isoformat() + 'Z' # Reformat in a form that Twitter wants
# Search the Tweets
df = search_tweets(query, starting_date)
# Extract and sort by # of likes
def get_likes(x):
return x.get('like_count')
df['nlikes'] = df.public_metrics.apply(get_likes)
df = df.sort_values('nlikes',ascending=False)