Wednesday, 8 June 2016

Live Streaming And Tracking Tweets in .NET

When you google "live streaming tweets" or "real time tweet tracking", you will find loads of APIs those will help you to achieve your task.

Now you will be thinking then why I am writing this post?

Well, there are main two reasons behind it.

1) Since you will find that there are many APIs available in market, which API should you use? How to get started? This blog post will help you on this by putting all things together. It will cover everything from soups to nuts to track live tweets.

2) Most importantly, how to real time track twitter emoji? To cover this point, it's prerequisite to understand first point (e.g how to track tweets real time). We will discuss this second point in my upcoming blog post.

So let's first see how to track tweets real time. Tweetinvi is the best API/Library I have found which has wide range of features to track tweets. As all the features and events of Tweetinvi is very well documented, I am not going to discuss it and rather I will focus on how to get started using it.

1) How to Get Started?


Here is the step by step process to get started with Tweetinvi.

Step-1: Install Tweetinvi NuGet

Tweetinvi is an intuitive .NET C# library to access the Twitter REST API. To install Tweetinvi, run the following command in the Package Manager Console

PM> Install-Package TweetinviAPI




Step-2: Write a code to connect with Twitter



Step-3: Write a code to track tweets based on keyword real-time



You can track tweets based on many criteria such as Geo-Location, Followers and many more. Refer Tweetinvi's official documentation on GitHub for the same.

For example: Write a code to track tweets based on Geo-Location real-time
Below code tracks all live tweets happening at Dubai.




Here is the complete code
 
static void Main(string[] args)
        {
            Auth.SetUserCredentials("key1", "key2", "key4", "key5");
                       
            using (var webClient = new WebClient())
            {
                webClient.Proxy = WebRequest.DefaultWebProxy;
                webClient.Credentials = System.Net.CredentialCache.DefaultCredentials; ;
                webClient.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
                webClient.Headers["User-Agent"] = "MOZILLA/5.0 (WINDOWS NT 6.1; WOW64) APPLEWEBKIT/537.1 (KHTML, LIKE GECKO) CHROME/21.0.1180.75 SAFARI/537.1";

                var creds = new TwitterCredentials("key1", "key2", "key3", "key4");


                var stream = Stream.CreateFilteredStream();                                
                
                stream.AddTrack("Technology");
                stream.MatchingTweetReceived += (sender, a) =>
                {
                    Console.WriteLine(a.Tweet.Text);
                    Console.WriteLine(Environment.NewLine);
                };

                var StreamThread = new Thread(() => stream.StartStreamMatchingAnyCondition());
                StreamThread.Start();
            }
        }

Step-4: See the result!

Running above code snippet will start live streaming of tweets having keyword "Technology" shown in below screen.


This was the just half journey completed for me.
Remember point-2?  How to real time track twitter emoji?

I still need to find a way to track tweets based on emoji icons. Neither Tweetinvi API nor Twitter API provides a direct way to track twitter emoji. This was a challenge for me and finally I was able to crack it. We will see how to track twitter emoji real time in upcoming post.


2) How to real time track twitter emoji

Check it out at Part-2 blog post.


No comments:

Post a Comment