13

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.

Simon
  • 189
  • 3
  • 11

4 Answers4

6

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
    }
rCevs
  • 111
  • 1
  • 2
0

I had a similar problem and solved with this little piece of code that i extracted from arstechnica

If you are using python pycurl will do the job. Its provides a way to execute a function for every little piece of data received.

import pycurl, json

STREAM_URL = "http://chirpstream.twitter.com/2b/user.json"

USER = "YOUR_USERNAME"
PASS = "XXXXXXXXX"


def on_receive(self, data):
    self.buffer += data
    if data.endswith("rn") and self.buffer.strip():
        content = json.loads(self.buffer)
        self.buffer = ""

        if "text" in content and content['user'] == 'justinbieber':
            print u"{0[user][name]}: {0[text]}".format(content)

conn = pycurl.Curl()
conn.setopt(pycurl.USERPWD, "%s:%s" % (USER, PASS))
conn.setopt(pycurl.URL, STREAM_URL)
conn.setopt(pycurl.WRITEFUNCTION, on_receive)
conn.perform()

You can find more information here Real time twitter stream api

Leonardo Hermoso
  • 838
  • 12
  • 26
0

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

Akash Ranjan
  • 922
  • 12
  • 24
-1

If I am understanding correctly you should be able to use User Streams for this.

Daveee
  • 49
  • 4
  • Unless I'm using it incorrectly, the user streams also don't work for me. For example, if you look at Walmart's Twitter feed (which is fairly constant due to support queries) at https://twitter.com/Walmart/with_replies, you will find that their tweets to other user's are not displayed in the stream. The only tweets I get are those tweeted by @walmart that do not reference any other user's. – Simon Jan 01 '14 at 00:45