topical media & game development

talk show tell print

mashup-amazon-07-07-02-AmazonRSS-App-Code-AmazonUtility.cs / cs



  
Author: Francis A. Shanahan

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; }
<summary> Returns RSS XML for a specific Amazon Item </summary> <param name="myItems">The Items instance</param> <returns>a string of XML</returns> 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 += "<img src=" + myItem.SmallImage.URL + " align=right/>";
      }
      // The lowest price (essential)
      strDescription += "Lowest Price:" + myItem.OfferSummary.LowestNewPrice.FormattedPrice + "<br/>";
  
      // 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;
  }
  
  
<summary> Returns HTML for a specific Amazon Item </summary> <param name="myItem">The Item instance</param> <returns>a string of HTML</returns> public static string FormatAsHTML(Item myItem) { // Will contain the results string strHTML = "";

          // Check for null
          if (myItem != null)
          {
              // Begin the item HTML
              strHTML += "<div class=\"item\">";
              // Add the title
              strHTML += "<h2>" + myItem.ItemAttributes.Title + "</h2>";
  
              // Add the item image if present
              if (myItem.SmallImage != null)
              {
                  strHTML += "<img src=" + myItem.SmallImage.URL;
                  strHTML += " width=" + myItem.SmallImage.Width.Value;
                  strHTML += " height=" + myItem.SmallImage.Height.Value;
                  strHTML += " align=\"left\" >";
              }
  
              // Add the ASIN
              strHTML += "<b>ASIN:</b> " + myItem.ASIN + "<br/>";
  
              // Add the price if present
              if (myItem.OfferSummary != null)
              {
                  if (myItem.OfferSummary.LowestNewPrice != null)
                  {
                      strHTML += "<b>Lowest Price:</b> " +
                         myItem.OfferSummary.LowestNewPrice.FormattedPrice
                         + "<br/>";
                  }
              }
              // Add a link to the details
              strHTML += "<a href=\"" + myItem.DetailPageURL + "\">View Details</a><br/>";
  
              // Close the output
              strHTML += "</div>";
          }
          return strHTML;
      }
  
<summary> Simple utility function to convert an XML document to a string </summary> <param name="myDoc"></param> <returns></returns> 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;
      }
  }
  
  


(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.