Linq not filtering on username

Jun 16, 2009 at 5:17 PM
Edited Jun 16, 2009 at 5:18 PM

The following code only seems to bring back the public stream not the users own public stream, what am I doing wrong?

var twitterCtx = new TwitterContext();

string account = "tigermain";

var tweets = from tweet in twitterCtx.Status
                         where tweet.Type == StatusType.Public
                         && tweet.User.Name == account
                         select tweet;

I've building this as a module for a CMS so users are only likely to know their screen name

Coordinator
Jun 16, 2009 at 6:02 PM

Hi,

The Twitter API doesn't natively support parameters for Public timeline calls.  There are parameters for other Twitter API calls though and LINQ to Twitter does support any Twitter API parameters when available.

Here's a link to the Twitter Public timeline:

http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-public_timeline

And here's the link to the full Twitter API docs, which describes what parameters are available for each call:

http://apiwiki.twitter.com/Twitter-API-Documentation

What you'll need to do is make the call with LINQ to Twitter, materialize the result into objects, and then use LINQ to Objects to perform further operations (such as filtering).  Here's an example:

              var publicTweets =
                (from tweet in twitterCtx.Status
                 where tweet.Type == StatusType.Public
                 select tweet)
                 .ToList();

            var filteredTweets =
                publicTweets.Where(tweet => tweet.User.Name.StartsWith("J"));

            filteredTweets.ToList().ForEach(
                tweet => Console.WriteLine(
                    "User Name: {0}, Tweet: {1}",
                    tweet.User.Name,
                    tweet.Text));

Notice how I called ToList on the initial query to make publicTweets a List<Status>.  Once I had that, I performed a LINQ to Objects query to filter the results.

One of the features I've considered adding is documentation on parameters to indicate whether they are in, out, or in/out.  Would that be useful?

Joe

Jun 16, 2009 at 7:51 PM

Thanks I appreciate what you are saying, but your example in this case will not work as it will only get back appropriate results if they are in the last 20 of the public time line.

I'll have a look to see if there is a more appropriate api call to take advantage of.

Essentially all Im trying to do is get the users last X tweets

Jun 16, 2009 at 7:56 PM

I think this is the method I need to put a call into

http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-user_timeline

Jun 16, 2009 at 8:13 PM

I had a look at how that method is called in the library and it seems to be through the User context, I tried the following but recevied a 401

            var tweets = (from tweet in twitterCtx.User
                          where tweet.ScreenName == account
                              select tweet);

Coordinator
Jun 16, 2009 at 8:16 PM

Does this work?

        /// <summary>
        /// shows how to query status with a screen name for specified number of tweets
        /// </summary>
        /// <param name="twitterCtx">TwitterContext</param>
        private static void UserStatusByNameQueryDemo(TwitterContext twitterCtx)
        {
            Console.WriteLine();

            var lastN = 20;

            var statusTweets =
                from tweet in twitterCtx.Status
                where tweet.Type == StatusType.User
                      && tweet.ScreenName == "JoeMayo"
                      && tweet.Count == lastN
                select tweet;

            foreach (var tweet in statusTweets)
            {
                Console.WriteLine(
                    "(" + tweet.ID + ")" +
                    "[" + tweet.User.ID + "]" +
                    tweet.User.Name + ", " +
                    tweet.Text + ", " +
                    tweet.CreatedAt);
            }
        }

Joe

Jun 16, 2009 at 8:25 PM
Edited Jun 16, 2009 at 8:29 PM

No because tweet.ScreenName doesnt exist is tweet.User.ScreenName which is where I think the problem is

I've realised that my method above (and yours) 401's because you can only call that if you have authenticated

This API is ridulous, all I want to do is produce a module the same as their free embed code!!!!!!

 

 

Actually no scratch that, the api call http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-user_timeline only requires authenticated for protected users the one Im testing is not

Jun 16, 2009 at 8:30 PM
Edited Jun 16, 2009 at 8:33 PM

In fact all I need to do is pass this RSS feed

http://twitter.com/statuses/user_timeline/tigermain.rss

Actually i only want updates not all status' :(

Jun 16, 2009 at 8:36 PM

Right getting somewhere

            var tweets = (from tweet in twitterCtx.Status
                          where tweet.Type == StatusType.User
                          && tweet.ID == account
                          select tweet);

That will get the equiv of the RSS feed

Coordinator
Jun 16, 2009 at 8:37 PM

There are examples of all the API calls in the LinqToTwitterDemo project, in the Program.cs file.

The way to get updates only is by getting the last status ID and saving it for that user.  Then, pass that last status ID as the SinceID and it will only include the updates that are after the SinceID.

Jun 16, 2009 at 8:53 PM

Right this code works, its a little hacky as it returns the users last 200 status' then filters them via linq to remove RT's and Replies

var tweets = (from tweet in twitterCtx.Status
                          where tweet.Type == StatusType.User
                          && tweet.ID == account
                          && tweet.Count == 200
                          select tweet).ToList();

var filteredTweets = tweets.Where(tweet => tweet.InReplyToScreenName.Length == 0 && tweet.InReplyToStatusID.Length == 0 && tweet.InReplyToUserID.Length == 0).Take(count).ToList();

Sep 6, 2009 at 11:26 PM
Edited Sep 6, 2009 at 11:33 PM

Tigermain,

I just wanted to post how I am filtering out Replies and RT's:

// returns the users last status' then filters them via linq to remove Replies and Retweets

var filteredTweets = tweets.Where(tweet => tweet.InReplyToScreenName.Length == 0 && tweet.Text.StartsWith("RT @") == false).ToList();

 

If you see an issue with this let me know...