Advertisement
Guest User

MyAmazonClient.cs

a guest
Apr 18th, 2014
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Net;
  7. using System.Net.Http;
  8. using amazon_rest;
  9. using System.Xml;
  10.  
  11. public class AmazonECSWrapper : IBookSearch
  12. {
  13.     public Book[] getBooks(string keywords)
  14.     {
  15.         String amazonAccessKey = "1111135H211111B11111";
  16.         String amazonSecretKey = "11111UwH1111SUaH12333E9B123fAgU/B";
  17.         String destination = "ecs.amazonaws.de";
  18.         String associateID = "amazon0xx1-10";
  19.  
  20.     // Create the request
  21.         SignedRequestHelper requestHelper = new SignedRequestHelper(amazonAccessKey, amazonSecretKey, destination, associateID);
  22.         String amazonQueryString = "Service=AWSECommerceService"
  23.                                     + "&Version=2011-08-01"
  24.                                     + "&Operation=ItemSearch"
  25.                                     + "&SearchIndex=Books"
  26.                                     + "&ResponseGroup=Large"
  27.                                     + "&Keywords=" + keywords;
  28.  
  29.         String signedAmazonQueryString = requestHelper.Sign(amazonQueryString);
  30.        
  31.         WebClient webClient = new WebClient();
  32.         XmlDocument xmlDoc = new XmlDocument();
  33.  
  34.     // Send the request to the Amazon-Webservice and get the result in XML
  35.         xmlDoc.Load(webClient.OpenRead(signedAmazonQueryString));
  36.         ArrayList bookList = new ArrayList();
  37.  
  38.     // Iterate over items
  39.         foreach(XmlElement itemXmlElem in xmlDoc.SelectNodes("/ItemSearchResponse/Items")) {
  40.             String bildURL = "";
  41.             String author = "";
  42.             String isbn = "";
  43.             String titel = "";
  44.             double preis = 0;
  45.             String erscheinungsDatum = "";
  46.  
  47.             XmlNode bildURLNode = itemXmlElem.GetElementsByTagName("SmallImage/URL")[0];
  48.             if (bildURLNode != null)
  49.                 bildURL = bildURLNode.Value;
  50.  
  51.             XmlNode authorNode = itemXmlElem.GetElementsByTagName("ItemAttributes/Author")[0];
  52.             if (authorNode != null)
  53.                 author = authorNode.Value;
  54.  
  55.             XmlNode isbnNode = itemXmlElem.GetElementsByTagName("ItemAttributes/ISBN")[0];
  56.             if (isbnNode != null)
  57.                 isbn = isbnNode.Value;
  58.  
  59.             XmlNode titelNode = itemXmlElem.GetElementsByTagName("ItemAttributes/Title")[0];
  60.             if (titelNode != null)
  61.                 titel = titelNode.Value;
  62.  
  63.             XmlNode preisNode = itemXmlElem.GetElementsByTagName("ItemAttributes/Price")[0];
  64.             if (preisNode != null)
  65.                 preis = Double.Parse(preisNode.Value);
  66.  
  67.             XmlNode erscheinungsDatumNode = itemXmlElem.GetElementsByTagName("ItemAttributes/PublicationDate")[0];
  68.             if (erscheinungsDatumNode != null)
  69.                 erscheinungsDatum = erscheinungsDatumNode.Value;
  70.  
  71.             bookList.Add(new Book(isbn, titel, preis, author, bildURL, erscheinungsDatum));
  72.         }
  73.  
  74.         return (Book[])bookList.ToArray(typeof(Book));
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement