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 | |
parent | c748be35c4bc35dc431066fb390945ee0c986ea3 (diff) |
Add Json parsing and refactoring
Diffstat (limited to 'Source')
-rw-r--r-- | Source/Assistant/IProduct.cs | 3 | ||||
-rw-r--r-- | Source/Assistant/Product.cs | 29 | ||||
-rw-r--r-- | Source/Assistant/SkuAssist.cs | 46 | ||||
-rw-r--r-- | Source/Assistant/StoreResponse.cs | 21 | ||||
-rw-r--r-- | Source/ExcelDNA/Functions.cs | 12 |
5 files changed, 45 insertions, 66 deletions
diff --git a/Source/Assistant/IProduct.cs b/Source/Assistant/IProduct.cs index de0eccf..54b3dd0 100644 --- a/Source/Assistant/IProduct.cs +++ b/Source/Assistant/IProduct.cs @@ -4,8 +4,7 @@ namespace Rehau.Sku.Assist { interface IProduct { - string Sku { get; } + string Id { get; } string Name { get; } - Uri Uri { get; } } } diff --git a/Source/Assistant/Product.cs b/Source/Assistant/Product.cs deleted file mode 100644 index 22905af..0000000 --- a/Source/Assistant/Product.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; - -namespace Rehau.Sku.Assist -{ - public class Product : IProduct - { - public string Sku { get; } - public string Name { get; } - public Uri Uri { get; } - - public Product(string sku, string name) - { - Sku = sku; - Name = name; - } - - public Product(string sku, string name, string uri) - { - Sku = sku; - Name = name; - Uri = new Uri(uri); - } - - public override string ToString() - { - return $"{this.Name} ({this.Sku})"; - } - } -}
\ No newline at end of file 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 diff --git a/Source/Assistant/StoreResponse.cs b/Source/Assistant/StoreResponse.cs new file mode 100644 index 0000000..78fe846 --- /dev/null +++ b/Source/Assistant/StoreResponse.cs @@ -0,0 +1,21 @@ +using System.Collections.Generic; + +namespace Rehau.Sku.Assist +{ + public class StoreResponce + { + public Ecommerce Ecommerce { get; set; } + } + + public class Ecommerce + { + public List<Product> Impressions { get; set; } + } + + public class Product : IProduct + { + public string Id { get; set; } + public string Name { get; set; } + public string Price { get; set; } + } +}
\ No newline at end of file diff --git a/Source/ExcelDNA/Functions.cs b/Source/ExcelDNA/Functions.cs index d45b6ee..9ce4429 100644 --- a/Source/ExcelDNA/Functions.cs +++ b/Source/ExcelDNA/Functions.cs @@ -1,6 +1,6 @@ using ExcelDna.Integration; -using System.Threading.Tasks; using System.Runtime.Caching; +using System.Threading.Tasks; namespace Rehau.Sku.Assist { @@ -17,7 +17,7 @@ namespace Rehau.Sku.Assist else { - object result = ExcelAsyncUtil.Run("Rauname", new[] { request }, + object result = ExcelAsyncUtil.Run("RauName", new[] { request }, delegate { Task<IProduct> p = Task.Run(() => SkuAssist.GetProduct(request)); @@ -32,7 +32,6 @@ namespace Rehau.Sku.Assist IProduct product = result as IProduct; MemoryCache.Default.Add(request, product, System.DateTime.Now.AddMinutes(10)); - //MemoryCache.Default.Add(product.Name, product, System.DateTime.Now.AddMinutes(10)); return product.Name; } } @@ -42,8 +41,8 @@ namespace Rehau.Sku.Assist { if (MemoryCache.Default.Contains(request)) { - IProduct result = MemoryCache.Default[request] as IProduct; - return result.Sku; + IProduct product = MemoryCache.Default[request] as IProduct; + return product.Id; } else { @@ -62,8 +61,7 @@ namespace Rehau.Sku.Assist IProduct product = result as IProduct; MemoryCache.Default.Add(request, product, System.DateTime.Now.AddMinutes(10)); - //MemoryCache.Default.Add(product.Sku, product, System.DateTime.Now.AddMinutes(10)); - return product.Sku; + return product.Id; } } } |