Пример #1
0
        private MyListingsSalesItem ProcessSellListings(HtmlNode item, string currency)
        {
            var node        = item.SelectSingleNode(".//span[@class='market_listing_price']");
            var date        = Regex.Match(item.InnerText, @"Listed: (.+)?\s").Groups[1].Value.Trim();
            var priceString = node.InnerText.Replace("\r", string.Empty).Replace("\n", string.Empty)
                              .Replace("\t", string.Empty).Replace(" ", string.Empty);

            if (priceString.Contains("Sold!"))
            {
                return(null);
            }

            var    game = item.SelectSingleNode("//span[@class='market_listing_game_name']").InnerText;
            double price;

            try
            {
                var priceParse     = priceString.Split('(')[0];
                var currencySymbol = SteamCurrencies.Currencies[currency];
                priceParse = priceParse.Replace(currencySymbol, string.Empty);
                var parseDouble = NumberUtils.TryParseDouble(priceParse, out price);
                if (parseDouble == false)
                {
                    throw new SteamException($"Cannot parse order listing price - {priceString}");
                }
            }
            catch (SteamException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new SteamException($"Cannot parse order listing price - {priceString} - {e.Message}");
            }

            var saleIdMatch = Regex.Match(item.InnerHtml, "(?<=mylisting_)([0-9]*)(?=_name)");

            if (!saleIdMatch.Success)
            {
                throw new SteamException("Cannot find sale listing ID");
            }

            var saleIdParse = long.TryParse(saleIdMatch.Value, out var saleId);

            if (!saleIdParse)
            {
                throw new SteamException($"Cannot parse sale listing ID - {saleIdMatch.Value}");
            }

            var imageUrl = item.SelectSingleNode($"//img[contains(@id, 'mylisting_{saleId}_image')]").Attributes["src"]
                           .Value;

            imageUrl = Regex.Match(imageUrl, "image/(.*)/").Groups[1].Value;

            var urlNode = item.SelectSingleNode(".//a[@class='market_listing_item_name_link']");

            if (urlNode == null)
            {
                throw new SteamException("Cannot find sale listing url");
            }

            var url = urlNode.Attributes["href"].Value;

            var hashName = MarketUtils.HashNameFromUrl(url);
            var appId    = MarketUtils.AppIdFromUrl(url);

            var nameNode = urlNode.InnerText;

            var result = new MyListingsSalesItem
            {
                AppId    = appId,
                HashName = hashName,
                Name     = nameNode,
                Date     = date,
                SaleId   = saleId,
                Price    = price,
                Url      = url,
                ImageUrl = imageUrl,
                Game     = game
            };

            return(result);
        }
Пример #2
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);
        }
