Filter #tag tweets for a specific account using Twitter Streaming API

106 Views Asked by At

I am able to get tweets from a specific account using the streaming API. I can also manage to get tweets for specific #tags like below:

endpoint.trackTerms(Lists.newArrayList("twitterapi", "@myTwitter"));

and

endpoint.trackTerms(Lists.newArrayList("twitterapi", "#yolo"));

I wonder how to merge these two queries as I want to get specific tweets (#yolo) from a specific user (@myTwitter)

Code can be found here https://github.com/twitter/hbc

1

There are 1 best solutions below

0
lrnzcig On

Take a look to Twitter's documentation on the streaming API, how to track terms:

A comma-separated list of phrases which will be used to determine what Tweets will be delivered on the stream. A phrase may be one or more terms separated by spaces, and a phrase will match if all of the terms in the phrase are present in the Tweet, regardless of order and ignoring case. By this model, you can think of commas as logical ORs, while spaces are equivalent to logical ANDs (e.g. ‘the twitter’ is the AND twitter, and ‘the,twitter’ is the OR twitter).

twitter-hbc only allows to track terms separated by commas, so if you do this,

endpoint.trackTerms(Lists.newArrayList("@myTwitter", "#yolo"));

You will actually be doing @myTwitter OR #yolo, take a look to the implementation of the method trackTerms,

/**
  * @param terms a list of Strings to track. These strings should NOT be url-encoded.
  */
public StatusesFilterEndpoint trackTerms(List<String> terms) {
    addPostParameter(Constants.TRACK_PARAM, Joiner.on(',').join(terms));
    return this;
}

Instead of using trackTerms, you could add the terms directly to the endpoint like this,

endpoint.addPostParameter(Constants.TRACK_PARAM, Joiner.on(' ').join(Lists.newArrayList("twitterapi", "#yolo")));

Or of course you could create a new method.

Hope it helps.