/// Author: Francis A. Shanahan
/// http://www.FrancisShanahan.com
using System;
using System.Data;
using System.Configuration;
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 com.amazon.webservices;
using System.Xml;
using System.IO;
///
/// Summary description for AmazonUtility
///
public static class AmazonUtility
{
static AmazonUtility()
{
//
// TODO: Add constructor logic here
//
}
public static ItemSearchResponse SearchAmazon(string strKeywords, string strSearchIndex)
{
// Create a new instance of the proxy class
AWSECommerceService myProxy = new AWSECommerceService();
// Create a new instance of the ItemSearch class
ItemSearch mySearch = new ItemSearch();
// ItemSearchRequest stores the actual request parameters
ItemSearchRequest mySearchRequest = new ItemSearchRequest();
// Set some parameters, Keyword and Search Index
mySearchRequest.Keywords = strKeywords;
mySearchRequest.SearchIndex = strSearchIndex;
// Just need Small results, not the full enchilada
mySearchRequest.ResponseGroup = new string[] { "Medium", "Request" };
// Set the subscription and associate tags here
mySearch.AWSAccessKeyId = ConfigurationManager.AppSettings["AWSAccessKeyId"];
mySearch.AssociateTag = ConfigurationManager.AppSettings["AssociateTag"];
// Setup request
mySearch.Request = new ItemSearchRequest[] { mySearchRequest };
// Execute the request and get the response
ItemSearchResponse myResponse = myProxy.ItemSearch(mySearch);
return myResponse;
}
public static string GetResultsFromResponse(ItemSearchResponse myResponse, bool doRss)
{
// Parse the response and return the results
string strResults = "";
foreach (Items myItems in myResponse.Items)
{
if (doRss)
{ // Get the results in RSS
strResults += FormatAsRSS(myItems);
}
else
{
if (myItems.Item != null)
{
foreach (Item myItem in myItems.Item)
{
// Get the results in HTML
strResults += FormatAsHTML(myItem);
}
}
}
}
return strResults;
}
///
/// Returns RSS XML for a specific Amazon Item
///
/// The Items instance
/// a string of XML
public static string FormatAsRSS(Items myItems)
{
XmlDocument myDoc = new XmlDocument();
// Create the root RSS node
XmlNode myRSS = myDoc.CreateNode(XmlNodeType.Element, "rss", "");
// Add the version attribute
XmlAttribute rssVersion = myDoc.CreateAttribute("version");
rssVersion.Value = "2.0";
myRSS.Attributes.Append(rssVersion);
// Create a new element node named "channel"
XmlNode channel = myDoc.CreateNode(XmlNodeType.Element, "channel", "");
// Create a new element node named "title"
XmlNode title = myDoc.CreateNode(XmlNodeType.Element, "title", "");
title.InnerText = "First Amazon Remix";
// Create a new element node named "link"
XmlNode link = myDoc.CreateNode(XmlNodeType.Element, "link", "");
link.InnerText = "http://www.FrancisShanahan.com/";
// Create a new element node named "description"
XmlNode description = myDoc.CreateNode(XmlNodeType.Element, "description", "");
description.InnerText = "Amazon results through RSS 2.0";
// Append the channel specific elements
channel.AppendChild(title);
channel.AppendChild(link);
channel.AppendChild(description);
// Iterate through the products
foreach (Item myItem in myItems.Item)
{
// Create a new item node for each one
XmlNode myNode = CreateRSSItem(myDoc, myItem);
// Append the resulting item to the channel
channel.AppendChild(myNode);
}
// Finally append the channel and the RSS node
myRSS.AppendChild(channel);
myDoc.AppendChild(myRSS);
return GetStringFromXml(myDoc);
}
private static XmlNode CreateRSSItem(XmlDocument myDoc, Item myItem)
{
// Create a new element node named "item"
XmlNode rssItem = myDoc.CreateNode(XmlNodeType.Element, "item", "");
// Create a new element node named "title"
XmlNode title = myDoc.CreateNode(XmlNodeType.Element, "title", "");
title.InnerText = myItem.ItemAttributes.Title;
// Create a new element node named "link"
XmlNode link = myDoc.CreateNode(XmlNodeType.Element, "link", "");
link.InnerText = myItem.DetailPageURL;
// Create a new element node named "description"
XmlNode description = myDoc.CreateNode(XmlNodeType.Element, "description", "");
// Build the description from the underlying item details
string strDescription = "";
// Display an image if one is present
if (myItem.SmallImage != null)
{
strDescription += "
";
}
// The lowest price (essential)
strDescription += "Lowest Price:" + myItem.OfferSummary.LowestNewPrice.FormattedPrice + "
";
// The editor's review if present
if (myItem.EditorialReviews[0] != null)
{
strDescription += myItem.EditorialReviews[0].Content;
}
description.InnerText = strDescription;
// Create a new element node named "pubDate"
XmlNode pubDate = myDoc.CreateNode(XmlNodeType.Element, "pubDate", "");
pubDate.InnerText = DateTime.Today.ToShortDateString().ToString();
// Append the new elements to this rss Item.
rssItem.AppendChild(title);
rssItem.AppendChild(link);
rssItem.AppendChild(description);
rssItem.AppendChild(pubDate);
return rssItem;
}
///
/// Returns HTML for a specific Amazon Item
///
/// The Item instance
/// a string of HTML
public static string FormatAsHTML(Item myItem)
{
// Will contain the results
string strHTML = "";
// Check for null
if (myItem != null)
{
// Begin the item HTML
strHTML += "
";
// Add the title
strHTML += "
" + myItem.ItemAttributes.Title + "
";
// Add the item image if present
if (myItem.SmallImage != null)
{
strHTML += "

";
}
// Add the ASIN
strHTML += "
ASIN: " + myItem.ASIN + "
";
// Add the price if present
if (myItem.OfferSummary != null)
{
if (myItem.OfferSummary.LowestNewPrice != null)
{
strHTML += "
Lowest Price: " +
myItem.OfferSummary.LowestNewPrice.FormattedPrice
+ "
";
}
}
// Add a link to the details
strHTML += "
View Details";
// Close the output
strHTML += "
";
}
return strHTML;
}
///
/// Simple utility function to convert an XML document to a string
///
///
///
public static string GetStringFromXml(XmlDocument myDoc)
{
StringWriter myStringWriter = new StringWriter();
XmlTextWriter myXmlTextWriter = new XmlTextWriter(myStringWriter);
// Write the Xml document out to a string
myDoc.WriteTo(myXmlTextWriter);
string strResults = myStringWriter.ToString();
return strResults;
}
}