Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter API - get tweets with specific id

Tags:

python

twitter

I have a list of tweet ids for which I would like to download their text content. Is there any easy solution to do this, preferably through a Python script? I had a look at other libraries like Tweepy and things don't appear to work so simple, and downloading them manually is out of the question since my list is very long.

like image 854
Crista23 Avatar asked Feb 07 '15 16:02

Crista23


People also ask

What is ID in Twitter API?

Today, Twitter IDs are unique 64-bit unsigned integers, which are based on time, instead of being sequential. The full ID is composed of a timestamp, a worker number, and a sequence number.

How do I download tweets from Twitter API?

Navigate to your app dashboard. Select the app you've enabled with the Tweets and users preview, then click Details. Select the Keys and tokens tab. In the Consumer API Keys section, copy the values for API Key into consumer_key and API Secret Key into consumer_secret .

Can you get old tweets from Twitter API?

Both Historical PowerTrack and Full-Archive Search provide access to any publicly available Tweet, starting with the first Tweet from March 2006. Deciding which product better suits your use case can be key in getting the data you want when you need it.

Which returns a single status specified by the ID parameter?

GET statuses/show/:id Returns a single Tweet, specified by the id parameter. The Tweet's author will also be embedded within the Tweet.


1 Answers

You can access specific tweets by their id with the statuses/show/:id API route. Most Python Twitter libraries follow the exact same patterns, or offer 'friendly' names for the methods.

For example, Twython offers several show_* methods, including Twython.show_status() that lets you load specific tweets:

CONSUMER_KEY = "<consumer key>" CONSUMER_SECRET = "<consumer secret>" OAUTH_TOKEN = "<application key>" OAUTH_TOKEN_SECRET = "<application secret" twitter = Twython(     CONSUMER_KEY, CONSUMER_SECRET,     OAUTH_TOKEN, OAUTH_TOKEN_SECRET)  tweet = twitter.show_status(id=id_of_tweet) print(tweet['text']) 

and the returned dictionary follows the Tweet object definition given by the API.

The tweepy library uses tweepy.get_status():

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) auth.set_access_token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET) api = tweepy.API(auth)  tweet = api.get_status(id_of_tweet) print(tweet.text) 

where it returns a slightly richer object, but the attributes on it again reflect the published API.

like image 194
Martijn Pieters Avatar answered Oct 05 '22 07:10

Martijn Pieters