diff --git a/Rehau.Sku.Assist.csproj b/Rehau.Sku.Assist.csproj index cc83173..87bd1e0 100644 --- a/Rehau.Sku.Assist.csproj +++ b/Rehau.Sku.Assist.csproj @@ -48,6 +48,9 @@ packages\ExcelDna.Registration.1.5.0\lib\net452\ExcelDna.Registration.dll True + + packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll + packages\NUnit.3.13.2\lib\net45\nunit.framework.dll @@ -86,9 +89,9 @@ + - 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 contentTask = Task.Run(() => HttpClientUtil.GetContentByUriAsync(uri)); Task documentTask = await contentTask.ContinueWith(content => HttpClientUtil.ContentToDocAsync(content)); - IProduct product = await documentTask.ContinueWith(doc => SkuAssist.GetFirstProduct(doc.Result)); + return GetProduct(documentTask.Result); + } + + public static IProduct GetProduct(IDocument d) + { + string script = d.Scripts + .Where(s => s.InnerHtml.Contains("dataLayer")) + .First() + .InnerHtml; + + string json = script + .Substring(script.IndexOf("push(") + 5) + .TrimEnd(new[] { ')', ';', '\n', ' ' }); + + StoreResponce storeResponse = JsonConvert.DeserializeObject(json); + IProduct product = storeResponse + .Ecommerce + .Impressions + .Where(i => Regex.IsMatch(i.Id, @"\d{11}", RegexOptions.None)) + .FirstOrDefault(); + return product; } - - public static IProduct GetFirstProduct(IDocument doc) - { - 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(); - } - - 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; - } - - public static string GetFistResultImageLink(IDocument doc) - { - var imageSource = doc.Images - .Where(x => x.ClassName == "product-item__image") - .FirstOrDefault(); - return imageSource != null ? imageSource.Source : "Нет ссылки"; - } } } \ 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 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 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; } } } diff --git a/packages.config b/packages.config index 96d00fe..b42dcc6 100644 --- a/packages.config +++ b/packages.config @@ -4,6 +4,7 @@ +