I'm a mean one, I'm the Grinch ... by using Amazon Web Services

Published 16 December 06 04:11 PM | Coding4Fun 

With the aid of Amazon.com web services, Clint Rutkas created a program that will guess what your gift is. With the size of your wrapped present, the program will poll Amazon and based on what is on your wishlist, it will guess what it may be!

 
Difficulty: Intermediate
Time Required: 1-3 hours
Cost: Free
Software: Visual Studio Express Editions
Hardware: None
Download: C# or Visual Basic 

    Signing up for the Amazon Web Services

    The first step in using this application is signing up for the Amazon Web Services.  From there you'll get a AWS key that you will use to access the web service for authorization purposes.  This key should be 20 characters.

    Adding the Amazon Web Services to .Net

    The wonderful thing about .Net is how easy it is to add in a Web Service.  First you right click on References in your solution.

     

    The next step is adding in the WSDL.  Here is the URL for it.

    http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl

    Oven is preheating, lets do some work.

    Ok, so now I have the web service added and Visual Studio is all ready to rock the casbah, we need know how to request information.  For that, we need documentation.  RTFM?  Yes, read the manual which is found here (PDF).  After a few quick searches, I found some samples through direct requests.

    Here is an example that will return my wishlists.  You need to add in insert your key.

    http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=YOUR_KEY&Operation=ListSearch&ListType=WishList&Email=Clint@pronerd.com

    Ok, so this is wonderful if I was doing JavaScript but I'm not.  So how does one make this request in c#?  You need the ListSearchRequest , ListSearch, ListSearchResponse, and AWSECommerceService objects.

    C# Code

       1:  // building search object for wishlist
       2:  ListSearchRequest request = new ListSearchRequest();
       3:  request.ListType = ListSearchRequestListType.WishList;
       4:  request.Email = txtEmail.Text;
       5:   
       6:  ListSearch listSearcher = new ListSearch();
       7:  listSearcher.AWSAccessKeyId = AmazonKey;
       8:  listSearcher.Request = new ListSearchRequest[] { request };
       9:   
      10:  ListSearchResponse response = amazonPrime.ListSearch(listSearcher);
      11:  // populate wishlists
      12:  if (response.Lists.Length > 0)
      13:  {
      14:      List[] returnLists = response.Lists[0].List;
      15:      lstWishlists.DataSource = returnLists;
      16:      lstWishlists.DisplayMember = "ListName"; // want to show the list name
      17:  }

    Visual Basic

       1:  Dim request As ListSearchRequest = New ListSearchRequest
       2:  request.ListType = ListSearchRequestListType.WishList
       3:  request.Email = txtEmail.Text
       4:  Dim listSearcher As ListSearch = New ListSearch
       5:  listSearcher.AWSAccessKeyId = AmazonKey
       6:  listSearcher.Request = New ListSearchRequest() {request}
       7:  Dim response As ListSearchResponse = amazonPrime.ListSearch(listSearcher)
       8:  ' populate wishlists
       9:  If (response.Lists.Length > 0) Then
      10:      Dim returnLists() As List = response.Lists(0).List
      11:      lstWishlists.DataSource = returnLists
      12:      lstWishlists.DisplayMember = "ListName"
      13:      ' want to show the list name
      14:  End If

    Will all my wishlist items Come On Down!

    From here, I have access to the List ID now that I need.  I still need the items on the list, which are returned 10 per request for max of 30 pages per the Amazon API.  For this request, it took a bit of snooping to figure out what RequestGroups I needed to return the items.  I settled on the ListItems and ItemAttributes to return the information off the wishlist.

    C# Code

       1:  private ListItem[] GetWishlistItems(string ListId, int Page, out int TotalPages, out int TotalItems)
       2:  {
       3:      TotalPages = 0;
       4:      TotalItems = 0;
       5:   
       6:      ListLookupRequest request = new ListLookupRequest();
       7:      request.ListId = ListId;
       8:      request.ListType = ListLookupRequestListType.WishList;
       9:      request.ListTypeSpecified = true;
      10:      request.ProductPage = Page.ToString();
      11:      request.ResponseGroup = new string[] { "ListItems", "ItemAttributes" }; // ItemAttributes gets me package size
      12:   
      13:      ListLookup listLookup = new ListLookup();
      14:      listLookup.AWSAccessKeyId = AmazonKey;
      15:      listLookup.Request = new ListLookupRequest[] { request };
      16:   
      17:      ListLookupResponse response = amazonPrime.ListLookup(listLookup);
      18:   
      19:      // populate wishlists
      20:      if (response.Lists != null && response.Lists.Length > 0 && response.Lists[0].List.Length > 0)
      21:      {
      22:          List returnedList = response.Lists[0].List[0];
      23:   
      24:          TotalPages = Convert.ToInt32(returnedList.TotalPages);
      25:          TotalItems = Convert.ToInt32(returnedList.TotalItems);
      26:   
      27:          return response.Lists[0].List[0].ListItem;
      28:      }
      29:      return null;
      30:  }

    Visual Basic

     

       1:      Private Function GetWishlistItems(ByVal ListId As String, ByVal Page As Integer, ByRef TotalPages As Integer, ByRef TotalItems As Integer) As ListItem()
       2:          TotalPages = 0
       3:          TotalItems = 0
       4:          Dim request As ListLookupRequest = New ListLookupRequest
       5:          request.ListId = ListId
       6:          request.ListType = ListLookupRequestListType.WishList
       7:          request.ListTypeSpecified = true
       8:          request.ProductPage = Page.ToString
       9:          request.ResponseGroup = New String() {"ListItems", "ItemAttributes"}
      10:          ' ItemAttributes gets me package size
      11:          Dim listLookup As ListLookup = New ListLookup
      12:          listLookup.AWSAccessKeyId = AmazonKey
      13:          listLookup.Request = New ListLookupRequest() {request}
      14:          Dim response As ListLookupResponse = amazonPrime.ListLookup(listLookup)
      15:          ' populate wishlists
      16:          If ((Not (response.Lists) Is Nothing)  _
      17:                      AndAlso ((response.Lists.Length > 0)  _
      18:                      AndAlso (response.Lists(0).List.Length > 0))) Then
      19:              Dim returnedList As List = response.Lists(0).List(0)
      20:              TotalPages = Convert.ToInt32(returnedList.TotalPages)
      21:              TotalItems = Convert.ToInt32(returnedList.TotalItems)
      22:              Return response.Lists(0).List(0).ListItem
      23:          End If
      24:          Return Nothing
      25:      End Function

    It rattles, it is Legos or a toaster.

    So it is neither, but as of now, we know the item's package size and that is about all.  We need to datamine this information down.  Since I stored the items in an array list, I can easily add and remove items without reallocating memory.  This is good since there is no way to predetermine what the gift's size you're looking to find is.  So with some inputs, we can request the height, width, and length of the item along with a tolerance for wrapping paper / extra padding.  After testing this, I never realized how many DVDs I have on my list.

    C# Code

       1:  ArrayList templist = new ArrayList();
       2:   
       3:  for (int i = 0; i < giftList.Count; i++)
       4:  {
       5:      decimal tolerance = numTol.Value;
       6:      ListItemsSimple temp = (ListItemsSimple)giftList[i];
       7:   
       8:      if(
       9:          Math.Abs(temp.Height - numHeight.Value) < tolerance && 
      10:          (
      11:              // incase Amazon and you disagree with 
      12:              // which is length and width
      13:              (Math.Abs(temp.Length - numLength.Value) < tolerance &&
      14:              Math.Abs(temp.Width - numWidth.Value) < tolerance)
      15:              ||
      16:              (Math.Abs(temp.Width - numLength.Value) < tolerance &&
      17:              Math.Abs(temp.Length - numWidth.Value) < tolerance))
      18:          )
      19:      {
      20:          templist.Add(giftList[i]);
      21:      }
      22:  }
      23:   
      24:  lstGifts.DataSource = templist;
      25:  lstGifts.DisplayMember = "Title"; // want to show the list name

    Visual Basic

       1:  Dim templist As ArrayList = New ArrayList
       2:  Dim i As Integer = 0
       3:  Do While (i < giftList.Count)
       4:      Dim tolerance As Decimal = numTol.Value
       5:      Dim temp As ListItemsSimple = CType(giftList(i),ListItemsSimple)
       6:      If ((Math.Abs((temp.Height - numHeight.Value)) < tolerance)  _
       7:                  AndAlso (((Math.Abs((temp.Length - numLength.Value)) < tolerance)  _
       8:                  AndAlso (Math.Abs((temp.Width - numWidth.Value)) < tolerance))  _
       9:                  OrElse ((Math.Abs((temp.Width - numLength.Value)) < tolerance)  _
      10:                  AndAlso (Math.Abs((temp.Length - numWidth.Value)) < tolerance)))) Then
      11:          templist.Add(giftList(i))
      12:      End If
      13:      i = (i + 1)
      14:  Loop
      15:  lstGifts.DataSource = templist
      16:  lstGifts.DisplayMember = "Title"

    Wrapping it all together with only 3 peices of tape.

    So the rest of the application is just fluff.  Add some paint using counter-clockwise swirling motions and you get this.

    Problems? 

    For a while I did have a snip of code that removed items that were bought and got greater than and less than signs and found out I'm getting 2 copies of Rome season one (good show) on accident.  That caused me to realize I don't know when items were bought and I'm slightly more stupid than I thought.  I know how many of an item were asked for and how many were purchased, I don't know when they were bought.  Due to this fact, I cannot restrict items.  Amazon has the wishlist anti-spoiler feature so I figured I'll just return everything.

    Author Bio: Clint is an application developer for SpringCM, an on-demand, web-based document and content management system. As a developer, Clint is part of an innovative team committed to supporting affordable, scalable and reliable enterprise content management. His two primary development languages are C# and JavaScript. He worked on his Disco Dance Floor and helps out Coding4Fun with thier daily blog entries. In his off time, he whips up other random weird projects and does twenty something activities with his friends. Clint's blog is betterthaneveryone.com and can be emailed at clint@rutkas.com. Clint Rutkas created a program will guess what your gift is. With the size of your wrapped present, the program will poll Amazon and based on what is on your wishlist, it will guess what it may be!]]> Clint Rutkas http://betterthaneveryone.com Clint Rutkas's Blog http://channel9.msdn.com/ShowPost.aspx?PostID=266742 http://channel9.msdn.com/ShowPost.aspx?PostID=266743 Intermediate Less than $50 Visual Studio Express Editions]]> ]]>

    Filed under: ,

    Comment Notification

    If you would like to receive an email when updates are made to this post, please register here

    Subscribe to this post's comments using RSS

    Comments

    No Comments

    Leave a Comment

    (required) 
    (optional)
    (required) 

      
    Enter Code Here: Required

    Search

    This Blog

    Syndication

    Page view tracker