Get the last tweet posted

Jul 30, 2011 at 12:31 AM
Edited Jul 30, 2011 at 12:37 AM

Hi Joe and everyone!

I'm building a desktop application on C# and I want to get just the last tweet posted from the time line, just like the main Twitter page. Let me explain my self, the application is gonna show just the last tweet and it's gonna be updating it every while with a thread but i'm having problems to get this last tweet. I'm using the status demos to test but i'm kinda lost.

This is the code of the method that i'm using to test:

 

 private static void FriendStatusQueryDemo(TwitterContext twitterCtx)
        {
            var friendTweets =
                from tweet in twitterCtx.Status
                where tweet.Type == StatusType.Friends
                select tweet;

            mainWin.timeline.Text = ("\nTweets for " + twitterCtx.UserName + "\n");
            foreach (var tweet in friendTweets)
            {
                mainWin.timeline.Text += (
                    "Friend: " + tweet.User.Name +
                    "\nTweet: " + tweet.Text + "\n" + "\n");
            }
        }

 

So I tried erasing the "+" from the line " mainWin.timeline.Text += ( " but it only shows a tweet like 20 tweets before the last one. I don't know if there's a method that i can use to do this.

I hope you could help me. Thanks :)

 

Jul 30, 2011 at 3:02 AM

I think you need .singleordefault at the end. I read a similiar post not to far back.

Aug 1, 2011 at 10:36 PM
Edited Aug 2, 2011 at 12:00 AM
hahnsolo wrote:

I think you need .singleordefault at the end. I read a similiar post not to far back.

at the end of the foreach? or where?

I tried with this:

var friendTweets = (
                from tweet in twitterCtx.Status
                where tweet.Type == StatusType.Friends
                select tweet)
                .Last();
          
                twitterapp.rTweet.Text = (
                    "Friend: " + friendTweets.User.Name +
                    "\nTweet: " + friendTweets.Text + "\n");
and it doesn't work, it just returns the oldest tweet from the friendTweets not the newer. Please help me.

Coordinator
Aug 2, 2011 at 2:22 AM

Hi sarahimireles,

Instead of a Status Friends query, you might want to try a User Friends query.  Here's the example from the demos:

        /// <summary>
        /// shows how to query friends of a specified user
        /// </summary>
        /// <param name="twitterCtx">TwitterContext</param>
        private static void UserFriendsQueryDemo(TwitterContext twitterCtx)
        {
            var users =
                from tweet in twitterCtx.User
                where tweet.Type == UserType.Friends &&
                      tweet.ID == "JoeMayo" // <-- user to get friends for
                select tweet;

            foreach (var user in users)
            {
                var status =
                    user.Protected || user.Status == null ?
                        "Status Unavailable" :
                        user.Status.Text;

                Console.WriteLine(
                    "ID: {0}, Name: {1}\nLast Tweet: {2}\n",
                    user.Identifier.ID, user.Identifier.ScreenName, status);
            }
        }

If the user list is too long, you can use a Cursor, which is also in the demo code.
@JoeMayo
Sep 8, 2011 at 11:36 PM

Thanks Joe,

I finally made it using arrays and it works.

Thanks for helping me :)