Пример #3
0
        private void GetPendingTransactionData(
            HtmlNode item,
            int tempIndex,
            MyListings myListings,
            ETransactionType type,
            string currency)
        {
            if (item == null)
            {
                return;
            }

            var node = item.SelectSingleNode(".//span[@class='market_listing_price']");

            if (node == null)
            {
                throw new SteamException(
                          $"Cannot parse order listing price and quantity node. Item index [{tempIndex}]");
            }

            var date = Regex.Match(item.InnerText, @"Listed: (.+)?\s").Groups[1].Value.Trim();
            var game = item.SelectSingleNode("//span[@class='market_listing_game_name']")?.InnerText;

            if (type == ETransactionType.Order)
            {
                var priceAndQuantityString = node.InnerText.Replace("\r", string.Empty).Replace("\n", string.Empty)
                                             .Replace("\t", string.Empty).Replace(" ", string.Empty);
                var priceAndQuantitySplit = priceAndQuantityString.Split('@');

                double price;
                try
                {
                    var priceParse     = priceAndQuantitySplit[1].Replace(".", string.Empty);
                    var currencySymbol = SteamCurrencies.Currencies[currency];
                    double.TryParse(priceParse.Replace(currencySymbol, string.Empty), out price);
                }
                catch (Exception)
                {
                    throw new SteamException($"Cannot parse order listing price. Item index [{tempIndex}]");
                }

                var quantityParse = int.TryParse(priceAndQuantitySplit[0], out var quantity);

                if (!quantityParse)
                {
                    throw new SteamException($"Cannot parse order listing quantity. Item index [{tempIndex}]");
                }

                var orderIdMatch = Regex.Match(item.InnerHtml, "(?<=mbuyorder_)([0-9]*)(?=_name)");

                if (!orderIdMatch.Success)
                {
                    throw new SteamException($"Cannot find order listing ID. Item index [{tempIndex}]");
                }

                var orderIdParse = long.TryParse(orderIdMatch.Value, out var orderId);

                if (!orderIdParse)
                {
                    throw new SteamException($"Cannot parse order listing ID. Item index [{tempIndex}]");
                }

                var imageUrl = item.SelectSingleNode($"//img[contains(@id, 'mylisting_{orderId}_image')]")
                               .Attributes["src"].Value;

                imageUrl = Regex.Match(imageUrl, "image/(.*)/").Groups[1].Value;

                var urlNode = item.SelectSingleNode(".//a[@class='market_listing_item_name_link']");
                if (urlNode == null)
                {
                    throw new SteamException($"Cannot find order listing url. Item index [{tempIndex}]");
                }

                var url = urlNode.Attributes["href"].Value;

                var hashName = MarketUtils.HashNameFromUrl(url);
                var appId    = MarketUtils.AppIdFromUrl(url);

                var nameNode = urlNode.InnerText;

                myListings.Orders.Add(
                    new MyListingsOrdersItem
                {
                    AppId    = appId,
                    HashName = hashName,
                    Name     = nameNode,
                    Date     = date,
                    OrderId  = orderId,
                    Price    = price,
                    Quantity = quantity,
                    Url      = url,
                    ImageUrl = imageUrl,
                    Game     = game
                });
            }
            else
            {
                var priceString = node.InnerText.Replace("\r", string.Empty).Replace("\n", string.Empty)
                                  .Replace("\t", string.Empty).Replace(" ", string.Empty);
                double price;
                try
                {
                    var priceParse     = priceString.Split('(')[0];
                    var currencySymbol = SteamCurrencies.Currencies[currency];
                    double.TryParse(priceParse.Replace(currencySymbol, string.Empty), out price);
                }
                catch (Exception)
                {
                    throw new SteamException($"Cannot parse order listing price. Item index [{tempIndex}]");
                }

                var saleIdMatch = Regex.Match(item.InnerHtml, "(?<=mylisting_)([0-9]*)(?=_name)");
                if (!saleIdMatch.Success)
                {
                    throw new SteamException($"Cannot find sale listing ID. Item index [{tempIndex}]");
                }

                var saleIdParse = long.TryParse(saleIdMatch.Value, out var saleId);

                if (!saleIdParse)
                {
                    throw new SteamException($"Cannot parse sale listing ID. Item index [{tempIndex}]");
                }

                var imageUrl = item.SelectSingleNode($"//img[contains(@id, 'mylisting_{saleId}_image')]")
                               .Attributes["src"].Value.Replace("38fx38f", string.Empty).Replace(
                    "https://steamcommunity-a.akamaihd.net/economy/image/",
                    string.Empty);

                var urlNode = item.SelectSingleNode(".//a[@class='market_listing_item_name_link']");
                if (urlNode == null)
                {
                    throw new SteamException($"Cannot find sale listing url. Item index [{tempIndex}]");
                }

                var url = urlNode.Attributes["href"].Value;

                var hashName = MarketUtils.HashNameFromUrl(url);
                var appId    = MarketUtils.AppIdFromUrl(url);

                var nameNode = urlNode.InnerText;

                var result = new MyListingsSalesItem
                {
                    AppId    = appId,
                    HashName = hashName,
                    Name     = nameNode,
                    Date     = date,
                    SaleId   = saleId,
                    Price    = price,
                    Url      = url,
                    ImageUrl = imageUrl,
                    Game     = game
                };

                var isConfirmation = item.InnerHtml.Contains("CancelMarketListingConfirmation");

                if (isConfirmation == false)
                {
                    myListings.Sales.Add(result);
                }
                else
                {
                    myListings.ConfirmationSales.Add(result);
                }
            }
        }