diff options
author | Sergey Chebotar <s.chebotar@gmail.com> | 2021-12-03 19:30:35 +0300 |
---|---|---|
committer | Sergey Chebotar <s.chebotar@gmail.com> | 2021-12-03 19:30:35 +0300 |
commit | 915929fa9d0738a4e4db4134ea522b343ab2c1d2 (patch) | |
tree | e58fc704c59283b2285e775781a734bc96992a03 /Source/Assistant/SkuAssist.cs | |
parent | c748be35c4bc35dc431066fb390945ee0c986ea3 (diff) |
Add Json parsing and refactoring
Diffstat (limited to 'Source/Assistant/SkuAssist.cs')
-rw-r--r-- | Source/Assistant/SkuAssist.cs | 46 |
1 files changed, 18 insertions, 28 deletions
diff --git a/Source/Assistant/SkuAssist.cs b/Source/Assistant/SkuAssist.cs index 121bc88..b6b5f7e 100644 --- a/Source/Assistant/SkuAssist.cs +++ b/Source/Assistant/SkuAssist.cs @@ -1,9 +1,9 @@ using AngleSharp.Dom; -using AngleSharp.Html.Dom; +using Newtonsoft.Json; using System; using System.Linq; -using System.Threading.Tasks; using System.Text.RegularExpressions; +using System.Threading.Tasks; namespace Rehau.Sku.Assist { @@ -25,38 +25,28 @@ namespace Rehau.Sku.Assist Task<string> contentTask = Task.Run(() => HttpClientUtil.GetContentByUriAsync(uri)); Task<IDocument> documentTask = await contentTask.ContinueWith(content => HttpClientUtil.ContentToDocAsync(content)); - IProduct product = await documentTask.ContinueWith(doc => SkuAssist.GetFirstProduct(doc.Result)); - return product; + return GetProduct(documentTask.Result); } - public static IProduct GetFirstProduct(IDocument doc) + public static IProduct GetProduct(IDocument d) { - return doc - .All - .Where(e => e.ClassName == "product-item__desc-top") - .Where(e => Regex.IsMatch(e.Children[0].TextContent, @"\d{11}", RegexOptions.None)) - .Select(e => - new Product(e.Children[0].TextContent, - e.Children[1].TextContent.Trim(new[] { '\n', ' ' }))) - .FirstOrDefault(); - } + string script = d.Scripts + .Where(s => s.InnerHtml.Contains("dataLayer")) + .First() + .InnerHtml; - public static Uri GetFirstResultLink(IDocument doc) - { - var link = new Uri(doc - .Links - .Where(e => e.ClassName == "product-item__title-link js-name") - .Select(l => ((IHtmlAnchorElement)l).Href) - .FirstOrDefault()); - return link; - } + string json = script + .Substring(script.IndexOf("push(") + 5) + .TrimEnd(new[] { ')', ';', '\n', ' ' }); - public static string GetFistResultImageLink(IDocument doc) - { - var imageSource = doc.Images - .Where(x => x.ClassName == "product-item__image") + StoreResponce storeResponse = JsonConvert.DeserializeObject<StoreResponce>(json); + IProduct product = storeResponse + .Ecommerce + .Impressions + .Where(i => Regex.IsMatch(i.Id, @"\d{11}", RegexOptions.None)) .FirstOrDefault(); - return imageSource != null ? imageSource.Source : "Нет ссылки"; + + return product; } } }
\ No newline at end of file |