-
Notifications
You must be signed in to change notification settings - Fork 218
Filtered Streams
Like the sample stream, the filtered streams is a stream only returning tweets. The difference between both of these streams is that you can filter the type of tweets returned by a filtered stream based on 3 criteria.
- Filter by keywords and type of tweets using the
AddTrack
method. - Filter by location using the
AddLocation
method. - Filter by user using the
AddFollow
method.
When at least 1 filter has been set up, you can start the stream. You can set up each of the criteria at the same time and each of them can contain multiple values.
var stream = Stream.CreateFilteredStream();
stream.AddTrack("tweetinvi");
stream.MatchingTweetReceived += (sender, args) =>
{
Console.WriteLine("A tweet containing 'tweetinvi' has been found; the tweet is '" + args.Tweet + "'");
};
stream.StartStreamMatchingAllConditions();
The first and most used filter is keywords. Twitter parameter is named Track
. Tweetinvi respected their naming choice and called them tracks too.
As a first note, please be aware that a track cannot contain more than 60 characters.
OR OPERATOR : You can add multiple tracks to the same filtered stream. When you do, twitter consider them as OR conditions. Adding 'tweetinvi' as a first track and 'linvi' in a second track will result in a stream that returns all tweets mentioning 'tweetinvi' OR 'linvi'.
AND OPERATOR : Separating multiple keywords within the same track enable you to use the AND operator. For example using the track 'tweetinvi linvi' will make the stream only returns tweets that contains both 'tweetinvi' AND 'linvi'.
Track URL : Please be aware that if you want to track an URL the 'wwww.' must be omitted. Failure to do so will result in the stream to not return any result for this specific track.
Example:
// Let's get tweets containing 'linvi'
stream.AddTrack("linvi");
// OR tweets containing both 'tweetinvi' AND 'rocks'.
stream.AddTrack("tweetinvi rocks");
Twitter also allow you to filter tweets based on their geographic coordinates.
// Either provide the 2 coordinates
stream.AddLocation(new Coordinates(-74,40), new Coordinates(-73,41));
// Or a location object
var centerOfNewYork = new Location(new Coordinates(-74,40), new Coordinates(-73,41));
stream.AddLocation(centerOfNewYork);