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:
|
|
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.
|
|