From 3ea18ae25e98527aa85835c9221ea01b36560b33 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Mon, 29 Nov 2021 21:24:44 +0300 Subject: Add Uri Converter --- Source/Assistant/IProduct.cs | 9 +++++ Source/Assistant/Product.cs | 21 ++++++++++++ Source/Assistant/SkuAssist.cs | 80 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 Source/Assistant/IProduct.cs create mode 100644 Source/Assistant/Product.cs create mode 100644 Source/Assistant/SkuAssist.cs (limited to 'Source/Assistant') diff --git a/Source/Assistant/IProduct.cs b/Source/Assistant/IProduct.cs new file mode 100644 index 0000000..aca3ff5 --- /dev/null +++ b/Source/Assistant/IProduct.cs @@ -0,0 +1,9 @@ +namespace Rehau.Sku.Assist +{ + interface IProduct + { + string Sku { get; } + string Name { get; } + string Uri { get; } + } +} diff --git a/Source/Assistant/Product.cs b/Source/Assistant/Product.cs new file mode 100644 index 0000000..17a7065 --- /dev/null +++ b/Source/Assistant/Product.cs @@ -0,0 +1,21 @@ +namespace Rehau.Sku.Assist +{ + public class Product : IProduct + { + public string Sku { get; } + public string Name { get; } + + public string Uri => throw new System.NotImplementedException(); + + public Product(string sku, string name) + { + Sku = sku; + Name = name; + } + + 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 new file mode 100644 index 0000000..152dbbb --- /dev/null +++ b/Source/Assistant/SkuAssist.cs @@ -0,0 +1,80 @@ +using AngleSharp; +using AngleSharp.Dom; +using System; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Threading.Tasks; + +namespace Rehau.Sku.Assist +{ + static class SkuAssist + { + private static HttpClient _httpClient; + private enum ResponseOrder + { + NoSettings, + Relevance, + Name, + Price, + Series + } + private static void _EnsureHttpClientRunning() + { + if (_httpClient == null) + _httpClient = new HttpClient(); + } + + public async static Task GetContent(string request) + { + Uri uri = _ConvertToUri(request, ResponseOrder.NoSettings); + _EnsureHttpClientRunning(); + ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; + + return await _httpClient.GetStringAsync(uri); + } + + public async static Task GetDocument(Task source) + { + IConfiguration config = Configuration.Default; + IBrowsingContext context = BrowsingContext.New(config); + + return await context.OpenAsync(req => req.Content(source.Result)); + } + + public static IProduct GetProductFromDocument(IDocument document) + { + return document + .All + .Where(e => e.ClassName == "product-item__desc-top") + .Select(e => new Product(e.Children[0].TextContent, e.Children[1].TextContent.Trim(new[] { '\n', ' ' }))) + .FirstOrDefault(); + } + + private static Uri _ConvertToUri(this string request, ResponseOrder order) + { + string cleanedRequest = request._CleanRequest(); + switch (order) + { + case ResponseOrder.Relevance: + return new Uri("https://shop-rehau.ru/catalogsearch/result/index/?dir=asc&order=relevance&q=" + cleanedRequest); + case ResponseOrder.Name: + return new Uri("https://shop-rehau.ru/catalogsearch/result/index/?dir=asc&order=name&q=" + cleanedRequest); + case ResponseOrder.Price: + return new Uri("https://shop-rehau.ru/catalogsearch/result/index/?dir=asc&order=price&q=" + cleanedRequest); + case ResponseOrder.Series: + return new Uri("https://shop-rehau.ru/catalogsearch/result/index/?dir=asc&order=sch_product_series&q=" + cleanedRequest); + case ResponseOrder.NoSettings: + return new Uri("https://shop-rehau.ru/catalogsearch/result/?q=" + cleanedRequest); + default: + throw new ArgumentException(); + } + } + private static string _CleanRequest(this string input) + { + return input.Replace("+", " plus "); + } + } +} + + -- cgit v1.2.3 From b5c71350700a486b8a8d1fa0fe7db32220963eba Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Fri, 3 Dec 2021 12:57:22 +0300 Subject: Add Caching and replace HttpClientUtil class to external file --- Source/Assistant/HttpClientUtil.cs | 74 ++++++++++++++++++++++++++++++ Source/Assistant/IProduct.cs | 6 ++- Source/Assistant/Product.cs | 14 ++++-- Source/Assistant/SkuAssist.cs | 94 +++++++++++++++----------------------- 4 files changed, 127 insertions(+), 61 deletions(-) create mode 100644 Source/Assistant/HttpClientUtil.cs (limited to 'Source/Assistant') diff --git a/Source/Assistant/HttpClientUtil.cs b/Source/Assistant/HttpClientUtil.cs new file mode 100644 index 0000000..f9c144b --- /dev/null +++ b/Source/Assistant/HttpClientUtil.cs @@ -0,0 +1,74 @@ +using AngleSharp; +using AngleSharp.Dom; +using System; +using System.Net; +using System.Net.Http; +using System.Threading.Tasks; +using System.Text; + +namespace Rehau.Sku.Assist +{ + static class HttpClientUtil + { + private static HttpClient _httpClient = AddIn.httpClient; + + public async static Task GetContentByUriAsync(Uri uri) + { + ServicePointManager.SecurityProtocol = + SecurityProtocolType.Tls12 | + SecurityProtocolType.Tls11 | + SecurityProtocolType.Tls; + + return await _httpClient.GetStringAsync(uri); + } + + public async static Task ContentToDocAsync(Task content) + { + IConfiguration config = Configuration.Default; + IBrowsingContext context = BrowsingContext.New(config); + + return await context.OpenAsync(req => req.Content(content.Result)); + } + + public static Uri ConvertToUri(this string request, ResponseOrder order) + { + UriBuilder baseUri = new UriBuilder("https", "shop-rehau.ru"); + + baseUri.Path = "/catalogsearch/result/index/"; + string cleanedRequest = request._CleanRequest(); + + switch (order) + { + case ResponseOrder.Relevance: + baseUri.Query = "dir=asc&order=relevance&q=" + cleanedRequest; + break; + case ResponseOrder.Name: + baseUri.Query = "dir=asc&order=name&q=" + cleanedRequest; + break; + case ResponseOrder.Price: + baseUri.Query = "dir=asc&order=price&q=" + cleanedRequest; + break; + case ResponseOrder.Series: + baseUri.Query = "dir=asc&order=sch_product_series&q=" + cleanedRequest; + break; + case ResponseOrder.NoSettings: + baseUri.Query = "q=" + cleanedRequest; + break; + default: + throw new ArgumentException(); + } + + return baseUri.Uri; + } + + private static string _CleanRequest(this string input) + { + return new StringBuilder(input) + .Replace("+", " plus ") + .Replace("РХ", "") + .Replace("º", " ") + .Replace(".", " ") + .ToString(); + } + } +} \ No newline at end of file diff --git a/Source/Assistant/IProduct.cs b/Source/Assistant/IProduct.cs index aca3ff5..de0eccf 100644 --- a/Source/Assistant/IProduct.cs +++ b/Source/Assistant/IProduct.cs @@ -1,9 +1,11 @@ -namespace Rehau.Sku.Assist +using System; + +namespace Rehau.Sku.Assist { interface IProduct { string Sku { get; } string Name { get; } - string Uri { get; } + Uri Uri { get; } } } diff --git a/Source/Assistant/Product.cs b/Source/Assistant/Product.cs index 17a7065..22905af 100644 --- a/Source/Assistant/Product.cs +++ b/Source/Assistant/Product.cs @@ -1,11 +1,12 @@ -namespace Rehau.Sku.Assist +using System; + +namespace Rehau.Sku.Assist { public class Product : IProduct { public string Sku { get; } public string Name { get; } - - public string Uri => throw new System.NotImplementedException(); + public Uri Uri { get; } public Product(string sku, string name) { @@ -13,6 +14,13 @@ 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})"; diff --git a/Source/Assistant/SkuAssist.cs b/Source/Assistant/SkuAssist.cs index 152dbbb..121bc88 100644 --- a/Source/Assistant/SkuAssist.cs +++ b/Source/Assistant/SkuAssist.cs @@ -1,80 +1,62 @@ -using AngleSharp; -using AngleSharp.Dom; +using AngleSharp.Dom; +using AngleSharp.Html.Dom; using System; using System.Linq; -using System.Net; -using System.Net.Http; using System.Threading.Tasks; +using System.Text.RegularExpressions; namespace Rehau.Sku.Assist { + public enum ResponseOrder + { + NoSettings, + Relevance, + Name, + Price, + Series + } + static class SkuAssist { - private static HttpClient _httpClient; - private enum ResponseOrder - { - NoSettings, - Relevance, - Name, - Price, - Series - } - private static void _EnsureHttpClientRunning() + public static async Task GetProduct(string request) { - if (_httpClient == null) - _httpClient = new HttpClient(); - } + Uri uri = request.ConvertToUri(ResponseOrder.NoSettings); - public async static Task GetContent(string request) - { - Uri uri = _ConvertToUri(request, ResponseOrder.NoSettings); - _EnsureHttpClientRunning(); - ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; + Task contentTask = Task.Run(() => HttpClientUtil.GetContentByUriAsync(uri)); + Task documentTask = await contentTask.ContinueWith(content => HttpClientUtil.ContentToDocAsync(content)); - return await _httpClient.GetStringAsync(uri); + IProduct product = await documentTask.ContinueWith(doc => SkuAssist.GetFirstProduct(doc.Result)); + return product; } - public async static Task GetDocument(Task source) + public static IProduct GetFirstProduct(IDocument doc) { - IConfiguration config = Configuration.Default; - IBrowsingContext context = BrowsingContext.New(config); - - return await context.OpenAsync(req => req.Content(source.Result)); - } - - public static IProduct GetProductFromDocument(IDocument document) - { - return document + return doc .All .Where(e => e.ClassName == "product-item__desc-top") - .Select(e => new Product(e.Children[0].TextContent, e.Children[1].TextContent.Trim(new[] { '\n', ' ' }))) + .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(); } - private static Uri _ConvertToUri(this string request, ResponseOrder order) + public static Uri GetFirstResultLink(IDocument doc) { - string cleanedRequest = request._CleanRequest(); - switch (order) - { - case ResponseOrder.Relevance: - return new Uri("https://shop-rehau.ru/catalogsearch/result/index/?dir=asc&order=relevance&q=" + cleanedRequest); - case ResponseOrder.Name: - return new Uri("https://shop-rehau.ru/catalogsearch/result/index/?dir=asc&order=name&q=" + cleanedRequest); - case ResponseOrder.Price: - return new Uri("https://shop-rehau.ru/catalogsearch/result/index/?dir=asc&order=price&q=" + cleanedRequest); - case ResponseOrder.Series: - return new Uri("https://shop-rehau.ru/catalogsearch/result/index/?dir=asc&order=sch_product_series&q=" + cleanedRequest); - case ResponseOrder.NoSettings: - return new Uri("https://shop-rehau.ru/catalogsearch/result/?q=" + cleanedRequest); - default: - throw new ArgumentException(); - } + var link = new Uri(doc + .Links + .Where(e => e.ClassName == "product-item__title-link js-name") + .Select(l => ((IHtmlAnchorElement)l).Href) + .FirstOrDefault()); + return link; } - private static string _CleanRequest(this string input) + + public static string GetFistResultImageLink(IDocument doc) { - return input.Replace("+", " plus "); + var imageSource = doc.Images + .Where(x => x.ClassName == "product-item__image") + .FirstOrDefault(); + return imageSource != null ? imageSource.Source : "Нет ссылки"; } } -} - - +} \ No newline at end of file -- cgit v1.2.3 From 915929fa9d0738a4e4db4134ea522b343ab2c1d2 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Fri, 3 Dec 2021 19:30:35 +0300 Subject: Add Json parsing and refactoring --- Source/Assistant/IProduct.cs | 3 +-- Source/Assistant/Product.cs | 29 ------------------------ Source/Assistant/SkuAssist.cs | 46 +++++++++++++++------------------------ Source/Assistant/StoreResponse.cs | 21 ++++++++++++++++++ 4 files changed, 40 insertions(+), 59 deletions(-) delete mode 100644 Source/Assistant/Product.cs create mode 100644 Source/Assistant/StoreResponse.cs (limited to 'Source/Assistant') 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 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(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 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 -- cgit v1.2.3 From 355e2d36729b607c80b75a4e78342ed487cc56d4 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Fri, 3 Dec 2021 22:25:20 +0300 Subject: Add RAUPRICE function. Refactoring. --- Source/Assistant/IProduct.cs | 5 ++-- Source/Assistant/SkuAssist.cs | 53 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 4 deletions(-) (limited to 'Source/Assistant') diff --git a/Source/Assistant/IProduct.cs b/Source/Assistant/IProduct.cs index 54b3dd0..d5db286 100644 --- a/Source/Assistant/IProduct.cs +++ b/Source/Assistant/IProduct.cs @@ -1,10 +1,9 @@ -using System; - -namespace Rehau.Sku.Assist +namespace Rehau.Sku.Assist { interface IProduct { string Id { get; } string Name { get; } + string Price { get; } } } diff --git a/Source/Assistant/SkuAssist.cs b/Source/Assistant/SkuAssist.cs index b6b5f7e..fd69a5b 100644 --- a/Source/Assistant/SkuAssist.cs +++ b/Source/Assistant/SkuAssist.cs @@ -1,7 +1,9 @@ using AngleSharp.Dom; +using ExcelDna.Integration; using Newtonsoft.Json; using System; using System.Linq; +using System.Runtime.Caching; using System.Text.RegularExpressions; using System.Threading.Tasks; @@ -16,6 +18,13 @@ namespace Rehau.Sku.Assist Series } + public enum ProductField + { + Name, + Id, + Price + } + static class SkuAssist { public static async Task GetProduct(string request) @@ -43,10 +52,52 @@ namespace Rehau.Sku.Assist IProduct product = storeResponse .Ecommerce .Impressions - .Where(i => Regex.IsMatch(i.Id, @"\d{11}", RegexOptions.None)) + .Where(p => Regex.IsMatch(p.Id, @"\d{11}", RegexOptions.None)) .FirstOrDefault(); return product; } + + public static object GetProduct(string request, ProductField field) + { + IProduct product; + + if (MemoryCache.Default.Contains(request)) + { + product = MemoryCache.Default[request] as IProduct; + } + + else + { + object result = ExcelAsyncUtil.Run("RauName", new[] { request }, + delegate + { + Task p = Task.Run(() => GetProduct(request)); + return p.Result; + }); + + if (result == null) + return "Не найдено"; + + if (result.Equals(ExcelError.ExcelErrorNA)) + return "Загрузка..."; + + product = result as IProduct; + MemoryCache.Default.Add(request, product, DateTime.Now.AddMinutes(10)); + return product.Name; + } + + switch (field) + { + case ProductField.Name: + return product.Name; + case ProductField.Id: + return product.Id; + case ProductField.Price: + return product.Price; + default: + return ExcelError.ExcelErrorValue; + } + } } } \ No newline at end of file -- cgit v1.2.3 From 4acde8a85b29ca968739bf1bf38d614a2830f6e7 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Fri, 3 Dec 2021 23:02:40 +0300 Subject: Fix wrong return --- Source/Assistant/SkuAssist.cs | 1 - 1 file changed, 1 deletion(-) (limited to 'Source/Assistant') diff --git a/Source/Assistant/SkuAssist.cs b/Source/Assistant/SkuAssist.cs index fd69a5b..69c1a81 100644 --- a/Source/Assistant/SkuAssist.cs +++ b/Source/Assistant/SkuAssist.cs @@ -84,7 +84,6 @@ namespace Rehau.Sku.Assist product = result as IProduct; MemoryCache.Default.Add(request, product, DateTime.Now.AddMinutes(10)); - return product.Name; } switch (field) -- cgit v1.2.3