One of the little gems that Twitter offers is their realtime search. I’m not sure
if I’d characterize it as “realtime” – but it’s pretty close. I’ve seen tweets
that I made show up in these search results within a matter of seconds. So if
you’re looking for good, relevant content on a particular subject, this search
facility is your friend.
And – it has two other features that I think make it ideal for a potential “content
suggester” – first, by adding “search.rss” to the url, all your results come
back as a feed. Secondly, there is a “filter:link” option that will eliminate
much of the glop that goes by on Twitter by only returning tweets that have a
link in them. Here is an example search url for “Silverlight”:
http://search.twitter.com/search.rss?lang=en&filter%3Alinks&rpp=100&q=Silverlight
You can also use the "minus" operator in your searches to filter out unwanted
results,e.g. "silverlight -travel" will return results for silverlight,
but exclude any results having the word "travel" in them.
In order to make this more “fun to use”, I whipped up a quick ASP.NET page with a
search textbox and a DataList to display the results as links. Here’s how the
code works:
The markup is very simple:
<form id="form1" runat="server">
<div>
Search Term: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Search" onclick="Button1_Click" />
<asp:Label ID="Label1" runat="Server"></asp:Label><br />
<ul>
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<li>
<a href='<%# Eval("link") %>'><%# Eval("text")
%></a>
</li>
</ItemTemplate>
</asp:DataList>
</ul>
<br />
</div>
</form>
And the relevant codebehind:
protected void Button1_Click(object sender, EventArgs e)
{
XElement rss = XElement.Load(searchUrl + this.TextBox1.Text);
var items = from item in rss.Descendants("item")
where (GetLink(item.Element("title").Value) != null)
select new
{
Text = item.Element("title").Value,
Link = GetLink(item.Element("title").Value),
PubDate = item.Element("pubDate").Value
};
this.DataList1.DataSource = items;
this.DataList1.DataBind();
this.Label1.Text = "Results based on your search for: " + TextBox1.Text + ":";
}
private string GetLink( string text)
{
string ret = null;
Regex regx = new Regex("http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);
MatchCollection matches = regx.Matches(text);
if (matches.Count >0)
{
ret = matches[0].Value;
}
return ret;
}
}
}
As an added bonus, Twitter has a crossdomain.xml policy file. Therefore, it is easy
to build a Silverlight client for this. Here is some basic sample code:
public class Item
{
public string Title { get; set; }
public string Link { get; set; }
public string PubDate { get; set; }
}
private void GetTweets()
{
WebClient wc = new WebClient();
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
wc.DownloadStringAsync(
new Uri("http://search.twitter.com/search.rss?lang=en&filter:links&rpp=100&q=Silverlight"));
}
void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
XElement rss = XElement.Parse(e.Result );
var items = from item in rss.Descendants("item")
where (GetLink(item.Element("title").Value) != null)
select new Item()
{
Title = item.Element("title").Value,
Link = GetLink(item.Element("title").Value),
PubDate = item.Element("pubDate").Value
};
this.Grid1.ItemsSource = items;
}
private
string GetLink( string text)
{
string
ret = null;
Regex
regx = new Regex("http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);
MatchCollection
matches = regx.Matches(text);
if
(matches.Count >0)
{
ret = matches[0].Value;
}
return
ret;
}
When you put the ASP.NET page into action, you'll get a display page that looks like
this:

You can download the sample here. or play with my live sample on ittyurl.net which I use to add fresh new Silverlight - related links to the site.