Get Twitter Trends on Windows Phone 7

February 21, 2011

In this post I will show you how you can get the list of trends on twitter.
On the next post I’ll show you how you can get the twits related to these trends and generally search twitter for twits.

image

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:

http://api.whatthetrend.com/api/v2/trends.json?api_key={0}

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 titles "Request API key". By filling a small form you can immediately get a free application key which you can use.

image

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.

image

The service returns 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 have the properties:

  • api_version
  • as_of
  • trends

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.

/// <summary>
/// Model for twitter trend description
/// </summary>
public class TrendDescription
{
    /// <summary>
    /// Gets or sets the created_at.
    /// </summary>
    /// <value>The created_at.</value>
    public DateTime created_at { get; set; }

    /// <summary>
    /// Gets or sets the text.
    /// </summary>
    /// <value>The text.</value>
    public string text { get; set; }

    /// <summary>
    /// Gets or sets the score.
    /// </summary>
    /// <value>The score.</value>
    public int score { get; set; }
}

/// <summary>
/// Model for twitter trend
/// </summary>
public class Trend
{
    /// <summary>
    /// Gets or sets the description.
    /// </summary>
    /// <value>The description.</value>
    public TrendDescription description { get; set; }

    /// <summary>
    /// Gets or sets the first_trended_at.
    /// </summary>
    /// <value>The first_trended_at.</value>
    public DateTime first_trended_at { get; set; }

    /// <summary>
    /// Gets or sets the last_trended_at.
    /// </summary>
    /// <value>The last_trended_at.</value>
    public DateTime last_trended_at { get; set; }

    /// <summary>
    /// Gets or sets the name.
    /// </summary>
    /// <value>The name.</value>
    public string name { get; set; }

    /// <summary>
    /// Gets or sets the newly_trending.
    /// </summary>
    /// <value>The newly_trending.</value>
    public int newly_trending { get; set; }

    /// <summary>
    /// Gets or sets the query.
    /// </summary>
    /// <value>The query.</value>
    public string query { get; set; }

    /// <summary>
    /// Gets or sets the trend_index.
    /// </summary>
    /// <value>The trend_index.</value>
    public int trend_index { get; set; }

    /// <summary>
    /// Gets or sets the URL.
    /// </summary>
    /// <value>The URL.</value>
    public string url { get; set; }

    /// <summary>
    /// Gets or sets the category_id.
    /// </summary>
    /// <value>The category_id.</value>
    public int category_id { get; set; }

    /// <summary>
    /// Gets or sets the category_name.
    /// </summary>
    /// <value>The category_name.</value>
    public string category_name { get; set; }

    /// <summary>
    /// Gets or sets a value indicating whether this <see cref="Trend"/> is locked.
    /// </summary>
    /// <value><c>true</c> if locked; otherwise, <c>false</c>.</value>
    public bool locked { get; set; }
}

/// <summary>
/// Model for trends results
/// </summary>
public class TrendsResults
{
    /// <summary>
    /// Gets or sets the api_version.
    /// </summary>
    /// <value>The api_version.</value>
    public string api_version { get; set; }

    /// <summary>
    /// Gets or sets the as_of.
    /// </summary>
    /// <value>The as_of.</value>
    public DateTime as_of { get; set; }

    /// <summary>
    /// Gets or sets the trends.
    /// </summary>
    /// <value>The trends.</value>
    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, lets 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();

// register on download complete event
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:

/// <summary>
/// Gets the trends.
/// </summary>
/// <param name="onGetTrendsCompleted">The on get trends completed.</param>
/// <param name="onError">The on error.</param>
public static void GetTrends(Action<IEnumerable<Trend>> onGetTrendsCompleted = null, Action<Exception> onError = null, Action onFinally = null)
{
    WebClient webClient = new WebClient();

    // register on download complete event
    webClient.OpenReadCompleted += delegate(object sender, OpenReadCompletedEventArgs e)
    {
        try
        {
            // report error
            if (e.Error != null)
            {
                if (onError != null)
                {
                    onError(e.Error);
                }
                return;
            }

            // convert json result to model
            Stream stream = e.Result;
            DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(TrendsResults));
            TrendsResults trendsResults = (TrendsResults)dataContractJsonSerializer.ReadObject(stream);

            // notify completed callback
            if (onGetTrendsCompleted != null)
            {
                onGetTrendsCompleted(trendsResults.trends);
            }
        }
        finally
        {
            // notify finally callback
            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.

Add comment
facebook linkedin twitter email

Leave a Reply