How To Track Twitter Emojis Real Time?

In previous post we talked about “How to do live streaming and tracking the tweets?” If you haven’t read that post, then I would recommend to Devour itfirst before moving further.

You will find many articles on internet about live streaming and tracking the tweets, but you will rarely find a way to track twitter emojis real time. There is only one example of tracking twitter emojis real time and it is http://emojitracker.com/ This tracker is awesome and shows live emojis getting used world wide. I have simillar requirement with a small change. My requirement was to track emojis for particular city and show it real time to decide the mood of city. For example: My client wanted to know the mood of city on internet by tracking emojis on twitter.

Now the link that I shared above (http://emojitracker.com/) was developed on non .NET platform and it was not easy for me to use. Moreover I haven’t found any article to track twitter emojis real time using .NET, hence after good amount of research and development I was able to achieve this real time emoji tracking. I am writing this blog post to help my friends who has similar requirement. It’s easy to track tweet based on different criteria such as location and text using TweetInvi API. But the challenge was “How to track emojis?” as it is NOT text. Moreover I needed to store count of these emjois in order to know the mood of cities. Finally this is how it was achieved. Steps: 1) Connect with TweetInvi API and track tweets based on your location (e.g. Dubai in my case)

2) Track twitter emojis by comparing emojis unicode with tweet text. You can a complete list of all           twitter emojis at http://unicode.org/emoji/charts/full-emoji-list.html 3) Store the count of different emojis in database to decide mood of the city.

Finally we created a nice dashboard page which will show LIVE mood of the city. You can also check mood of the city at particular time by changing filter criteria.

Here is the complete code set of above steps.

   class Program     {         static SqlConnection con;         static SqlCommand cmd;         static void Main(string[] args)         {             Auth.SetUserCredentials("key1", "key2", "key3", "key4");              con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlCon"].ConnectionString);             if (con.State != ConnectionState.Open)                 con.Open();             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();                 var dubaiCords = new Location(new Coordinates(51.596696, 24.369107), new Coordinates(56.348645, 25.005892));                                  stream.AddLocation(dubaiCords);                                 stream.MatchingTweetReceived += (sender, a) =>                 {                     if (a.Tweet.CreatedAt.Day == DateTime.Now.Day)                     {                         try                         {                             InsertTweet(a.Tweet.Text, a.Tweet.CreatedBy.Location == null ? string.Empty : a.Tweet.CreatedBy.Location);                         }                         catch (Exception ex)                         {                             Console.WriteLine("Error "  + ex.ToString());                         }                                              }                 };                  var StreamThread = new Thread(() => stream.StartStreamMatchingAnyCondition());                 StreamThread.Start();                  Console.Read();                 con.Close();                  cmd.Dispose();             }         }          public static void InsertTweet(string tweet, string location)         {             Console.WriteLine("Streaming...");             cmd = new SqlCommand("UpdateMood", con);             cmd.CommandType = CommandType.StoredProcedure;             cmd.Parameters.Clear();              cmd.Parameters.AddWithValue("@Hour", DateTime.Now.Hour);             cmd.Parameters.AddWithValue("@Date", DateTime.Now.ToString("yyyy-MM-dd"));              if (tweet.Contains("😀") || tweet.Contains("😁") || tweet.Contains("😂") || tweet.Contains("😃") || tweet.Contains("😄") || tweet.Contains("😆")                 || tweet.Contains("😉") || tweet.Contains("😎") || tweet.Contains("😍") || tweet.Contains("☺") || tweet.Contains("🙂") || tweet.Contains("😇")                 || tweet.Contains("🎉") || tweet.Contains("😊") || tweet.Contains("😋") || tweet.Contains("😘") || tweet.Contains("😗") || tweet.Contains("😙")                 || tweet.Contains("😚") || tweet.Contains("☺") || tweet.Contains("😜") || tweet.Contains("😝"))             {                                 cmd.Parameters.AddWithValue("@Mood", "Happy");                 cmd.ExecuteNonQuery();                 InsertTweetEntry(tweet,location);             }              if (tweet.Contains("🙁") || tweet.Contains("😦") || tweet.Contains("🙍") || tweet.Contains("😠") || tweet.Contains("😡") || tweet.Contains("💢")                 || tweet.Contains("🗯") || tweet.Contains("😥") || tweet.Contains("😫") || tweet.Contains("😔") || tweet.Contains("😕")                 || tweet.Contains("☹") || tweet.Contains("😖") || tweet.Contains("😞") || tweet.Contains("😟") || tweet.Contains("😢")                 || tweet.Contains("😭") || tweet.Contains("😦") || tweet.Contains("😧") || tweet.Contains("😨") || tweet.Contains("😩")                 || tweet.Contains("😰"))             {                              cmd.Parameters.AddWithValue("@Mood", "Stressed");                 cmd.ExecuteNonQuery();                 InsertTweetEntry(tweet, location);             }              if (tweet.Contains("✍") || tweet.Contains("✍🏼") || tweet.Contains("📔") || tweet.Contains("📕") || tweet.Contains("📖") || tweet.Contains("📗")                 || tweet.Contains("📘") || tweet.Contains("📙") || tweet.Contains("📚") || tweet.Contains("📓"))             {                              cmd.Parameters.AddWithValue("@Mood", "Studying");                 cmd.ExecuteNonQuery();                 InsertTweetEntry(tweet, location);             }              if (tweet.Contains("🍔") || tweet.Contains("🍟") || tweet.Contains("🍕") || tweet.Contains("🍲") || tweet.Contains("🍚") || tweet.Contains("🍛")                 || tweet.Contains("🍜") || tweet.Contains("🍝") || tweet.Contains("🍠") || tweet.Contains("🍣") || tweet.Contains("🍽") || tweet.Contains("🍷")                 || tweet.Contains("🍸") || tweet.Contains("🍴"))             {                              cmd.Parameters.AddWithValue("@Mood", "Hungry");                 cmd.ExecuteNonQuery();                 InsertTweetEntry(tweet, location);             }              if (tweet.Contains("💻") || tweet.Contains("🖥") || tweet.Contains("🎥") || tweet.Contains("📺") || tweet.Contains("▶") || tweet.Contains("◀"))                             {                                 cmd.Parameters.AddWithValue("@Mood", "Youtubing");                 cmd.ExecuteNonQuery();                 InsertTweetEntry(tweet, location);             }              if (tweet.Contains("⚽") || tweet.Contains("⚾") || tweet.Contains("🏀") || tweet.Contains("🏈") || tweet.Contains("🏉") || tweet.Contains("📣")                 || tweet.Contains("📯"))             {                                 cmd.Parameters.AddWithValue("@Mood", "Cheering");                 cmd.ExecuteNonQuery();                 InsertTweetEntry(tweet, location);             }          }          private static void InsertTweetEntry(string tweet, string location)         {             //Finally record the tweet             cmd = new SqlCommand("InsertTweet", con);             cmd.CommandType = CommandType.StoredProcedure;             cmd.Parameters.Clear();             cmd.Parameters.AddWithValue("@Tweet", tweet);             cmd.Parameters.AddWithValue("@Location", location);             int index = cmd.ExecuteNonQuery();                     }     }       

Hope you liked it. Stay tuned for more such posts!

Leave a Reply