Using the Twitter streaming API, is it possible to only display tweets from a specific user?

Viewed 2589

I am currently using the Twitter API to retrieve tweets made by certain users. For the sake of this question, we will use @justinbieber as an example.

When using the https://stream.twitter.com/1.1/statuses/filter.json resource, setting follow to the required user ID (@justinbieber = 27260086), and allowing it to run, while I would only expect @justinbieber's tweets, I end up getting the tweets made to him from his millions of fans. Obviously this means I get way more information than I wanted, and from what I've found, I sometimes end up missing the user's own tweets!

I have tried changing each of the parameters on https://dev.twitter.com/docs/streaming-apis/parameters to no avail.

The follow parameter states:

For each user specified, the stream will contain:

   Tweets created by the user.
   Tweets which are retweeted by the user.
   Replies to any Tweet created by the user.
   Retweets of any Tweet created by the user.
   Manual replies, created without pressing a reply button (e.g. “@twitterapi I agree”).

As it's in the docs, I would assume that there is no way to get just that user's tweets without having to filter the results myself (which, as specified before, means I may end up missing the user's own tweets anyway!), but I would love to know if someone knows a way around it.

Before anyone suggests using something such as statuses/user_timeline instead, I know it is able to do what I want, however it has 2 drawbacks that keep me on the streaming API:

  • Each request means I lose a request, and since Twitter is rate limited, I would like to avoid this.
  • Each request has the costly overheads of the HTTP protocol. Too much time is spent negotiating.

Is what I want to do possible? @justinbieber is simply an example of a high overhead Twitter account. I want to use this code to retrieve tweets of many high overhead accounts, thus speed, and the ability to see every tweet from each user are requirements.

4 Answers

In twitter API v2, you can use operators for getting only the tweets you need. To get the tweets from a specific users you can create rule like,

from:username -is:retweet -is:reply

Then use the filtered stream endpoint to get the latest tweets as and when they are posted. You can club multiple usernames in the rules as well.

Resources:

This is a very good example for the same: https://developer.twitter.com/en/docs/tutorials/stream-tweets-in-real-time

How to build a rule: https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/integrate/build-a-rule

Read about filtered stream: https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/introduction

Related