Simon
Simon

Reputation: 189

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:

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.

Upvotes: 13

Views: 3000

Answers (4)

Akash Ranjan
Akash Ranjan

Reputation: 1074

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

Upvotes: 0

Leonardo Hermoso
Leonardo Hermoso

Reputation: 838

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

Upvotes: 0

rCevs
rCevs

Reputation: 111

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
    }

Upvotes: 6

Daveee
Daveee

Reputation: 49

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

Upvotes: -1

Related Questions