Here’s the ASP.NET code for the instant search program (Bing first, then Twitter)
BING:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Question.net.live.search;
namespace Question
{
public partial class Instant : System.Web.UI.Page
{
// List of results from service
List _results = new List();
// Variables for search services
private Question.net.live.search.api.LiveSearchService _service = new Question.net.live.search.api.LiveSearchService();
private Question.net.live.search.api.SearchRequest _request = new Question.net.live.search.api.SearchRequest();
private Question.net.live.search.api.SearchResponse _response = new Question.net.live.search.api.SearchResponse();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
_request.AppId = "???"; // put your appId here
Session["_request"] = _request;
Session["_response"] = _response;
Session["_service"] = _service;
}
}
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
// As each row is bound, fill in the url and summary for the item
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink hLink = new HyperLink();
hLink.NavigateUrl = _results[e.Row.DataItemIndex].resultLink;
// Only show portion of URL to avoid lenghty column
if (hLink.NavigateUrl.Length < 33)
{
hLink.Text = "(" + hLink.NavigateUrl.Substring(7, hLink.NavigateUrl.Length - 8) + ")";
}
else
{
hLink.Text = "(" + hLink.NavigateUrl.Substring(7, 25) + "...)";
}
// Tooltip has the whole URL
hLink.ToolTip = hLink.NavigateUrl;
hLink.Target = "_blank";
// Add url and result to cell
TableCell myCell = e.Row.Cells[0];
myCell.Controls.Add(hLink);
myCell = e.Row.Cells[1];
myCell.Style.Add("text-align", "left");
myCell.Text = _results[e.Row.DataItemIndex].resultText;
}
else if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Text = " Link ";
e.Row.Cells[1].Text = " Summary ";
}
}
private void doSearch()
{
// Set source to Web or News depending on user selection
switch (rbMode.SelectedValue)
{
case "Web":
_request = (Question.net.live.search.api.SearchRequest)Session["_request"];
_request.Sources = new Question.net.live.search.api.SourceType[] { Question.net.live.search.api.SourceType.Web};
break;
case "News":
_request = (Question.net.live.search.api.SearchRequest)Session["_request"];
_request.Sources = new Question.net.live.search.api.SourceType[] { Question.net.live.search.api.SourceType.News };
break;
}
_service = (Question.net.live.search.api.LiveSearchService)Session["_service"];
_request.Query = txtQuestion.Text;
try
{
// Perform the search and get response
_response = _service.Search(_request);
// Fill results structure with results for Web or News
switch (rbMode.SelectedValue)
{
case "News":
if (_response.News.Results != null)
{
_results.Clear();
foreach (Question.net.live.search.api.NewsResult wr in _response.News.Results)
{
_results.Add(new ResultItem(wr.Url, wr.Snippet));
}
}
break;
case "Web":
if (_response.Web.Results != null)
{
_results.Clear();
foreach (Question.net.live.search.api.WebResult wr in _response.Web.Results)
{
_results.Add(new ResultItem(wr.Url, wr.Description));
}
}
break;
}
// Bind the gridview with data
gvResults.DataSource = _results;
gvResults.DataBind();
}
catch (Exception ex)
{
// Just ignore errors for now.
}
}
protected void txtQuestion_TextChanged(object sender, EventArgs e)
{
// Perform searches on Bing a
doSearch();
List tweets = TwitterSearch.Query(txtQuestion.Text);
gvTwitter.DataSource = tweets;
gvTwitter.DataBind();
}
protected void rbMode_SelectedIndexChanged(object sender, EventArgs e)
{
_results.Clear();
gvResults.DataSource = _results;
gvResults.DataBind();
txtQuestion.Text = string.Empty;
}
}
}
TWITTER Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
namespace Question
{
public class TwitterSearch
{
private const string _url = "http://search.twitter.com/search.atom?q={0}";
private static XNamespace _atom = "http://www.w3.org/2005/Atom";
public static List Query(string query)
{
XDocument xmlDoc = XDocument.Load(string.Format(_url, query));
var tweets =
(from tweet in xmlDoc.Descendants(_atom + "entry")
select new Tweet
{
Title = (string)tweet.Element(_atom + "title"),
Published =
DateTime.Parse((string)tweet.Element(_atom + "published")),
Link = tweet.Elements(_atom + "link")
.Where(link => (string)link.Attribute("rel") == "alternate")
.Select(link => (string)link.Attribute("href"))
.First()
});
return tweets.ToList();
}
public class Tweet
{
public string Id { get; set; }
public DateTime Published { get; set; }
public string Link { get; set; }
public string Title { get; set; }
}
}
}
One thought on “Instant Search Code”
Comments are closed.