In the previous post, we’ve seen how to get a list of trends on twitter.
In this post, we continue to explore twitter service. We will see how to search for twits on twitter.
Searching Twitter
Twitter exposes a JSON-based search service in the following address:
where {0}
should be replaced with your search term.
The results are returned in JSON data format, so we will use again the DataContractJsonSerializer. For more details on how to use it, check out the previous post.
Defining Twit and TwitterResults
Similarly to what we’ve done in the previous post, we need to declare some C# model classes that will be used to parse the twitter JSON results.
public class Twit
{
public string text { get; set; }
public string DecodedText
{
get
{
return HttpUtility.HtmlDecode(text);
}
}
public string from_user { get; set; }
public string profile_image_url { get; set; }
public DateTime created_at { get; set; }
}
public class TwitterResults
{
public Twit[] results { get; set; }
}
Note that the twit text should be HTML-decoded before using it, so I’ve added a DecodedText
property to do the job.
Implement the Twitter Search Service
We will implement a static
method named TwitterService.Search
that will receive the search term as its first parameter and a few delegates to allow our class to work asynchronously:
Action<IEnumerable<Twit>> onSearchCompleted
, which will be called when the twitter search is complete.
Action<Exception> onError
, which will be called if there is an error while searching twitter.
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 Search(string searchText, Action<IEnumerable<Twit>>
onSearchCompleted = null, Action<Exception> onError = null, Action onFinally = null)
To search twitter, we will use the WebClient class, yet again:
WebClient webClient = new WebClient();
webClient.OpenReadCompleted += delegate(object sender, OpenReadCompletedEventArgs e)
{
...
};
string encodedSearchText = HttpUtility.UrlEncode(searchText);
webClient.OpenReadAsync(new Uri
(string.Format(TwitterSearchQuery, encodedSearchText)));
where TwitterSearchQuery
is defined as follows:
private const string TwitterSearchQuery =
"http://search.twitter.com/search.json?q={0}";
The rest of the code handles the different delegates: on SearchCompleted
, onError
, onFinally
. I bring here the method in its full:
public static void Search(string searchText,
Action<IEnumerable<Twit>> onSearchCompleted = 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(TwitterResults));
TwitterResults twitterResults =
(TwitterResults)dataContractJsonSerializer.ReadObject(stream);
if (onSearchCompleted != null)
{
onSearchCompleted(twitterResults.results);
}
}
finally
{
if (onFinally != null)
{
onFinally();
}
}
};
string encodedSearchText = HttpUtility.UrlEncode(searchText);
webClient.OpenReadAsync(new Uri(string.Format
(TwitterSearchQuery, encodedSearchText)));
}
Using the Twitter Search Service
Using the service is easy:
TwitterService.Search(
textbox.Text,
(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.