using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Net; using System.IO; using System.Text; using System.Text.RegularExpressions; using System.Xml; using com.google.api; using System.Xml.Xsl; public partial class _default : System.Web.UI.Page { HttpWebRequest myAmazonRequest; XmlDocument myAmazonDoc; XmlDocument myGoogleDoc; XmlDocument myEbayDoc; private bool insufficientArguments = true; // Create a Google Search object GoogleSearchService myGoogleService; string strKeywords; string strSearchIndex; int iResultsPage = 0; protected void Page_Load(object sender, EventArgs e) { GetResults(); } void GetResults() { strKeywords = Request.Params["Keywords"]; // Keywords strSearchIndex = Request.Params["SearchIndex"]; iResultsPage = Convert.ToInt32(Request.Params["ResultsPage"]); if ((strKeywords != "") & (strKeywords != null)) { insufficientArguments = false; PageAsyncTask amazonTask = new PageAsyncTask( new BeginEventHandler(BeginAmazonSearch), new EndEventHandler(EndAmazonSearch), new EndEventHandler(TimeoutAmazonSearch), null); RegisterAsyncTask(amazonTask); PageAsyncTask googleTask = new PageAsyncTask( new BeginEventHandler(BeginGoogleSearch), new EndEventHandler(EndGoogleSearch), new EndEventHandler(TimeoutGoogleSearch), null); RegisterAsyncTask(googleTask); } else { insufficientArguments = true; } } protected void Page_PreRenderComplete(object sender, EventArgs e) { try { // Consolidate the results if (insufficientArguments) { // do nothing } else { // Merge the results of all calls XmlDocument resultsDoc = new XmlDocument(); XmlNode myRoot = resultsDoc.CreateElement("MyMashup"); resultsDoc.AppendChild(myRoot); // first document to merge XmlNode tmpNode; if (myAmazonDoc != null) { tmpNode = resultsDoc.ImportNode(myAmazonDoc.DocumentElement, true); resultsDoc.DocumentElement.AppendChild(tmpNode); } // second document to merge if (myGoogleDoc != null) { tmpNode = resultsDoc.ImportNode(myGoogleDoc.DocumentElement, true); resultsDoc.DocumentElement.AppendChild(tmpNode); } // print merged documents if (resultsDoc != null) { Response.Clear(); Response.ContentType = "text/xml"; //Create the XsltArgumentList. XsltArgumentList xslArg = new XsltArgumentList(); //Create a parameter of a string XsltArgumentList myArgs = new XsltArgumentList(); myArgs.AddParam("resultPage", "", this.iResultsPage); Response.Write(webUtility.DoXSLTransformation(resultsDoc, "AmazonGoogle.xsl", myArgs)); } } } catch (Exception ex) { Response.Write("There was an error whilst merging your results. "); Response.Write(ex.Message); } } // Override the default rendering event. protected override void Render(HtmlTextWriter writer) { if (insufficientArguments) { base.Render(writer); } else { // override and do nothing as in this case we're outputing XML } } IAsyncResult BeginAmazonSearch(object sender, EventArgs e, AsyncCallback cb, object state) { myAmazonRequest = (HttpWebRequest)WebRequest.Create("http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=[YOUR ID HERE]&Operation=ItemSearch&ResponseGroup=Medium&AssociateTag=francshanacom&SearchIndex=" + strSearchIndex + "&Keywords=" + strKeywords + "&ItemPage=" + iResultsPage); return myAmazonRequest.BeginGetResponse(cb, state); } IAsyncResult BeginGoogleSearch(object sender, EventArgs e, AsyncCallback cb, object state) { myGoogleService = new GoogleSearchService(); string strKeywords = Request.Params["keywords"]; // Google uses the result to start from // not the page number to start from int iStartFrom = iResultsPage * 10; // Get the Google Results // Invoke the search method return myGoogleService.BegindoGoogleSearch( "[YOUR ID HERE]", strKeywords, iStartFrom, 10, true, "", true, "", "", "", cb, state); } void EndAmazonSearch(IAsyncResult ar) { myAmazonDoc = webUtility.GetUri(myAmazonRequest, ar); } void EndGoogleSearch(IAsyncResult ar) { GoogleSearchResult myGoogleResults; // Obtain the results myGoogleResults = myGoogleService.EnddoGoogleSearch(ar); myGoogleDoc = new XmlDocument(); string googleNS = "urn:google:search"; XmlNode myRoot = myGoogleDoc.CreateNode(XmlNodeType.Element, "MyGoogle", googleNS); foreach (ResultElement googleItem in myGoogleResults.resultElements) { XmlNode myItem = myGoogleDoc.CreateNode(XmlNodeType.Element, "item", googleNS); // Create a new element node named "title" XmlNode myTitle = myGoogleDoc.CreateNode(XmlNodeType.Element, "title", googleNS); myTitle.InnerText = googleItem.title; myItem.AppendChild(myTitle); // Another for the link XmlNode myLink = myGoogleDoc.CreateNode(XmlNodeType.Element, "link", googleNS); myLink.InnerText = googleItem.URL; myItem.AppendChild(myLink); // One more for the description XmlNode myDescription = myGoogleDoc.CreateNode(XmlNodeType.Element, "description", googleNS); myDescription.InnerText = googleItem.snippet; myItem.AppendChild(myDescription); // Append to the parent myRoot.AppendChild(myItem); } // Add the root to the google results. myGoogleDoc.AppendChild(myRoot); } void TimeoutAmazonSearch(IAsyncResult ar) { Response.Write("Amazon Data temporarily unavailable"); } void TimeoutGoogleSearch(IAsyncResult ar) { Response.Write("Google Data temporarily unavailable"); } protected void cmdSubmit_ServerClick(object sender, EventArgs e) { GetResults(); } }