topical media & game development
mashup-amazon-13-13-05-GoogleAmazon-code.txt / txt
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;
public partial class fs : System.Web.UI.Page
{
private WebRequest _amazonRequest;
private WebRequest _yahooRequest;
private WebRequest _ebayRequest;
private XmlDocument _amazonDoc;
private XmlDocument _yahooDoc;
private XmlDocument _ebayDoc;
private bool _insufficientArguments = false;
protected void Page_Load(object sender, EventArgs e)
{
string strKeywords = Request.QueryString["Keywords"]; // Keywords
if ((strKeywords != "") & (strKeywords != null))
{
PageAsyncTask amazonTask = new PageAsyncTask(
new BeginEventHandler(BeginAmazonAsyncOperation),
new EndEventHandler(EndAmazonAsyncOperation),
new EndEventHandler(TimeoutAmazonAsyncOperation),
null);
RegisterAsyncTask(amazonTask);
PageAsyncTask ebayTask = new PageAsyncTask(
new BeginEventHandler(BeginEbayAsyncOperation),
new EndEventHandler(EndEbayAsyncOperation),
new EndEventHandler(TimeoutEbayAsyncOperation),
null);
RegisterAsyncTask(ebayTask);
PageAsyncTask yahooTask = new PageAsyncTask(
new BeginEventHandler(BeginYahooAsyncOperation),
new EndEventHandler(EndYahooAsyncOperation),
new EndEventHandler(TimeoutYahooAsyncOperation),
null);
RegisterAsyncTask(yahooTask);
}
else
{
_insufficientArguments = true;
}
}
protected void Page_PreRenderComplete(object sender, EventArgs e)
{
try
{
// This is the last thing to happen
///////////////////////
if (_insufficientArguments)
{
// do nothing
}
else
{
// Merge the results of all 3 calls
XmlDocument resultsDoc = new XmlDocument();
XmlNode cont = resultsDoc.CreateElement("BaeBo");
resultsDoc.AppendChild(cont);
// first document to merge
XmlNode imported;
if (_amazonDoc != null)
{
imported = resultsDoc.ImportNode(_amazonDoc.DocumentElement, true);
resultsDoc.DocumentElement.AppendChild(imported);
}
// second document to merge
if (_yahooDoc != null)
{
imported = resultsDoc.ImportNode(_yahooDoc.DocumentElement, true);
resultsDoc.DocumentElement.AppendChild(imported);
}
// third document to merge
if (_ebayDoc != null)
{
imported = resultsDoc.ImportNode(_ebayDoc.DocumentElement, true);
resultsDoc.DocumentElement.AppendChild(imported);
}
// print merged documents
if (resultsDoc != null)
{
Response.Clear();
Response.ContentType = "text/xml";
if (Request.QueryString["format"] == "rss")
{
Response.Write(webUtility.TransformDoc(resultsDoc, "ayerss"));
}
else
{
Response.Write(webUtility.ConvertXmlToString(resultsDoc));
}
//Response.Write(webUtility.TransformDoc(resultsDoc, "ayerss"));
}
}
}
catch (Exception ex)
{
Response.Write("There was an error whilst merging your results. ");
Response.Write(ex.Message);
}
}
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 BeginAmazonAsyncOperation(object sender, EventArgs e, AsyncCallback cb, object state)
{
string strKeywords = webUtility.nvl(Request.QueryString, "Keywords");
string strSearchIndex = webUtility.nvl(Request.QueryString, "SearchIndex");
_amazonRequest = WebRequest.Create("http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=16KBB0XN5XP4WSNNVKG2&Operation=ItemSearch&ResponseGroup=Medium&AssociateTag=francshanacom&" + strKeywords + strSearchIndex);
return _amazonRequest.BeginGetResponse(cb, state);
}
IAsyncResult BeginYahooAsyncOperation(object sender, EventArgs e, AsyncCallback cb, object state)
{
string strKeywords = Request.QueryString.Get("keywords");
_yahooRequest = WebRequest.Create("http://api.shopping.yahoo.com/ShoppingService/V1/productSearch?appid=francisshanahan&results=10&query=" + strKeywords);
return _yahooRequest.BeginGetResponse(cb, state);
}
IAsyncResult BeginEbayAsyncOperation(object sender, EventArgs e, AsyncCallback cb, object state)
{
string strKeywords = Request.QueryString.Get("keywords");
_ebayRequest = WebRequest.Create("http://rest.api.ebay.com/restapi?CompatibilityLevel=445&CallName=GetSearchResults&RequestToken=UburRjOvd0Q%3D**25lhKIc3hPhgPpZ8ojZzv1TLm1I%3D&RequestUserId=francisshanahan&Schema=1&Currency=1&TrackingProvider=1&TrackingId=1889305&SearchInDescription=1&MaxResults=10&Query=" + strKeywords);
return _ebayRequest.BeginGetResponse(cb, state);
}
void EndAmazonAsyncOperation(IAsyncResult ar)
{
GetXml(_amazonRequest, ar, ref _amazonDoc);
}
void EndYahooAsyncOperation(IAsyncResult ar)
{
GetXml(_yahooRequest, ar, ref _yahooDoc);
}
void EndEbayAsyncOperation(IAsyncResult ar)
{
GetXml(_ebayRequest, ar, ref _ebayDoc);
}
void GetXml(WebRequest myReq, IAsyncResult myResult, ref XmlDocument myDoc)
{
using (WebResponse response = myReq.EndGetResponse(myResult))
{
using (StreamReader receiveStream = new StreamReader(response.GetResponseStream()))
{
myDoc = new XmlDocument();
myDoc.Load(receiveStream);
}
}
}
void TimeoutAmazonAsyncOperation(IAsyncResult ar) { Response.Write("Amazon Data temporarily unavailable"); }
void TimeoutYahooAsyncOperation(IAsyncResult ar) { Response.Write("Yahoo Data temporarily unavailable"); }
void TimeoutEbayAsyncOperation(IAsyncResult ar) { Response.Write("eBay Data temporarily unavailable"); }
}
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.