Best practise for tracking Tweets which contain own @ScreenName

Mar 12, 2014 at 2:52 PM
Using UserStream it is possible to detect Tweets CreatedByFriends/CreatedByAnyone etc. However, what is best practise when one wants to detect tweets which contain @MyScreenName in them. Is this normally done with a FilteredSteam object, or with a UserStream object and some searching of each Tweet as it arrives, or is there a better way.

Thanks.
Coordinator
Mar 12, 2014 at 8:38 PM
Edited Mar 12, 2014 at 8:38 PM
Hi MatR,

It depends on what you are willing to do. If the @MyScreenName is really YOUR ScreenName, I would personally use the UserStream because it will include the private tweets.
var us = Stream.CreateUserStream();
us.TweetCreatedByAnyone += (sender, args) =>
{
    var userMentions = args.Tweet.UserMentions;
    if (userMentions != null && userMentions.Any(x => x.ScreenName == "MY_SCREEN_NAME"))
    {
        // Do whatever
    }
};
us.StartStream();
If you want to get all the public tweets mentioning a specific user screen you can use the FilteredStream.
var fs = Stream.CreateFilteredStream();
fs.AddTrack("@MY_SCREEN_NAME");
fs.MatchingTweetReceived += (sender, args) =>
{
    Console.WriteLine(args.Tweet.Text);
};
fs.StartStreamMatchingAnyCondition();
Hope this helps.
Linvi
Marked as answer by linvi on 3/13/2014 at 11:34 AM
Mar 13, 2014 at 10:52 AM
Hi Linvi

many thanks for your reply.

I too would favour the UserStream, However, in your example the compiler does not accept the .Any method. Is this dowm the datatyping of userMentions variable ?

Thanks
Coordinator
Mar 13, 2014 at 11:00 AM
Hi,

This is a Linq query, please just add the namespace :)
using System.Linq;
Linvi
Mar 13, 2014 at 11:10 AM
Ahhh. Of course. Thanks - that works now.
Mar 18, 2014 at 5:58 PM
Edited Mar 18, 2014 at 5:59 PM
Using the code snippet provided above with filteredstream I am getting some unexpected results


fs.AddTrack("@MY_SCREEN_NAME");

When I track a screen name text with the "@" prefixed, e.g. "@BBC_TopGear" I get no data. But if I match on just the text part ("BBC_TopGear") it works.

Is there a reason why filteredstream should be doing this?

I really wanted to filter on the whole screenname.
Thanks
Coordinator
Mar 19, 2014 at 12:30 AM
When you put the @, it means that it is a tweet that is in reply to the user.
It is not all the tweets containing this user name.

Start your stream with :
fs.AddTrack("@BBC_TopGear");
Go to the BBC_TopGear account and reply to one of their tweet. You will see the reply added to your filteredstream.

Linvi
Marked as answer by linvi on 3/18/2014 at 5:57 PM
Mar 19, 2014 at 10:16 AM
No this is not working for me - either with a reply to one of their tweets, or originating a tweet with their @screenname in the tweet.
If I filter on their screenname without the "@" it works OK. This seems odd to me.

Any ideas?
Thanks

(using 0.9.0.4)
Editor
Mar 19, 2014 at 10:23 AM
Edited Mar 19, 2014 at 10:27 AM
You need to use fs.AddFollow(). It gives all tweets made by the user and all tweets mentioning the user (including retweets of the user's tweets by other users).
var stream = Stream.CreateFilteredStream();
stream.AddFollow(User.GetUserFromScreenName("BBC_TopGear"));
stream.MatchingTweetReceived += (sender, args) => Console.WriteLine(args.Tweet.Text);
stream.StreamStarted += (sender, args) => Console.WriteLine("stream started...");
stream.StartStreamMatchingAnyCondition();
I just tested by replying to bbctopgear and it works as expected.

Hope this helps.

Lokesh
Mar 19, 2014 at 12:01 PM
Thank you for your reply Lokesh.

If .AddFollow() is used, does this actually follow the named user, if so, and the tracking is only required for the duration of the session the stream is running for, shouldnt an explicit unfollow be done when finsihed. Or, is unfollow implicit when the stream is stopped?

Thanks
Editor
Mar 19, 2014 at 12:05 PM
No, this method does not make you follow the user on twitter.

https://dev.twitter.com/docs/api/1.1/post/statuses/filter

The filter api uses the above endpoint. So, AddFollow() simply adds a userID into the follow parameter.

Lokesh
Mar 19, 2014 at 12:45 PM
Thanks. Understood.