Ejemplo n.º 1
0
        public List <AmazonItem> searchFormAmazone(String keyword, out string searchMoreUrl)
        {
            searchMoreUrl = String.Empty;
            List <AmazonItem> amazoneItems = new List <AmazonItem>();
            // Arvutan välja mitu lehekülge on vaja.
            int searchRequestPageCounts = 1;

            if (Settings.SEARCH_RESULTS_PER_PAGE > AMAZON_REQUST_ITEM_LIMIT)
            {
                double neededItems = Convert.ToDouble(Settings.SEARCH_RESULTS_PER_PAGE * Settings.PRELOAD_PAGE_COUNT);
                searchRequestPageCounts = Convert.ToInt32(Math.Ceiling((double)(neededItems / AMAZON_REQUST_ITEM_LIMIT)));
                if (searchRequestPageCounts > AMAZON_REQUST_PAGE_LIMIT)
                {
                    searchRequestPageCounts = AMAZON_REQUST_PAGE_LIMIT;
                }
            }

            // Arvutan mitu päringut ma Amazon'i serveri teen
            int differentRequests = Convert.ToInt32(Math.Ceiling((double)searchRequestPageCounts / (double)AMAZON_MAX_BATCH_REQUEST));

            for (int i = 0; i < differentRequests; i++)
            {
                int startPage = i * AMAZON_MAX_BATCH_REQUEST + 1;
                int endPage   = i * AMAZON_MAX_BATCH_REQUEST + AMAZON_MAX_BATCH_REQUEST;
                if (endPage > searchRequestPageCounts)
                {
                    endPage = endPage - (endPage % searchRequestPageCounts);
                }
                Items[] searchedItems = searchAmazonItems(keyword, startPage, endPage);
                if (searchedItems != null)
                {
                    for (int j = 0; j < searchedItems.Length; j++)
                    {
                        foreach (Item result in searchedItems[j].Item)
                        {
                            ItemAttributes itemAttributes = result.ItemAttributes;
                            Decimal        amount         = Decimal.MinValue;
                            String         currencyCode   = "";
                            if (itemAttributes.ListPrice != null)
                            {
                                Decimal.TryParse(itemAttributes.ListPrice.Amount, out amount);
                                currencyCode = itemAttributes.ListPrice.CurrencyCode;
                                if (amount != Decimal.MinValue)
                                {
                                    amount = amount / 100;
                                }
                            }

                            AmazonItem amazoneItem = new AmazonItem(itemAttributes.Title, amount, currencyCode);
                            amazoneItem.Index      = amazoneItems.Count + 1;
                            amazoneItem.PageNumber = Convert.ToInt32(Math.Ceiling((double)amazoneItem.Index / (double)Settings.SEARCH_RESULTS_PER_PAGE));
                            amazoneItems.Add(amazoneItem);
                        }
                        searchMoreUrl = searchedItems[j].MoreSearchResultsUrl;
                    }
                }
            }

            return(amazoneItems);
        }
Ejemplo n.º 2
0
        public List<AmazonItem> searchFormAmazone(String keyword, out string searchMoreUrl)
        {
            searchMoreUrl = String.Empty;
            List<AmazonItem> amazoneItems = new List<AmazonItem>();
            // Arvutan välja mitu lehekülge on vaja.
            int searchRequestPageCounts = 1;
            if (Settings.SEARCH_RESULTS_PER_PAGE > AMAZON_REQUST_ITEM_LIMIT)
            {
                double neededItems = Convert.ToDouble(Settings.SEARCH_RESULTS_PER_PAGE * Settings.PRELOAD_PAGE_COUNT);
                searchRequestPageCounts = Convert.ToInt32(Math.Ceiling((double)(neededItems / AMAZON_REQUST_ITEM_LIMIT)));
                if (searchRequestPageCounts > AMAZON_REQUST_PAGE_LIMIT)
                    searchRequestPageCounts = AMAZON_REQUST_PAGE_LIMIT;
            }

            // Arvutan mitu päringut ma Amazon'i serveri teen
            int differentRequests = Convert.ToInt32(Math.Ceiling((double)searchRequestPageCounts / (double)AMAZON_MAX_BATCH_REQUEST));
            for (int i = 0; i < differentRequests; i++)
            {
                int startPage = i * AMAZON_MAX_BATCH_REQUEST + 1;
                int endPage = i * AMAZON_MAX_BATCH_REQUEST + AMAZON_MAX_BATCH_REQUEST;
                if (endPage > searchRequestPageCounts)
                {
                    endPage = endPage - (endPage%searchRequestPageCounts);
                }
                Items[] searchedItems = searchAmazonItems(keyword, startPage, endPage);
                if (searchedItems != null)
                {
                    for (int j = 0; j < searchedItems.Length; j++)
                    {
                        foreach (Item result in searchedItems[j].Item)
                        {
                            ItemAttributes itemAttributes = result.ItemAttributes;
                            Decimal amount = Decimal.MinValue;
                            String currencyCode = "";
                            if (itemAttributes.ListPrice != null)
                            {
                                Decimal.TryParse(itemAttributes.ListPrice.Amount, out amount);
                                currencyCode = itemAttributes.ListPrice.CurrencyCode;
                                if (amount != Decimal.MinValue)
                                    amount = amount / 100;
                            }

                            AmazonItem amazoneItem = new AmazonItem(itemAttributes.Title, amount, currencyCode);
                            amazoneItem.Index = amazoneItems.Count + 1;
                            amazoneItem.PageNumber = Convert.ToInt32(Math.Ceiling((double)amazoneItem.Index / (double)Settings.SEARCH_RESULTS_PER_PAGE));
                            amazoneItems.Add(amazoneItem);
                            
                        }
                        searchMoreUrl = searchedItems[j].MoreSearchResultsUrl;
                    }
                }
            }

            return amazoneItems;
        }