I was excited to find that Twitter had a JSON (Javascript Object Notation) endpoint for the current trending topics and decided to write a simple consumer which can read this and then spit it out in a simple console. And JSON being so simple and more or less “universal” meant that there are multiple implementations for .NET. Of course if you got lots of bandwidth you can roll out your own parser.

I ended up using Json.NET , which in addition to being OpenSource is also one of the most robust utilities which makes working with JSON formatted data dead simple.

The code for the console app is quite straightforward. The static function ReadTrends() retrieves the JSON string from twitter which is then consumed and extracted. The only tricky part was using a constant key; the easiest way I could think of doing this was to replace the date-time stamp with a literal and then use that literal.

Of course this will fail if you the function ReadTrends() is called at (or just before midnight) on Dec 31st and the code returns to the main() function in the new year. I don’t think this is something I am going to put in production and am not going to be too worried about this behaviour.

At the time of writing this, the twitter trends (in JSON) are:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
{“trends”:{“2011-03-04 17:18:01”:[{“name”:”#coisasderetiro”,”events”:null,”query”:”#coisasderetiro”,”promoted_content”:null},
{“name”:”#tigerblood”,”events”:null,”query”:”#tigerblood”,”promoted_content”:null},
{“name”:”#blackpeoplemovies”,”events”:null,”query”:”#blackpeoplemovies”,”promoted_content”:null},
{“name”:”Frying Nemo”,”events”:null,”query”:”Frying Nemo”,”promoted_content”:null},
{“name”:”Acoustic Aftermath”,”events”:null,”query”:”Acoustic Aftermath”,”promoted_content”:null},
{“name”:”Blade Runner”,”events”:null,”query”:”Blade Runner”,”promoted_content”:null},
{“name”:”Fun Race”,”events”:null,”query”:”Fun Race”,”promoted_content”:null},
{“name”:”Bandra”,”events”:null,”query”:”Bandra”,”promoted_content”:null},
{“name”:”Mike Huckabee”,”events”:null,”query”:”Mike Huckabee”,”promoted_content”:null},
{“name”:”Arctic Monkeys”,”events”:null,”query”:”Arctic Monkeys”,”promoted_content”:null}]},”as_of”:1299259081}

And here is the output in the console. I can see Charlie Sheen’s #tigerblood is still trending; and wonder what Artic Monkeys are upto – is there new album out or something?

#coisasderetiro #tigerblood #blackpeoplemovies Frying Nemo Acoustic Aftermath Blade Runner Fun Race Bandra Mike Huckabee Arctic Monkeys All done. Press any key to continue...

And finally here is the code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using System.Net;
using System.IO;
 
namespace TwitterTrends
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //get the trends
                string temp = ReadTrends();
 
                //as the key is a datetime stamp (which is constantly moving), need something constant to interrogate.
                //simplest way is to find out the year and then take 19 characters from that which is the datetime stamp
                //replace that with a literal (DESIGEEK.COM in my case) and then use the literal. I don't think I will
                //be trending on Twitter; but if you are worried then you can use something like a GUID.
                //
                // For example at the time of writing this the datetime stamp was: "2011-03-02 14:20:00"
                string theDate = temp.Substring(temp.IndexOf(DateTime.Now.Year.ToString()), 19);
                temp = temp.Replace(theDate, "DESIGEEK.COM");
 
                //parse the string for the literal
                JObject trends = JObject.Parse(temp);
                var twitterTrends = from t in trends["trends"]["DESIGEEK.COM"]
                                    select t.Value<string>("name");
 
                //iterate through
                foreach (var item in twitterTrends)
                {
                    Console.WriteLine(item);
                }
 
            }
            catch (Exception ex)
            {
                Console.WriteLine("Following error occured:\n" + ex.ToString());
            }
            Console.WriteLine("\nAll done. Press any key to continue...");
            Console.ReadKey();
        }
 
        private static string ReadTrends()
        {
            //talk to twitter
            WebRequest theRequest = HttpWebRequest.Create("http://search.twitter.com/trends/current.json");
            HttpWebResponse theResponse = (HttpWebResponse)theRequest.GetResponse();
 
            //extract the data
            Stream stream = theResponse.GetResponseStream();
            StreamReader reader = new StreamReader(stream);
            string temp = reader.ReadToEnd();
 
            //clean up
            reader.Close();
            stream.Close();
            theResponse.Close();
 
            return temp;
        }
    }
}