示例#1
0
        //解析购物车页
        internal static CartResponse?ParseCertPage(HtmlDocumentResponse response)
        {
            if (response == null)
            {
                return(null);
            }

            IEnumerable <IElement?> gameNodes = response.Content.SelectNodes("//div[@class='cart_item_list']/div");

            List <CartData> cartGames = new();

            foreach (IElement gameNode in gameNodes)
            {
                IElement?eleName  = gameNode.SelectSingleElementNode("//div[@class='cart_item_desc']/a");
                IElement?elePrice = gameNode.SelectSingleElementNode("//div[@class='price']");

                string gameName = eleName.TextContent.Trim() ?? "出错";
                string gameLink = eleName.GetAttribute("href") ?? "出错";

                Match  match    = Regex.Match(gameLink, @"\w+\/\d+");
                string gamePath = match.Success ? match.Value : "出错";

                match = Regex.Match(elePrice.TextContent, @"\d+([.,]\d+)?");
                string strPrice = match.Success ? match.Value : "-1";

                bool success = float.TryParse(strPrice, out float gamePrice);
                if (!success)
                {
                    gamePrice = -1;
                }

                cartGames.Add(new CartData(gamePath, gameName, (int)gamePrice * 100));
            }

            int  totalPrice = 0;
            bool purchaseSelf = false, purchaseGift = false;

            if (cartGames.Count > 0)
            {
                IElement?eleTotalPrice = response.Content.SelectSingleNode("//div[@id='cart_estimated_total']");

                Match match = Regex.Match(eleTotalPrice.TextContent, @"\d+([.,]\d+)?");

                string strPrice = match.Success ? match.Value : "0";

                bool success = float.TryParse(strPrice, out float totalProceFloat);
                if (!success)
                {
                    totalProceFloat = -1;
                }
                totalPrice = (int)totalProceFloat * 100;

                purchaseSelf = response.Content.SelectSingleNode("//a[@id='btn_purchase_self']") != null;
                purchaseGift = response.Content.SelectSingleNode("//a[@id='btn_purchase_gift']") != null;
            }

            return(new CartResponse(cartGames, totalPrice, purchaseSelf, purchaseGift));
        }
示例#2
0
        public async Task <HtmlDocumentResponse?> UrlPostToHtmlDocument <T>(string request, T?data = null, string?referer = null, ERequestOptions requestOptions = ERequestOptions.None, byte maxTries = MaxTries) where T : class
        {
            if (string.IsNullOrEmpty(request) || (maxTries == 0))
            {
                throw new ArgumentNullException(nameof(request) + " || " + nameof(maxTries));
            }

            HtmlDocumentResponse?result = null;

            for (byte i = 0; i < maxTries; i++)
            {
                await using StreamResponse? response = await UrlPostToStream(request, data, referer, requestOptions | ERequestOptions.ReturnClientErrors, 1).ConfigureAwait(false);

                if (response?.StatusCode.IsClientErrorCode() == true)
                {
                    if (requestOptions.HasFlag(ERequestOptions.ReturnClientErrors))
                    {
                        result = new HtmlDocumentResponse(response);
                    }

                    break;
                }

                if (response?.StatusCode.IsServerErrorCode() == true)
                {
                    if (requestOptions.HasFlag(ERequestOptions.ReturnServerErrors))
                    {
                        result = new HtmlDocumentResponse(response);
                    }

                    break;
                }

                if (response?.Content == null)
                {
                    continue;
                }

                try {
                    result = await HtmlDocumentResponse.Create(response).ConfigureAwait(false);
                } catch (Exception e) {
                    ArchiLogger.LogGenericWarningException(e);

                    continue;
                }

                return(result);
            }

            if (maxTries > 1)
            {
                ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, maxTries));
                ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request));
            }

            return(result);
        }
