Click here to Skip to main content
Click here to Skip to main content

Get Twitter Trends on Windows Phone 7

, 21 Feb 2011
Rate this:
Please Sign up or sign in to vote.
In this post, I will show you how you can get the list of trends on Twitter.

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.

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:

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.

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

  • api_version
  • trends
  • as_of

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.

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

    /// <span class="code-SummaryComment"><summary>
</span>    /// Gets or sets the text.
    /// <span class="code-SummaryComment"></summary>
</span>    /// <span class="code-SummaryComment"><value>The text.</value>
</span>    public string text { get; set; }

    /// <span class="code-SummaryComment"><summary>
</span>    /// Gets or sets the score.
    /// <span class="code-SummaryComment"></summary>
</span>    /// <span class="code-SummaryComment"><value>The score.</value>
</span>    public int score { get; set; }
}

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

    /// <span class="code-SummaryComment"><summary>
</span>    /// Gets or sets the first_trended_at.
    /// <span class="code-SummaryComment"></summary>
</span>    /// <span class="code-SummaryComment"><value>The first_trended_at.</value>
</span>    public DateTime first_trended_at { get; set; }

    /// <span class="code-SummaryComment"><summary>
</span>    /// Gets or sets the last_trended_at.
    /// <span class="code-SummaryComment"></summary>
</span>    /// <span class="code-SummaryComment"><value>The last_trended_at.</value>
</span>    public DateTime last_trended_at { get; set; }

    /// <span class="code-SummaryComment"><summary>
</span>    /// Gets or sets the name.
    /// <span class="code-SummaryComment"></summary>
</span>    /// <span class="code-SummaryComment"><value>The name.</value>
</span>    public string name { get; set; }

    /// <span class="code-SummaryComment"><summary>
</span>    /// Gets or sets the newly_trending.
    /// <span class="code-SummaryComment"></summary>
</span>    /// <span class="code-SummaryComment"><value>The newly_trending.</value>
</span>    public int newly_trending { get; set; }

    /// <span class="code-SummaryComment"><summary>
</span>    /// Gets or sets the query.
    /// <span class="code-SummaryComment"></summary>
</span>    /// <span class="code-SummaryComment"><value>The query.</value>
</span>    public string query { get; set; }

    /// <span class="code-SummaryComment"><summary>
</span>    /// Gets or sets the trend_index.
    /// <span class="code-SummaryComment"></summary>
</span>    /// <span class="code-SummaryComment"><value>The trend_index.</value>
</span>    public int trend_index { get; set; }

    /// <span class="code-SummaryComment"><summary>
</span>    /// Gets or sets the URL.
    /// <span class="code-SummaryComment"></summary>
</span>    /// <span class="code-SummaryComment"><value>The URL.</value>
</span>    public string url { get; set; }

    /// <span class="code-SummaryComment"><summary>
</span>    /// Gets or sets the category_id.
    /// <span class="code-SummaryComment"></summary>
</span>    /// <span class="code-SummaryComment"><value>The category_id.</value>
</span>    public int category_id { get; set; }

    /// <span class="code-SummaryComment"><summary>
</span>    /// Gets or sets the category_name.
    /// <span class="code-SummaryComment"></summary>
</span>    /// <span class="code-SummaryComment"><value>The category_name.</value>
</span>    public string category_name { get; set; }

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

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

    /// <span class="code-SummaryComment"><summary>
</span>    /// Gets or sets the as_of.
    /// <span class="code-SummaryComment"></summary>
</span>    /// <span class="code-SummaryComment"><value>The as_of.</value>
</span>    public DateTime as_of { get; set; }

    /// <span class="code-SummaryComment"><summary>
</span>    /// Gets or sets the trends.
    /// <span class="code-SummaryComment"></summary>
</span>    /// <span class="code-SummaryComment"><value>The trends.</value>
</span>    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();

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

/// <span class="code-SummaryComment"><summary>
</span>/// Gets the trends.
/// <span class="code-SummaryComment"></summary>
</span>/// <span class="code-SummaryComment"><param name="onGetTrendsCompleted">The on get trends completed.</param>
</span>/// <span class="code-SummaryComment"><param name="onError">The on error.</param>
</span>public static void GetTrends(Action<IEnumerable<Trend>>

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.

kick it on DotNetKicks.com Shout it

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

About the Author

Arik Poznanski
Software Developer (Senior) Verint
Israel Israel
Arik Poznanski is a senior software developer at Verint. He completed two B.Sc. degrees in Mathematics & Computer Science, summa cum laude, from the Technion in Israel.
 
Arik has extensive knowledge and experience in many Microsoft technologies, including .NET with C#, WPF, Silverlight, WinForms, Interop, COM/ATL programming, C++ Win32 programming and reverse engineering (assembly, IL).
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralMy Vote of 5 PinmemberRaviRanjankr28-Feb-11 17:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 22 Feb 2011
Article Copyright 2011 by Arik Poznanski
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid