In this post, I will show you how you can get the list of trends on Twitter.
In the next post, I’ll show you how you can get the twits related to these trends and generally search twitter for twits.
What The Trend?
Twitter provides a cool site named http://whatthetrend.com/ where you can see the 10 topics which are most discussed currently on twitter.
This site also provides us a web service that returns its results in JSON data format.
The service can be accessed using the following URL:
where {0} should be replaced with your generated application key.
Generate Application Key
Since the twitter trends service provider limits its access using an application key, you must create one.
To do this, go to the site http://api.whatthetrend.com/, on the right side, you have a section titled "Request API key". By filling a small form, you can immediately get a free application key which you can use.
Parsing JSON Data
The data returned from http://whatthetrend.com/ is in JSON data format. We would like to parse this data and convert it to C# classes.
To do so, we will use the class DataContractJsonSerializer which resides in the assembly System.ServiceModel.Web.dll.
The first thing we need to do is to add a reference to this assembly.
The service returns the following JSON formatted data:
{"api_version":"2.0","as_of":"2011-02-05 14:39:19","trends":[{"name":"#scariestwordsever","trend_id":"201146","url":"http:\/\/search.twitter.com\/search?q=%23scariestwordsever","category_id":"9","category_name":"Meme","query":"
#scariestwordsever","description":{"created_at":"2011-02-04T09:05:27+00:00","blurb_id":"322542","text":"Users are tweeting words\/phrases that would be the scariest to ever hear.","score":"1"},"first_trended_at":"2011-02-04T05:27:55+00:00","last_trended_at":"2011-02-05T14:35:01+00:00","newly_trending":"0","trend_index":"2","locked":false},{"name":"#thebadbehaviourtour","trend_id":"201678","url":"http:\/\/search.twitter.com\/search?q=%23thebadbehaviourtour","category_id":"14","category_name":"Other","query":
"#thebadbehaviourtour","description":{"created_at":"2011-02-05T11:56:09+00:00","blurb_id":"322813","text":"@PlanetJedward aka John and Edward have announced new tour dates! @planetJedward are reply to as many fans as they can who put #TheBadBehaviourtour.","score":"0"},"first_trended_at":"2011-02-05T09:18:50+00:00","last_trended_at":"2011-02-05T14:35:01+00:00","newly_trending":"0","trend_index":"3","locked":false},
...
]}
Yea, this looks scary, but I’ve highlighted the interesting parts.
What we have here is a structure that has the properties:
The type of the trends
property is an array of structures, each has the following properties:
name
trend_id
url
category_id
category_name
query
description
first_trended_at
last_trended_at
newly_trending
trend_index
locked
The description
property is itself a structure that has the following properties:
created_at
blurb_id
text
score
All we need to do to get this information in an easy C# way is to define model classes which will have the same properties.
public class TrendDescription
{
public DateTime created_at { get; set; }
public string text { get; set; }
public int score { get; set; }
}
public class Trend
{
public TrendDescription description { get; set; }
public DateTime first_trended_at { get; set; }
public DateTime last_trended_at { get; set; }
public string name { get; set; }
public int newly_trending { get; set; }
public string query { get; set; }
public int trend_index { get; set; }
public string url { get; set; }
public int category_id { get; set; }
public string category_name { get; set; }
public bool locked { get; set; }
}
public class TrendsResults
{
public string api_version { get; set; }
public DateTime as_of { get; set; }
public Trend[] trends { get; set; }
}
to parse the data into these objects, we use the DataContractJsonSerializer
:
DataContractJsonSerializer dataContractJsonSerializer =
new DataContractJsonSerializer(typeof(TrendsResults));
TrendsResults trendsResults =
(TrendsResults)dataContractJsonSerializer.ReadObject(stream);
where stream
is a Stream
object which contains the data in JSON format.
Implement the Get Trends Service
Now that we know how to parse the results, let's wrap this all in a method.
Our GetTrends
method will receive a few delegates as parameters to allow our class to work asynchronously:
Action<IEnumerable<Trend>> onGetTrendsCompleted
, which will be called when the trends are ready to be processed. Action<Exception> onError
, which will be called if there is an error while getting the trends. Action onFinally
, which will be called always, whether there was an exception or not. Think of it as the finally
section on a common try-catch
block.
So the method signature will be:
public static void GetTrends(Action<IEnumerable<Trend>>
onGetTrendsCompleted = null, Action<Exception> onError = null,
Action onFinally = null)
To get the trends, we will use the WebClient class:
WebClient webClient = new WebClient();
webClient.OpenReadCompleted += delegate(object sender, OpenReadCompletedEventArgs e)
{
...
};
webClient.OpenReadAsync(new Uri(string.Format
(WhatTheTrendSearchQuery, WhatTheTrendApplicationKey)));
Where WhatTheTrendSearchQuery
is defined as follows:
private const string WhatTheTrendSearchQuery =
"http://api.whatthetrend.com/api/v2/trends.json?api_key={0}";
The rest of the code handles the different delegates: onGetTrendsCompleted
, onError
, onFinally
. I bring here the method in its full:
public static void GetTrends(Action<IEnumerable<Trend>>
onGetTrendsCompleted = null, Action<Exception> onError = null,
Action onFinally = null)
{
WebClient webClient = new WebClient();
webClient.OpenReadCompleted +=
delegate(object sender, OpenReadCompletedEventArgs e)
{
try
{
if (e.Error != null)
{
if (onError != null)
{
onError(e.Error);
}
return;
}
Stream stream = e.Result;
DataContractJsonSerializer dataContractJsonSerializer =
new DataContractJsonSerializer(typeof(TrendsResults));
TrendsResults trendsResults =
(TrendsResults)dataContractJsonSerializer.ReadObject(stream);
if (onGetTrendsCompleted != null)
{
onGetTrendsCompleted(trendsResults.trends);
}
}
finally
{
if (onFinally != null)
{
onFinally();
}
}
};
webClient.OpenReadAsync(new Uri(string.Format
(WhatTheTrendSearchQuery, WhatTheTrendApplicationKey)));
}
Using the Twitter Trends Service
As usual, the end result is very easy to use, simply call TwitterService.GetTrends static
method and pass a delegate for the “completed” notification.
TwitterService.GetTrends(
(items) => { listbox.ItemsSource = items; },
(exception) => { MessageBox.Show(exception.Message); },
null
);
There is a sample application which can be downloaded here.
Note: This code was first published as part of the “Using Pivot and Panorama Controls” lab found in the Windows Phone Training Kit for Developers, which I wrote for Microsoft.
That’s it for now,
Arik Poznanski.
CodeProject