示例#3
0
        //解析商店页
        internal static StoreResponse?ParseStorePage(HtmlDocumentResponse response)
        {
            if (response == null)
            {
                return(null);
            }

            IEnumerable <IElement> gameNodes = response.Content.SelectNodes("//div[@id='game_area_purchase']/div");

            List <SubData> subInfos = new();

            foreach (IElement gameNode in gameNodes)
            {
                IElement?eleName  = gameNode.SelectSingleElementNode("//h1");
                IElement?eleForm  = gameNode.SelectSingleElementNode("//form");
                IElement?elePrice = gameNode.SelectSingleElementNode("//div[@data-price-final]");

                if (elePrice == null)//DLC的按钮,无价格
                {
                    continue;
                }

                string subName    = eleName.TextContent.Trim() ?? "出错";
                string formName   = eleForm.GetAttribute("name") ?? "出错";
                string finalPrice = elePrice.GetAttribute("data-price-final") ?? "出错";

                Match match = Regex.Match(formName, @"\d+$");

                if (uint.TryParse(match.Value, out uint subID) && uint.TryParse(finalPrice, out uint gamePrice))
                {
                    bool bundle = formName.IndexOf("bundle") != -1;
                    subInfos.Add(new SubData(bundle, subID, subName, gamePrice));
                }
                else
                {
                    ASF.ArchiLogger.LogGenericWarning(subName);
                    ASF.ArchiLogger.LogGenericWarning(formName);
                    ASF.ArchiLogger.LogGenericWarning(finalPrice);
                }
            }

            IElement?eleGameName = response.Content.SelectSingleNode("//div[@id='appHubAppName']");
            string   gameName    = eleGameName?.TextContent.Trim() ?? "出错";

            if (subInfos.Count == 0)
            {
                IElement eleError = response.Content.SelectSingleNode("//div[@id='error_box']/span");
                if (eleError != null)
                {
                    gameName = eleError.TextContent.Trim();
                }
            }

            return(new StoreResponse(subInfos, gameName));
        }
示例#4
0
        public async Task <HtmlDocumentResponse> UrlPostToHtmlDocument(string request, IReadOnlyCollection <KeyValuePair <string, string> > data = null, string referer = null, ERequestOptions requestOptions = ERequestOptions.None, byte maxTries = MaxTries)
        {
            if (string.IsNullOrEmpty(request) || (maxTries == 0))
            {
                ArchiLogger.LogNullError(nameof(request) + " || " + nameof(maxTries));

                return(null);
            }

            HtmlDocumentResponse result = null;

            for (int i = 0; i < maxTries; i++)
            {
                await using StreamResponse response = await UrlPostToStream(request, data, referer, requestOptions | ERequestOptions.ReturnClientErrors, 1).ConfigureAwait(false);

                if (response == null)
                {
                    return(null);
                }

                if (response.StatusCode.IsClientErrorCode())
                {
                    if (requestOptions.HasFlag(ERequestOptions.ReturnClientErrors))
                    {
                        result = new HtmlDocumentResponse(response);
                    }

                    break;
                }

                if (response.Content == null)
                {
                    continue;
                }

                try {
                    result = await HtmlDocumentResponse.Create(response).ConfigureAwait(false);
                } catch (Exception e) {
                    ArchiLogger.LogGenericWarningException(e);

                    continue;
                }

                return(result);
            }

            if (maxTries > 1)
            {
                ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, maxTries));
                ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request));
            }

            return(result);
        }
        public async Task <HtmlDocumentResponse> UrlPostToHtmlDocument(string request, IReadOnlyCollection <KeyValuePair <string, string> > data = null, string referer = null, ERequestOptions requestOptions = ERequestOptions.None, byte maxTries = MaxTries)
        {
            if (string.IsNullOrEmpty(request) || (maxTries == 0))
            {
                ArchiLogger.LogNullError(nameof(request) + " || " + nameof(maxTries));

                return(null);
            }

            using StreamResponse response = await UrlPostToStream(request, data, referer, requestOptions, maxTries).ConfigureAwait(false);

            return(response != null ? await HtmlDocumentResponse.Create(response).ConfigureAwait(false) : null);
        }
示例#6
0
        public async Task <HtmlDocumentResponse> UrlPostToHtmlDocument <T>(string request, T data = null, string referer = null, ERequestOptions requestOptions = ERequestOptions.None, byte maxTries = MaxTries) where T : class
        {
            if (string.IsNullOrEmpty(request) || (maxTries == 0))
            {
                return(null);
            }

            HtmlDocumentResponse result = null;

            for (byte i = 0; i < maxTries; i++)
            {
                await using StreamResponse response = await UrlPostToStream(request, data, referer, requestOptions | ERequestOptions.ReturnClientErrors, 1).ConfigureAwait(false);

                if (response?.StatusCode.IsClientErrorCode() == true)
                {
                    if (requestOptions.HasFlag(ERequestOptions.ReturnClientErrors))
                    {
                        result = new HtmlDocumentResponse(response);
                    }

                    break;
                }

                if (response?.Content == null)
                {
                    continue;
                }

                try {
                    result = await HtmlDocumentResponse.Create(response).ConfigureAwait(false);
                } catch (Exception) {
                    continue;
                }

                return(result);
            }

            return(result);
        }