Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 213
Simon Avatar asked Dec 31 '13 23:12

Simon


People also ask

What is the difference between Twitter search API and streaming API?

Unlike Twitter's Search API where you are polling data from tweets that have already happened, Twitter's Streaming API is a push of data as tweets happen in near real-time. With Twitter's Streaming API, users register a set of criteria (keywords, usernames, locations, named places, etc.)

What is streaming API Twitter?

The Twitter API allows you to stream public Tweets from the platform in real-time so that you can display them and basic metrics about them.


1 Answers

After using json_decode, you can use the following IF statements to determine what kind of tweet it is:

    // if it is a retweet        
    if (isset($data['retweeted_status']))
    {
         //TODO
    }

    // if it is a reply
    else if (isset($data['in_reply_to_status_id_str']))
    {
         //TODO
    }

    // if it is a mention
    else if (isset($data['in_reply_to_user_id_str']))
    {
         //TODO
    }

    // if it is an original tweet
    else
    {
         //TODO
    }
like image 79
rCevs Avatar answered Sep 28 '22 01:09

rCevs