Author

Hi there!

I'm Anil Kumar M.S. A technology enthusiast living in Mysore, India. I'm Passionate about .Net, Web Apps, Cloud, Application design and Architecture, Ux, Javascript and Windows Phone.

A Microsoft Certified Technology Specialist and also a Professional Developer in .Net 4 Web. I like travelling, doing Adventure & Love food.

Let's be friends !



Webpage : www.msanil.com
×

Thursday, December 01, 2011

Talk to Twitter using C#.Net and LinqToTwitter!

Thursday, December 01, 2011

We have seen Linq to Object, Linq to SQL and Linq to XML. This post is regarding a library Linq To Twitter!!

You guessed right. This is a library for .Net developers to do many things with twitter!
As you can imagine, you can query twitter and get details of users, tweets, timelines, friends, trends and you can search twitter for anything. Use Linq to do all these.


Well, this opens up lot of possibilities isn't it?
You can query the followers your friends have, and even see their tweets, their friends & followers and so on. Stop Spying!. No No. Its open, you can! :)

Update your status (Post a new tweet), find the mentions, follow new friends all with just few lines of code!

If you can use this library to a good extent, you can develop a super cool twitter application yourself instead of using any other twitter client or even the Official twitter web. Let us see how to use it.

Official Definition
LINQ to Twitter is a 3rd party LINQ Provider for the Twitter micro-blogging service. It uses standard LINQ syntax for queries and includes method calls for changes via the Twitter API.

Codeplex URL
http://linqtotwitter.codeplex.com

Its Free!, do not hesitate to try.

Quick Start
  1. Open Visual Studio and Create a new Console Application. (I am using console to make things fast. You can use LinqToTwitter on Windows or web apps. Just a matter of referencing the DLL, see below)
  2.  If you use Nuget in Visual Studio, this is the command
    PM> Install-Package LinqToTwitter
    OR
    If you dont have Nuget,
  • Download the DLL from http://linqtotwitter.codeplex.com/releases/77843/download/308277
  • Extract LinqToTwitter.dll from the downloaded zip file and add a reference to it in your console application  you just created.
  1. Open up the program.cs and replace it with below code. All the methods are self explanatory!!
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using LinqToTwitter;
namespace LinqToTwitter_POC
{
    class Program
    {
        static void Main(string[] args)
        {
            //Twitter Userid on which you want to do some operation
            string sUser = "MSGuyTweets";
            
            //Display some information about the user
            ShowUserInfo(sUser);

            //Display a list of people he is following
            ShowFriends(sUser);
       
            //Who is following this user on twitter!
            ShowFollowers(sUser);

            //Show last 10 tweets of this user. (Max tweets you can retrieve is 3200)
            ShowTweets(sUser, 10);
            
            //A small method just to make sure the console stays open
            NoEscape();
        }

        private static void ShowUserInfo(string sUser)
        {
            var objTwitter = new TwitterContext();
            var objAccount = from account in objTwitter.User
                             where account.Type == UserType.Show &&
                                   account.ScreenName == sUser
                             select account;

            var user = objAccount.SingleOrDefault();


            Console.WriteLine("UserName       : " + user.ScreenName);
            Console.WriteLine("Name           : " + user.Name);
            Console.WriteLine("Location       : " + user.Location);
            Console.WriteLine("URL            : " + user.URL);
            Console.WriteLine("# of Followers : " + user.FollowersCount);
            Console.WriteLine("# Following    : " + user.FriendsCount);
            Console.WriteLine("Photo URL      : " + user.ProfileImageUrl);
            Console.WriteLine("-------------------------------------------");
        }

        private static void ShowFriends(string i_sUser)
        {
            var objTwitter = new TwitterContext();
            var objAccount = from account in objTwitter.User
                             where account.Type == UserType.Friends &&
                                   account.ScreenName == i_sUser
                             select account;

            int i = 0;
            foreach (var user in objAccount)
            {
                Console.WriteLine(i + 1 + " Name: {1}\n", user.ID, user.Name);
                i++;
            }
        }

        private static void ShowFollowers(string i_sUser)
        {
            var objTwitter = new TwitterContext();
            var objAccount = from account in objTwitter.User
                             where account.Type == UserType.Followers &&
                                   account.ScreenName == i_sUser
                             select account;

            int i = 0;
            foreach (var user in objAccount)
            {
                Console.WriteLine(i + 1 + " Name: {1}\n", user.ID, user.Name);
                i++;
            }
        }

        private static void ShowTweets(string i_sUser, int i_iCount)
        {
            var objTwitter = new TwitterContext();
            var statusTweets =
                from tweet in objTwitter.Status
                where tweet.Type == StatusType.User
                      && tweet.ID == i_sUser && tweet.Count == i_iCount  // ID for User
                select tweet;

            foreach (var tweet in statusTweets)
            {
                Console.WriteLine(tweet.Text);
                Console.WriteLine();
            }
        }

        private static void NoEscape()
        {
            Console.ReadLine();
        }
    }
}

Now, go ahead and start building your own twitter client!! LinqToTwitter on Code Project has very good documentation. Why don't you give a try?

Author : Anil Kumar M.S
A technology enthusiast living in Mysore, India.
You can connect with me on Twitter @MSGuyTweets or on Facebook.
Email me at ms.anil.kumar@gmail.com


Was this post useful?