Getting/Filtering Specific Tweets

Jun 10, 2013 at 3:39 PM
Edited Jun 10, 2013 at 3:41 PM
I have created some simple test code that returns tweets associated with a particular twitter account:
using (var twitterCtx = new TwitterContext(auth))
{
   var queryResults = twitterCtx.Search
      .Where(s => (
         (s.Type == SearchType.Search) &&
         (s.Query == "@MyTwitterName") &&
         (s.Count == 3)
      ));

   var result = queryResults.Single();

   return JsonUtils.JsonSerializer(result.Statuses);
}
Which seems to work well, but what I really want to be able to do is only collect tweets that were originated/posted by "@MyTwitterName" can anyone tell me if this is possible?

Thanks.
Coordinator
Jun 10, 2013 at 4:03 PM
Hi,

If you're authenticated as the user, you can use the StatusType.Home query. If you need to authenticate as yourself, but look at someone else's tweets, you can use the StatusType.User query.

@JoeMayo
Jun 10, 2013 at 4:34 PM
Thanks Joe, that's much cleaner, but I notice that the feed still contains re-tweets. Is it possible that these can be removed/filtered from the result? Essentially I really just want to pick up the last 'n' tweets created directly from my account so excluding re-tweets and replies, i.e. messages created by me. Apologies, I should have been clearer in the first place.
Coordinator
Jun 10, 2013 at 7:05 PM
Some APIs have a property named IncludeRetweets, which you can set to false. You'll need to look at the documentation for each API to see what is available. e.g. StatusType.Home doesn't have an option to include retweets, but StatusType.User does.

@JoeMayo
Jun 11, 2013 at 7:14 AM
Thank you Joe. I am embarrassed to admit that I hadn't made the connection between the Linq2Twitter API and the underlying Twitter API and had 'assumed' that I could simply query against any attributes of the collection.

Having played around, I haven't been able to find an API call that does exactly what I want, i.e. return tweets originated by a specific account with all replies and re-tweets excluded. The only way I have found is to make the initial API call to get a local data set and then filter that data to give the result:
using (var twitterCtx = new TwitterContext(auth))
{
   // Get the initial data set using the API call
   var queryResults = twitterCtx.Status
      .Where(s => (
         (s.Type == StatusType.User) &&
         (s.ScreenName == search) &&
         (s.ExcludeReplies == true) &&
         (s.IncludeMyRetweet == false) &&
         (s.Count == initialDataCount)
      ))

      // Now filter the API results
      .Where(s => (
         (s.RetweetCount < 1)
      ))
      .Take(resultCount)
      .ToList();

   return JsonUtils.JsonSerializer(queryResults);
}
The problem with this is, of course, getting the right ratio of initial data count to result count to ensure that there are enough target records included in the data. This makes this approach somewhat expensive in terms of returning more data than you actually need and somewhat fragile.

I am hoping that I have missed something obvious and that there is a better approach - any suggestions would be much appreciated.
Coordinator
Jun 11, 2013 at 2:36 PM
It can be confusing because you are querying a REST API, as opposed to a relational data store. All you have available is what the Twitter API exposes. If you get back more data than you want, you should materialize the query and then do a LINQ to Objects query to filter the results. LINQ to Twitter only supports the parameters that the Twitter API exposes.

@JoeMayo