Example #1
0
        public static DataModels.SearchResult SearchItems(string Keyword, double Rate, string SearchIndex)
        {
            #region init Model
            Models.DataModels.SearchResult Searchresults = new DataModels.SearchResult();
            List <Models.DataModels.SearchResult.SearchResultsList> SearchresultsList = new List <DataModels.SearchResult.SearchResultsList>();
            #endregion

            ItemSearchResponse resp = AmazonRequestByPage(Keyword, 1, SearchIndex);
            #region Check Response for errors
            //Check for null response
            if (resp == null)
            {
                Searchresults.Error = "Server Error - no response received!";
            }
            if (Convert.ToInt32(resp.Items[0].TotalResults) == 0)
            {
                Searchresults.Error = "No matches found!";
            }
            #endregion

            #region Set the pages to Loop Through according to amazon product advertising api
            ///If SearchIndex==All then Max Pages to query is 5
            ///else Max pages to loop through is 10
            int PagesToLoopTrhough         = Convert.ToInt32(resp.Items[0].TotalPages.ToString());
            int ModifiedPagesToLoopThrough = 0;
            if (SearchIndex == "All")
            {
                if (PagesToLoopTrhough > 5)
                {
                    ModifiedPagesToLoopThrough = 5;
                }
                else
                {
                    ModifiedPagesToLoopThrough = PagesToLoopTrhough;
                }
            }
            else
            {
                if (PagesToLoopTrhough > 10)
                {
                    ModifiedPagesToLoopThrough = 10;
                }
                else
                {
                    ModifiedPagesToLoopThrough = PagesToLoopTrhough;
                }
            }
            #endregion
            #region Loop Through The Search Results And Add to Model
            for (int i = 1; i < ModifiedPagesToLoopThrough + 1; i++)
            {
                resp = AmazonRequestByPage(Keyword, i, SearchIndex);
                foreach (Item item in resp.Items[0].Item)
                {
                    Models.DataModels.SearchResult.SearchResultsList SingleSearchResult = new Models.DataModels.SearchResult.SearchResultsList();
                    if (item.ItemAttributes.Author != null)
                    {
                        SingleSearchResult.Author = item.ItemAttributes.Author[0].ToString();
                    }
                    SingleSearchResult.Title        = item.ItemAttributes.Title ?? "";
                    SingleSearchResult.ProductGroup = item.ItemAttributes.ProductGroup;
                    if (item.ItemAttributes.ListPrice != null)
                    {
                        string Trimmed = item.ItemAttributes.ListPrice.FormattedPrice.Trim('£');
                        Trimmed = Trimmed.Replace(",", string.Empty);
                        SingleSearchResult.Price = Convert.ToString(Math.Round(Convert.ToDouble(Trimmed) * Rate, 2));
                    }
                    else
                    {
                        SingleSearchResult.Price = null;
                    }

                    SearchresultsList.Add(SingleSearchResult);
                }
            }

            #endregion

            Searchresults._SearchResultsList = SearchresultsList;
            return(Searchresults);
        }
Example #2
0
        public static DataModels.SearchResult SearchItems(string Keyword, double Rate, string SearchIndex)
        {
            #region init Model
            Models.DataModels.SearchResult Searchresults= new DataModels.SearchResult();
            List<Models.DataModels.SearchResult.SearchResultsList> SearchresultsList = new List<DataModels.SearchResult.SearchResultsList>();
            #endregion

            ItemSearchResponse resp = AmazonRequestByPage(Keyword, 1, SearchIndex);
            #region Check Response for errors
            //Check for null response
            if (resp == null)
                Searchresults.Error="Server Error - no response received!";
            if (Convert.ToInt32(resp.Items[0].TotalResults) == 0)
                Searchresults.Error="No matches found!";
            #endregion

            #region Set the pages to Loop Through according to amazon product advertising api
            ///If SearchIndex==All then Max Pages to query is 5
            ///else Max pages to loop through is 10
            int PagesToLoopTrhough=Convert.ToInt32(resp.Items[0].TotalPages.ToString());
            int ModifiedPagesToLoopThrough = 0;
            if (SearchIndex=="All")
            {
                if (PagesToLoopTrhough>5){ModifiedPagesToLoopThrough = 5;}
                else{ModifiedPagesToLoopThrough = PagesToLoopTrhough;}
            }
            else
            {
                if (PagesToLoopTrhough>10){ModifiedPagesToLoopThrough = 10;}
                else{ModifiedPagesToLoopThrough = PagesToLoopTrhough;}
            }
            #endregion
            #region Loop Through The Search Results And Add to Model
            for (int i = 1; i < ModifiedPagesToLoopThrough+1; i++)
            {
                    resp = AmazonRequestByPage(Keyword, i, SearchIndex);
                    foreach (Item item in resp.Items[0].Item)
                    {
                        Models.DataModels.SearchResult.SearchResultsList SingleSearchResult = new Models.DataModels.SearchResult.SearchResultsList();
                        if (item.ItemAttributes.Author != null)
                            SingleSearchResult.Author = item.ItemAttributes.Author[0].ToString();
                        SingleSearchResult.Title = item.ItemAttributes.Title ?? "";
                        SingleSearchResult.ProductGroup = item.ItemAttributes.ProductGroup;
                        if (item.ItemAttributes.ListPrice != null)
                        {
                            string Trimmed = item.ItemAttributes.ListPrice.FormattedPrice.Trim('£');
                            Trimmed = Trimmed.Replace(",", string.Empty);
                            SingleSearchResult.Price = Convert.ToString(Math.Round(Convert.ToDouble(Trimmed) * Rate, 2));
                        }
                        else SingleSearchResult.Price = null;

                        SearchresultsList.Add(SingleSearchResult);
                    }
              }

            #endregion

            Searchresults._SearchResultsList = SearchresultsList;
            return Searchresults;
        }