Exemplo n.º 1
0
        public MarketSearch Search(
            string query = "",
            int start    = 0,
            int count    = 10,
            bool searchInDescriptions = false,
            int appId = 0,
            EMarketSearchSortColumns sortColumn = EMarketSearchSortColumns.Name,
            ESort sort = ESort.Desc,
            IDictionary <string, string> custom = null)
        {
            var urlQuery = new Dictionary <string, string>
            {
                { "query", query },
                { "start", start.ToString() },
                { "count", count.ToString() },
                { "search_descriptions", (searchInDescriptions ? 1 : 0).ToString() },
                { "appid", appId.ToString() },
                { "sort_column", MarketUtils.FirstCharacterToLower(sortColumn.ToString()) },
                { "sort_dir", MarketUtils.FirstCharacterToLower(sort.ToString()) }
            };

            if (custom != null && custom.Count > 0)
            {
                urlQuery = urlQuery.Concat(custom).ToDictionary(x => x.Key, x => x.Value);
            }

            var resp = this.steam.Request(
                Urls.Market + "/search/render/",
                Method.GET,
                Urls.Market,
                urlQuery,
                true,
                proxy: this.Proxy);
            var respDes = JsonConvert.DeserializeObject <JMarketSearch>(resp.Data.Content);

            if (!respDes.Success)
            {
                throw new SteamException("Failed Search");
            }

            var marketSearch = new MarketSearch();

            if (respDes.TotalCount <= 0)
            {
                return(marketSearch);
            }

            marketSearch.TotalCount = respDes.TotalCount;

            var html = respDes.ResultsHtml;
            var doc  = new HtmlDocument();

            doc.LoadHtml(html);

            var itemNodes = doc.DocumentNode.SelectNodes("//a[@class='market_listing_row_link']");

            if (itemNodes == null || itemNodes.Count <= 0)
            {
                return(marketSearch);
            }

            var tempIndex = 0;

            foreach (var item in itemNodes)
            {
                tempIndex++;

                var nameNode = item.SelectSingleNode(".//span[@class='market_listing_item_name']");
                if (nameNode == null)
                {
                    throw new SteamException($"Cannot parse item name. Index [{tempIndex}]");
                }

                var name = nameNode.InnerText;

                var gameNode = item.SelectSingleNode(".//span[@class='market_listing_game_name']");
                if (gameNode == null)
                {
                    throw new SteamException($"Cannot parse game name. Index [{tempIndex}]");
                }

                var game = gameNode.InnerText;

                var imageUrlNode = item.SelectSingleNode(".//img[@class='market_listing_item_img']");

                var imageUrl = imageUrlNode?.Attributes["srcset"]?.Value;

                var urlAttr = item.Attributes["href"];
                if (urlAttr == null)
                {
                    throw new SteamException($"Cannot find item url. Item [{name}], Index [{tempIndex}]");
                }

                var url = urlAttr.Value;

                var quantityNode = item.SelectSingleNode(".//span[@class='market_listing_num_listings_qty']");
                if (quantityNode == null)
                {
                    throw new SteamException($"Cannot find quantity. Item [{name}], Index [{tempIndex}]");
                }

                var quantityParse = int.TryParse(
                    quantityNode.InnerText,
                    NumberStyles.AllowThousands,
                    CultureInfo.InvariantCulture,
                    out var quantity);

                if (!quantityParse)
                {
                    throw new SteamException($"Cannot parse quantity. Item [{name}], Index [{tempIndex}]");
                }

                var minPriceNode = item.SelectSingleNode(".//span[@class='normal_price']");
                if (minPriceNode == null)
                {
                    throw new SteamException($"Cannot find item price. Item [{name}], Index [{tempIndex}]");
                }

                var minPriceClean = minPriceNode.InnerText.Split(' ')[0];

                var minPriceParse = double.TryParse(
                    minPriceClean,
                    NumberStyles.Currency,
                    CultureInfo.GetCultureInfo("en-US"),
                    out var minPrice);
                if (!minPriceParse)
                {
                    throw new SteamException($"Cannot parse min. price. Item [{name}], Index [{tempIndex}]");
                }

                marketSearch.Items.Add(
                    new MarketSearchItem
                {
                    ImageUrl = imageUrl,
                    Name     = name,
                    Quantity = quantity,
                    Game     = game,
                    Url      = url,
                    MinPrice = minPrice,
                    HashName = MarketUtils.HashNameFromUrl(url),
                    AppId    = MarketUtils.AppIdFromUrl(url)
                });
            }

            return(marketSearch);
        }