From 589246aaef847847b65d00e87fb5826603724e81 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Sun, 5 Dec 2021 21:19:28 +0300 Subject: Move responseOrder field to AddIn class --- Source/Assistant/HttpClientUtil.cs | 4 ++-- Source/Assistant/SkuAssist.cs | 11 +---------- 2 files changed, 3 insertions(+), 12 deletions(-) (limited to 'Source/Assistant') diff --git a/Source/Assistant/HttpClientUtil.cs b/Source/Assistant/HttpClientUtil.cs index 3ad5d85..6277770 100644 --- a/Source/Assistant/HttpClientUtil.cs +++ b/Source/Assistant/HttpClientUtil.cs @@ -30,14 +30,14 @@ namespace Rehau.Sku.Assist return await context.OpenAsync(req => req.Content(content.Result)); } - public static Uri ConvertToUri(this string request, ResponseOrder order) + public static Uri ConvertToUri(this string request) { UriBuilder baseUri = new UriBuilder("https", "shop-rehau.ru"); baseUri.Path = "/catalogsearch/result/index/"; string cleanedRequest = request._CleanRequest(); - switch (order) + switch (AddIn.responseOrder) { case ResponseOrder.Relevance: baseUri.Query = "dir=asc&order=relevance&q=" + cleanedRequest; diff --git a/Source/Assistant/SkuAssist.cs b/Source/Assistant/SkuAssist.cs index e8ce789..a0195ba 100644 --- a/Source/Assistant/SkuAssist.cs +++ b/Source/Assistant/SkuAssist.cs @@ -10,15 +10,6 @@ using System.Threading.Tasks; namespace Rehau.Sku.Assist { - public enum ResponseOrder - { - NoSettings, - Relevance, - Name, - Price, - Series - } - public enum ProductField { Name, @@ -30,7 +21,7 @@ namespace Rehau.Sku.Assist { public static async Task GetProduct(string request) { - Uri uri = request.ConvertToUri(ResponseOrder.NoSettings); + Uri uri = request.ConvertToUri(); Task contentTask = Task.Run(() => HttpClientUtil.GetContentByUriAsync(uri)); Task documentTask = await contentTask.ContinueWith(content => HttpClientUtil.ContentToDocAsync(content)); -- cgit v1.2.3 From 8bc62a6f024ae2c7d346fe3570c9c0241a773ec5 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Sun, 5 Dec 2021 21:25:30 +0300 Subject: Refactoring --- Source/Assistant/SkuAssist.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'Source/Assistant') diff --git a/Source/Assistant/SkuAssist.cs b/Source/Assistant/SkuAssist.cs index a0195ba..bd757b0 100644 --- a/Source/Assistant/SkuAssist.cs +++ b/Source/Assistant/SkuAssist.cs @@ -29,12 +29,13 @@ namespace Rehau.Sku.Assist return GetProduct(documentTask.Result); } - public static IProduct GetProduct(IDocument d) + public static IProduct GetProduct(IDocument document) { - string script = d.Scripts - .Where(s => s.InnerHtml.Contains("dataLayer")) - .First() - .InnerHtml; + string script = document + .Scripts + .Where(s => s.InnerHtml.Contains("dataLayer")) + .First() + .InnerHtml; string json = script .Substring(script.IndexOf("push(") + 5) -- cgit v1.2.3 From 0288ae0a185095d182dfc18af6709bac7159f5e0 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Mon, 6 Dec 2021 19:50:00 +0300 Subject: Add IsRehauSku() method --- Source/Assistant/SkuAssist.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'Source/Assistant') diff --git a/Source/Assistant/SkuAssist.cs b/Source/Assistant/SkuAssist.cs index bd757b0..bfe7b90 100644 --- a/Source/Assistant/SkuAssist.cs +++ b/Source/Assistant/SkuAssist.cs @@ -48,7 +48,7 @@ namespace Rehau.Sku.Assist IProduct product = storeResponse .Ecommerce .Impressions - .Where(p => Regex.IsMatch(p.Id, @"\d{11}", RegexOptions.None)) + .Where(p => p.Id.IsRehauSku()) .FirstOrDefault(); return product; @@ -94,5 +94,12 @@ namespace Rehau.Sku.Assist return ExcelError.ExcelErrorValue; } } + + public static bool IsRehauSku(this string line) + { + return Regex.IsMatch(line, @"\d{11}") && + line[0].Equals('1') && + line[7].Equals('1'); + } } } \ No newline at end of file -- cgit v1.2.3 From 4743aa706e8342cdb3202c59e82bddef277cabfa Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Mon, 6 Dec 2021 19:52:34 +0300 Subject: Refactoring --- Source/Assistant/SkuAssist.cs | 3 --- 1 file changed, 3 deletions(-) (limited to 'Source/Assistant') diff --git a/Source/Assistant/SkuAssist.cs b/Source/Assistant/SkuAssist.cs index bfe7b90..a524daa 100644 --- a/Source/Assistant/SkuAssist.cs +++ b/Source/Assistant/SkuAssist.cs @@ -28,7 +28,6 @@ namespace Rehau.Sku.Assist return GetProduct(documentTask.Result); } - public static IProduct GetProduct(IDocument document) { string script = document @@ -53,7 +52,6 @@ namespace Rehau.Sku.Assist return product; } - public static object GetProduct(string request, ProductField field) { IProduct product; @@ -94,7 +92,6 @@ namespace Rehau.Sku.Assist return ExcelError.ExcelErrorValue; } } - public static bool IsRehauSku(this string line) { return Regex.IsMatch(line, @"\d{11}") && -- cgit v1.2.3 From 1136d5e94ae33c3c3c6a4df88b1b326386feca7d Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Tue, 7 Dec 2021 08:34:48 +0300 Subject: Rename ResponseOrder default value. Move switch NoSettings to default. --- Source/Assistant/HttpClientUtil.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'Source/Assistant') diff --git a/Source/Assistant/HttpClientUtil.cs b/Source/Assistant/HttpClientUtil.cs index 6277770..6eb0b69 100644 --- a/Source/Assistant/HttpClientUtil.cs +++ b/Source/Assistant/HttpClientUtil.cs @@ -51,11 +51,9 @@ namespace Rehau.Sku.Assist case ResponseOrder.Series: baseUri.Query = "dir=asc&order=sch_product_series&q=" + cleanedRequest; break; - case ResponseOrder.NoSettings: + default: baseUri.Query = "q=" + cleanedRequest; break; - default: - throw new ArgumentException(); } return baseUri.Uri; -- cgit v1.2.3 From adf66ad2a2c358ec8d3edfc92f96ba0376888e74 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Tue, 7 Dec 2021 08:48:35 +0300 Subject: Add try...catch to GetProduct --- Source/Assistant/SkuAssist.cs | 44 ++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) (limited to 'Source/Assistant') diff --git a/Source/Assistant/SkuAssist.cs b/Source/Assistant/SkuAssist.cs index a524daa..6d72a72 100644 --- a/Source/Assistant/SkuAssist.cs +++ b/Source/Assistant/SkuAssist.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Runtime.Caching; using System.Text.RegularExpressions; using System.Threading.Tasks; +using System.Windows.Forms; namespace Rehau.Sku.Assist { @@ -30,27 +31,36 @@ namespace Rehau.Sku.Assist } public static IProduct GetProduct(IDocument document) { - string script = document - .Scripts - .Where(s => s.InnerHtml.Contains("dataLayer")) - .First() - .InnerHtml; + try + { + string script = document + .Scripts + .Where(s => s.InnerHtml.Contains("dataLayer")) + .FirstOrDefault() + .InnerHtml; - string json = script - .Substring(script.IndexOf("push(") + 5) - .TrimEnd(new[] { ')', ';', '\n', ' ' }); + string json = script + .Substring(script.IndexOf("push(") + 5) + .TrimEnd(new[] { ')', ';', '\n', ' ' }); - if (!json.Contains("impressions")) - return null; + if (!json.Contains("impressions")) + return null; - StoreResponce storeResponse = JsonConvert.DeserializeObject(json); - IProduct product = storeResponse - .Ecommerce - .Impressions - .Where(p => p.Id.IsRehauSku()) - .FirstOrDefault(); + StoreResponce storeResponse = JsonConvert.DeserializeObject(json); + IProduct product = storeResponse + .Ecommerce + .Impressions + .Where(p => p.Id.IsRehauSku()) + .FirstOrDefault(); + + return product; + } - return product; + catch (NullReferenceException e) + { + MessageBox.Show(e.Message, "Ошибка получения данных", MessageBoxButtons.OK, MessageBoxIcon.Error); + return null; + } } public static object GetProduct(string request, ProductField field) { -- cgit v1.2.3 From c42e3e604d775bdaa896d7bc3d9886f6ce64c773 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 8 Dec 2021 09:44:23 +0300 Subject: Add t-pieces request filter. Exclude CleanUp method to RequestModifier class. --- Source/Assistant/HttpClientUtil.cs | 14 +-------- Source/Assistant/RequestModifier.cs | 60 +++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 13 deletions(-) create mode 100644 Source/Assistant/RequestModifier.cs (limited to 'Source/Assistant') diff --git a/Source/Assistant/HttpClientUtil.cs b/Source/Assistant/HttpClientUtil.cs index 6eb0b69..2d439ea 100644 --- a/Source/Assistant/HttpClientUtil.cs +++ b/Source/Assistant/HttpClientUtil.cs @@ -4,7 +4,6 @@ using System; using System.Net; using System.Net.Http; using System.Threading.Tasks; -using System.Text; namespace Rehau.Sku.Assist { @@ -35,7 +34,7 @@ namespace Rehau.Sku.Assist UriBuilder baseUri = new UriBuilder("https", "shop-rehau.ru"); baseUri.Path = "/catalogsearch/result/index/"; - string cleanedRequest = request._CleanRequest(); + string cleanedRequest = request.CleanRequest(); switch (AddIn.responseOrder) { @@ -58,16 +57,5 @@ namespace Rehau.Sku.Assist return baseUri.Uri; } - - private static string _CleanRequest(this string input) - { - return new StringBuilder(input) - .Replace("+", " plus ") - .Replace("РХ", "") - .Replace("º", " ") - .Replace(".", " ") - .Replace("Ø", " ") - .ToString(); - } } } \ No newline at end of file diff --git a/Source/Assistant/RequestModifier.cs b/Source/Assistant/RequestModifier.cs new file mode 100644 index 0000000..28f5775 --- /dev/null +++ b/Source/Assistant/RequestModifier.cs @@ -0,0 +1,60 @@ +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; + +namespace Rehau.Sku.Assist +{ + public static class RequestModifier + { + public static string CleanRequest(this string input) + { + string replace = new StringBuilder(input) + .Replace("+", " plus ") + .Replace("РХ", "") + .Replace("º", " ") + .Replace(".", " ") + .Replace("Ø", " ") + .ToString(); + + return replace._tPiece(); + } + + private static string _tPiece(this string line) + { + if (!line.ToLower().Contains("тройник")) + return line; + + string m = Regex.Match(line, @"\d{2}.\d{2}.\d{2}").Value; + + int endFaceA = int.Parse($"{m[0]}{m[1]}"); + int side = int.Parse($"{m[3]}{m[4]}"); + int endFaceB = int.Parse($"{m[6]}{m[7]}"); + + int[] endFaces = new[] { endFaceA, endFaceB }; + + List additions = new List(); + + if (endFaces.All(x => x < side)) + additions.Add("увеличенный боковой"); + + else + { + if (new[] { endFaceA, endFaceB, side }.Distinct().Count() == 1) + additions.Add("равнопроходной"); + else + additions.Add("уменьшенный"); + + if (endFaces.Any(x => x > side)) + additions.Add("боковой"); + if (endFaceA != endFaceB) + additions.Add("торцевой"); + } + + string piece = $" {endFaces.Max()}-{side}-{endFaces.Min()} "; + string replace = string.Join(" ", additions) + piece; + + return line.Replace(m, replace); + } + } +} \ No newline at end of file -- cgit v1.2.3 From 8da955aab1ec6bd9ef6cbff13f47b1f89c465dbf Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 8 Dec 2021 10:38:58 +0300 Subject: Refactoring regex methods --- Source/Assistant/RequestModifier.cs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'Source/Assistant') diff --git a/Source/Assistant/RequestModifier.cs b/Source/Assistant/RequestModifier.cs index 28f5775..7532b2d 100644 --- a/Source/Assistant/RequestModifier.cs +++ b/Source/Assistant/RequestModifier.cs @@ -22,14 +22,16 @@ namespace Rehau.Sku.Assist private static string _tPiece(this string line) { - if (!line.ToLower().Contains("тройник")) + Regex regex = new Regex(@"\d{2}.\d{2}.\d{2}"); + + if (!regex.IsMatch(line)) return line; - string m = Regex.Match(line, @"\d{2}.\d{2}.\d{2}").Value; + string match = regex.Match(line).Value; - int endFaceA = int.Parse($"{m[0]}{m[1]}"); - int side = int.Parse($"{m[3]}{m[4]}"); - int endFaceB = int.Parse($"{m[6]}{m[7]}"); + int endFaceA = int.Parse($"{match[0]}{match[1]}"), + side = int.Parse($"{match[3]}{match[4]}"), + endFaceB = int.Parse($"{match[6]}{match[7]}"); int[] endFaces = new[] { endFaceA, endFaceB }; @@ -47,14 +49,15 @@ namespace Rehau.Sku.Assist if (endFaces.Any(x => x > side)) additions.Add("боковой"); + if (endFaceA != endFaceB) additions.Add("торцевой"); } string piece = $" {endFaces.Max()}-{side}-{endFaces.Min()} "; - string replace = string.Join(" ", additions) + piece; + string modifiedMatch = string.Join(" ", additions) + piece; - return line.Replace(m, replace); + return line.Replace(match, modifiedMatch); } } } \ No newline at end of file -- cgit v1.2.3 From 89e420a890afb39c342a82b2ca1bf2f18df407dc Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 8 Dec 2021 12:03:33 +0300 Subject: Refactoring --- Source/Assistant/RequestModifier.cs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'Source/Assistant') diff --git a/Source/Assistant/RequestModifier.cs b/Source/Assistant/RequestModifier.cs index 7532b2d..d6433ed 100644 --- a/Source/Assistant/RequestModifier.cs +++ b/Source/Assistant/RequestModifier.cs @@ -17,10 +17,10 @@ namespace Rehau.Sku.Assist .Replace("Ø", " ") .ToString(); - return replace._tPiece(); + return replace._tPieceNormalize(); } - private static string _tPiece(this string line) + private static string _tPieceNormalize(this string line) { Regex regex = new Regex(@"\d{2}.\d{2}.\d{2}"); @@ -29,11 +29,12 @@ namespace Rehau.Sku.Assist string match = regex.Match(line).Value; - int endFaceA = int.Parse($"{match[0]}{match[1]}"), - side = int.Parse($"{match[3]}{match[4]}"), - endFaceB = int.Parse($"{match[6]}{match[7]}"); - - int[] endFaces = new[] { endFaceA, endFaceB }; + int side = int.Parse($"{match[3]}{match[4]}"); + int[] endFaces = new int[] + { + int.Parse($"{match[0]}{match[1]}"), + int.Parse($"{match[6]}{match[7]}") + }; List additions = new List(); @@ -42,7 +43,7 @@ namespace Rehau.Sku.Assist else { - if (new[] { endFaceA, endFaceB, side }.Distinct().Count() == 1) + if (new[] { endFaces[0], endFaces[1], side }.Distinct().Count() == 1) additions.Add("равнопроходной"); else additions.Add("уменьшенный"); @@ -50,7 +51,7 @@ namespace Rehau.Sku.Assist if (endFaces.Any(x => x > side)) additions.Add("боковой"); - if (endFaceA != endFaceB) + if (endFaces[0] != endFaces[1]) additions.Add("торцевой"); } -- cgit v1.2.3 From 74877f874e401d29f0576c5027abba83587103f3 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 8 Dec 2021 13:58:37 +0300 Subject: Rename to RehauSku --- Source/Assistant/HttpClientUtil.cs | 2 +- Source/Assistant/IProduct.cs | 2 +- Source/Assistant/RequestModifier.cs | 2 +- Source/Assistant/SkuAssist.cs | 2 +- Source/Assistant/StoreResponse.cs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) (limited to 'Source/Assistant') diff --git a/Source/Assistant/HttpClientUtil.cs b/Source/Assistant/HttpClientUtil.cs index 2d439ea..57cd060 100644 --- a/Source/Assistant/HttpClientUtil.cs +++ b/Source/Assistant/HttpClientUtil.cs @@ -5,7 +5,7 @@ using System.Net; using System.Net.Http; using System.Threading.Tasks; -namespace Rehau.Sku.Assist +namespace RehauSku.Assist { static class HttpClientUtil { diff --git a/Source/Assistant/IProduct.cs b/Source/Assistant/IProduct.cs index d5db286..e469ff8 100644 --- a/Source/Assistant/IProduct.cs +++ b/Source/Assistant/IProduct.cs @@ -1,4 +1,4 @@ -namespace Rehau.Sku.Assist +namespace RehauSku.Assist { interface IProduct { diff --git a/Source/Assistant/RequestModifier.cs b/Source/Assistant/RequestModifier.cs index d6433ed..c479a74 100644 --- a/Source/Assistant/RequestModifier.cs +++ b/Source/Assistant/RequestModifier.cs @@ -3,7 +3,7 @@ using System.Linq; using System.Text; using System.Text.RegularExpressions; -namespace Rehau.Sku.Assist +namespace RehauSku.Assist { public static class RequestModifier { diff --git a/Source/Assistant/SkuAssist.cs b/Source/Assistant/SkuAssist.cs index 6d72a72..775a33c 100644 --- a/Source/Assistant/SkuAssist.cs +++ b/Source/Assistant/SkuAssist.cs @@ -9,7 +9,7 @@ using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; -namespace Rehau.Sku.Assist +namespace RehauSku.Assist { public enum ProductField { diff --git a/Source/Assistant/StoreResponse.cs b/Source/Assistant/StoreResponse.cs index 78fe846..ef1f05d 100644 --- a/Source/Assistant/StoreResponse.cs +++ b/Source/Assistant/StoreResponse.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; -namespace Rehau.Sku.Assist +namespace RehauSku.Assist { public class StoreResponce { -- cgit v1.2.3 From dc1fc8b221e9324fe0f82c4ea4a32d87d282bd25 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 8 Dec 2021 14:45:14 +0300 Subject: refactoring namespaces --- Source/Assistant/HttpClientUtil.cs | 2 +- Source/Assistant/IProduct.cs | 2 +- Source/Assistant/RequestModifier.cs | 2 +- Source/Assistant/SkuAssist.cs | 2 +- Source/Assistant/StoreResponse.cs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) (limited to 'Source/Assistant') diff --git a/Source/Assistant/HttpClientUtil.cs b/Source/Assistant/HttpClientUtil.cs index 57cd060..95a90e9 100644 --- a/Source/Assistant/HttpClientUtil.cs +++ b/Source/Assistant/HttpClientUtil.cs @@ -5,7 +5,7 @@ using System.Net; using System.Net.Http; using System.Threading.Tasks; -namespace RehauSku.Assist +namespace RehauSku.Assistant { static class HttpClientUtil { diff --git a/Source/Assistant/IProduct.cs b/Source/Assistant/IProduct.cs index e469ff8..9494eeb 100644 --- a/Source/Assistant/IProduct.cs +++ b/Source/Assistant/IProduct.cs @@ -1,4 +1,4 @@ -namespace RehauSku.Assist +namespace RehauSku.Assistant { interface IProduct { diff --git a/Source/Assistant/RequestModifier.cs b/Source/Assistant/RequestModifier.cs index c479a74..0db0b90 100644 --- a/Source/Assistant/RequestModifier.cs +++ b/Source/Assistant/RequestModifier.cs @@ -3,7 +3,7 @@ using System.Linq; using System.Text; using System.Text.RegularExpressions; -namespace RehauSku.Assist +namespace RehauSku.Assistant { public static class RequestModifier { diff --git a/Source/Assistant/SkuAssist.cs b/Source/Assistant/SkuAssist.cs index 775a33c..eddb0cd 100644 --- a/Source/Assistant/SkuAssist.cs +++ b/Source/Assistant/SkuAssist.cs @@ -9,7 +9,7 @@ using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; -namespace RehauSku.Assist +namespace RehauSku.Assistant { public enum ProductField { diff --git a/Source/Assistant/StoreResponse.cs b/Source/Assistant/StoreResponse.cs index ef1f05d..8e1759d 100644 --- a/Source/Assistant/StoreResponse.cs +++ b/Source/Assistant/StoreResponse.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; -namespace RehauSku.Assist +namespace RehauSku.Assistant { public class StoreResponce { -- cgit v1.2.3 From 93835233f3ddc335d93b3092d5ae2fcca87ff2c2 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 8 Dec 2021 17:20:28 +0300 Subject: Edit IsRehauSku regex expression --- Source/Assistant/SkuAssist.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'Source/Assistant') diff --git a/Source/Assistant/SkuAssist.cs b/Source/Assistant/SkuAssist.cs index eddb0cd..9beb0ce 100644 --- a/Source/Assistant/SkuAssist.cs +++ b/Source/Assistant/SkuAssist.cs @@ -104,9 +104,7 @@ namespace RehauSku.Assistant } public static bool IsRehauSku(this string line) { - return Regex.IsMatch(line, @"\d{11}") && - line[0].Equals('1') && - line[7].Equals('1'); + return Regex.IsMatch(line, @"[1]\d{6}[1]\d{3}"); } } } \ No newline at end of file -- cgit v1.2.3 From bc3421e67f7fd64ff0a7a0c078bbba6fbf971905 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 8 Dec 2021 17:22:46 +0300 Subject: Edit IsRehauSku regex expression --- Source/Assistant/SkuAssist.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source/Assistant') diff --git a/Source/Assistant/SkuAssist.cs b/Source/Assistant/SkuAssist.cs index 9beb0ce..28d1503 100644 --- a/Source/Assistant/SkuAssist.cs +++ b/Source/Assistant/SkuAssist.cs @@ -104,7 +104,7 @@ namespace RehauSku.Assistant } public static bool IsRehauSku(this string line) { - return Regex.IsMatch(line, @"[1]\d{6}[1]\d{3}"); + return Regex.IsMatch(line, @"^[1]\d{6}[1]\d{3}$"); } } } \ No newline at end of file -- cgit v1.2.3 From 0e92b45e3133788b15bf0a6c46f3317315363c8b Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Thu, 9 Dec 2021 10:30:14 +0300 Subject: Fix for RAUPIANO t-pieces in request modifier --- Source/Assistant/RequestModifier.cs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'Source/Assistant') diff --git a/Source/Assistant/RequestModifier.cs b/Source/Assistant/RequestModifier.cs index 0db0b90..9f42e71 100644 --- a/Source/Assistant/RequestModifier.cs +++ b/Source/Assistant/RequestModifier.cs @@ -36,6 +36,9 @@ namespace RehauSku.Assistant int.Parse($"{match[6]}{match[7]}") }; + if (new[] { endFaces[0], endFaces[1], side }.Any(x => x == 45 || x == 90 || x == 87)) + return line; + List additions = new List(); if (endFaces.All(x => x < side)) -- cgit v1.2.3 From b7c65d64e98092049fddc1b482bfc7aa97759d60 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Fri, 10 Dec 2021 13:56:28 +0300 Subject: Refactoring --- Source/Assistant/HttpClientUtil.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source/Assistant') diff --git a/Source/Assistant/HttpClientUtil.cs b/Source/Assistant/HttpClientUtil.cs index 95a90e9..131bd7f 100644 --- a/Source/Assistant/HttpClientUtil.cs +++ b/Source/Assistant/HttpClientUtil.cs @@ -36,7 +36,7 @@ namespace RehauSku.Assistant baseUri.Path = "/catalogsearch/result/index/"; string cleanedRequest = request.CleanRequest(); - switch (AddIn.responseOrder) + switch (AddIn.StoreResponse) { case ResponseOrder.Relevance: baseUri.Query = "dir=asc&order=relevance&q=" + cleanedRequest; -- cgit v1.2.3 From e175a634cefc6e8c0ecd49514a89b1d4f30ce33b Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Mon, 13 Dec 2021 20:39:41 +0300 Subject: Refactoring. ExcelDNA.IntelliSense library add. Add description to Excel functions. --- Source/Assistant/HttpClientUtil.cs | 18 ++---- Source/Assistant/MemoryCacheExtensions.cs | 30 ++++++++++ Source/Assistant/ParseUtil.cs | 55 +++++++++++++++++ Source/Assistant/SkuAssist.cs | 98 ++----------------------------- Source/Assistant/SkuExtension.cs | 12 ++++ 5 files changed, 107 insertions(+), 106 deletions(-) create mode 100644 Source/Assistant/MemoryCacheExtensions.cs create mode 100644 Source/Assistant/ParseUtil.cs create mode 100644 Source/Assistant/SkuExtension.cs (limited to 'Source/Assistant') diff --git a/Source/Assistant/HttpClientUtil.cs b/Source/Assistant/HttpClientUtil.cs index 131bd7f..16e4287 100644 --- a/Source/Assistant/HttpClientUtil.cs +++ b/Source/Assistant/HttpClientUtil.cs @@ -1,6 +1,4 @@ -using AngleSharp; -using AngleSharp.Dom; -using System; +using System; using System.Net; using System.Net.Http; using System.Threading.Tasks; @@ -11,8 +9,10 @@ namespace RehauSku.Assistant { private static HttpClient _httpClient = AddIn.httpClient; - public async static Task GetContentByUriAsync(Uri uri) + public async static Task GetContentByRequest(string request) { + Uri uri = request.ConvertToUri(); + ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | @@ -21,15 +21,7 @@ namespace RehauSku.Assistant 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) + private static Uri ConvertToUri(this string request) { UriBuilder baseUri = new UriBuilder("https", "shop-rehau.ru"); diff --git a/Source/Assistant/MemoryCacheExtensions.cs b/Source/Assistant/MemoryCacheExtensions.cs new file mode 100644 index 0000000..7eb1408 --- /dev/null +++ b/Source/Assistant/MemoryCacheExtensions.cs @@ -0,0 +1,30 @@ +using System; +using System.Runtime.Caching; +using System.Threading.Tasks; + +namespace RehauSku.Assistant +{ + static class MemoryCacheExtensions + { + public static bool IsCached(this string request) + { + return MemoryCache.Default.Contains(request); + } + + public static IProduct GetFromCache(this string request) + { + return MemoryCache.Default[request] as IProduct; + } + + public static async Task RequestAndCache(this string request) + { + IProduct product = await SkuAssist.GetProductAsync(request); + + if (product == null) + return null; + + MemoryCache.Default.Add(request, product, DateTime.Now.AddMinutes(10)); + return product; + } + } +} \ No newline at end of file diff --git a/Source/Assistant/ParseUtil.cs b/Source/Assistant/ParseUtil.cs new file mode 100644 index 0000000..571c6b0 --- /dev/null +++ b/Source/Assistant/ParseUtil.cs @@ -0,0 +1,55 @@ +using AngleSharp; +using AngleSharp.Dom; +using Newtonsoft.Json; +using System; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace RehauSku.Assistant +{ + static class ParseUtil + { + public async static Task ContentToDocAsync(string content) + { + IConfiguration config = Configuration.Default; + IBrowsingContext context = BrowsingContext.New(config); + + return await context.OpenAsync(req => req.Content(content)); + } + + public static IProduct GetProduct(IDocument document) + { + try + { + string script = document + .Scripts + .Where(s => s.InnerHtml.Contains("dataLayer")) + .FirstOrDefault() + .InnerHtml; + + string json = script + .Substring(script.IndexOf("push(") + 5) + .TrimEnd(new[] { ')', ';', '\n', ' ' }); + + if (!json.Contains("impressions")) + return null; + + StoreResponce storeResponse = JsonConvert.DeserializeObject(json); + IProduct product = storeResponse + .Ecommerce + .Impressions + .Where(p => p.Id.IsRehauSku()) + .FirstOrDefault(); + + return product; + } + + catch (NullReferenceException e) + { + MessageBox.Show(e.Message, "Ошибка получения данных", MessageBoxButtons.OK, MessageBoxIcon.Error); + return null; + } + } + } +} \ No newline at end of file diff --git a/Source/Assistant/SkuAssist.cs b/Source/Assistant/SkuAssist.cs index 28d1503..6c68288 100644 --- a/Source/Assistant/SkuAssist.cs +++ b/Source/Assistant/SkuAssist.cs @@ -1,13 +1,4 @@ -using AngleSharp.Dom; -using ExcelDna.Integration; -using Newtonsoft.Json; -using System; -using System.Globalization; -using System.Linq; -using System.Runtime.Caching; -using System.Text.RegularExpressions; -using System.Threading.Tasks; -using System.Windows.Forms; +using System.Threading.Tasks; namespace RehauSku.Assistant { @@ -20,91 +11,12 @@ namespace RehauSku.Assistant static class SkuAssist { - public static async Task GetProduct(string request) + public static async Task GetProductAsync(string request) { - Uri uri = request.ConvertToUri(); + var content = await HttpClientUtil.GetContentByRequest(request); + var document = await ParseUtil.ContentToDocAsync(content); - Task contentTask = Task.Run(() => HttpClientUtil.GetContentByUriAsync(uri)); - Task documentTask = await contentTask.ContinueWith(content => HttpClientUtil.ContentToDocAsync(content)); - - return GetProduct(documentTask.Result); - } - public static IProduct GetProduct(IDocument document) - { - try - { - string script = document - .Scripts - .Where(s => s.InnerHtml.Contains("dataLayer")) - .FirstOrDefault() - .InnerHtml; - - string json = script - .Substring(script.IndexOf("push(") + 5) - .TrimEnd(new[] { ')', ';', '\n', ' ' }); - - if (!json.Contains("impressions")) - return null; - - StoreResponce storeResponse = JsonConvert.DeserializeObject(json); - IProduct product = storeResponse - .Ecommerce - .Impressions - .Where(p => p.Id.IsRehauSku()) - .FirstOrDefault(); - - return product; - } - - catch (NullReferenceException e) - { - MessageBox.Show(e.Message, "Ошибка получения данных", MessageBoxButtons.OK, MessageBoxIcon.Error); - return null; - } - } - 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)); - } - - switch (field) - { - case ProductField.Name: - return product.Name; - case ProductField.Id: - return product.Id; - case ProductField.Price: - return double.Parse((string)product.Price, CultureInfo.InvariantCulture); - default: - return ExcelError.ExcelErrorValue; - } - } - public static bool IsRehauSku(this string line) - { - return Regex.IsMatch(line, @"^[1]\d{6}[1]\d{3}$"); + return ParseUtil.GetProduct(document); } } } \ No newline at end of file diff --git a/Source/Assistant/SkuExtension.cs b/Source/Assistant/SkuExtension.cs new file mode 100644 index 0000000..51aaf6c --- /dev/null +++ b/Source/Assistant/SkuExtension.cs @@ -0,0 +1,12 @@ +using System.Text.RegularExpressions; + +namespace RehauSku.Assistant +{ + static class SkuExtension + { + public static bool IsRehauSku(this string line) + { + return Regex.IsMatch(line, @"^[1]\d{6}[1]\d{3}$"); + } + } +} \ No newline at end of file -- cgit v1.2.3 From aaaa6b45859e74c791f35e18c220118a54a345ad Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Fri, 17 Dec 2021 09:07:34 +0300 Subject: Create simple base form for settings window --- Source/Assistant/HttpClientUtil.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source/Assistant') diff --git a/Source/Assistant/HttpClientUtil.cs b/Source/Assistant/HttpClientUtil.cs index 16e4287..63be4a8 100644 --- a/Source/Assistant/HttpClientUtil.cs +++ b/Source/Assistant/HttpClientUtil.cs @@ -28,7 +28,7 @@ namespace RehauSku.Assistant baseUri.Path = "/catalogsearch/result/index/"; string cleanedRequest = request.CleanRequest(); - switch (AddIn.StoreResponse) + switch (AddIn.StoreResponseOrder) { case ResponseOrder.Relevance: baseUri.Query = "dir=asc&order=relevance&q=" + cleanedRequest; -- cgit v1.2.3 From f97d344f39c46b5e2f883765e8859e78007a11b0 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 22 Dec 2021 08:26:54 +0300 Subject: Add Registry Util --- Source/Assistant/HttpClientUtil.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source/Assistant') diff --git a/Source/Assistant/HttpClientUtil.cs b/Source/Assistant/HttpClientUtil.cs index 63be4a8..316ea07 100644 --- a/Source/Assistant/HttpClientUtil.cs +++ b/Source/Assistant/HttpClientUtil.cs @@ -28,7 +28,7 @@ namespace RehauSku.Assistant baseUri.Path = "/catalogsearch/result/index/"; string cleanedRequest = request.CleanRequest(); - switch (AddIn.StoreResponseOrder) + switch (RegistryUtil.StoreResponseOrder) { case ResponseOrder.Relevance: baseUri.Query = "dir=asc&order=relevance&q=" + cleanedRequest; -- cgit v1.2.3 From ce5597d042062c820288c63b4e571ee77ac23ab0 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 22 Dec 2021 17:07:37 +0300 Subject: RegistryUtil and some fixes --- Source/Assistant/ParseUtil.cs | 45 +++++++++++++++------------------------ Source/Assistant/SkuExtension.cs | 12 ----------- Source/Assistant/SkuExtensions.cs | 12 +++++++++++ 3 files changed, 29 insertions(+), 40 deletions(-) delete mode 100644 Source/Assistant/SkuExtension.cs create mode 100644 Source/Assistant/SkuExtensions.cs (limited to 'Source/Assistant') diff --git a/Source/Assistant/ParseUtil.cs b/Source/Assistant/ParseUtil.cs index 571c6b0..a93c658 100644 --- a/Source/Assistant/ParseUtil.cs +++ b/Source/Assistant/ParseUtil.cs @@ -1,10 +1,8 @@ using AngleSharp; using AngleSharp.Dom; using Newtonsoft.Json; -using System; using System.Linq; using System.Threading.Tasks; -using System.Windows.Forms; namespace RehauSku.Assistant { @@ -20,36 +18,27 @@ namespace RehauSku.Assistant public static IProduct GetProduct(IDocument document) { - try - { - string script = document - .Scripts - .Where(s => s.InnerHtml.Contains("dataLayer")) - .FirstOrDefault() - .InnerHtml; + string script = document + .Scripts + .Where(s => s.InnerHtml.Contains("dataLayer")) + .FirstOrDefault() + .InnerHtml; - string json = script - .Substring(script.IndexOf("push(") + 5) - .TrimEnd(new[] { ')', ';', '\n', ' ' }); + string json = script + .Substring(script.IndexOf("push(") + 5) + .TrimEnd(new[] { ')', ';', '\n', ' ' }); - if (!json.Contains("impressions")) - return null; - - StoreResponce storeResponse = JsonConvert.DeserializeObject(json); - IProduct product = storeResponse - .Ecommerce - .Impressions - .Where(p => p.Id.IsRehauSku()) - .FirstOrDefault(); + if (!json.Contains("impressions")) + return null; - return product; - } + StoreResponce storeResponse = JsonConvert.DeserializeObject(json); + IProduct product = storeResponse + .Ecommerce + .Impressions + .Where(p => p.Id.IsRehauSku()) + .FirstOrDefault(); - catch (NullReferenceException e) - { - MessageBox.Show(e.Message, "Ошибка получения данных", MessageBoxButtons.OK, MessageBoxIcon.Error); - return null; - } + return product; } } } \ No newline at end of file diff --git a/Source/Assistant/SkuExtension.cs b/Source/Assistant/SkuExtension.cs deleted file mode 100644 index 51aaf6c..0000000 --- a/Source/Assistant/SkuExtension.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System.Text.RegularExpressions; - -namespace RehauSku.Assistant -{ - static class SkuExtension - { - public static bool IsRehauSku(this string line) - { - return Regex.IsMatch(line, @"^[1]\d{6}[1]\d{3}$"); - } - } -} \ No newline at end of file diff --git a/Source/Assistant/SkuExtensions.cs b/Source/Assistant/SkuExtensions.cs new file mode 100644 index 0000000..e39807b --- /dev/null +++ b/Source/Assistant/SkuExtensions.cs @@ -0,0 +1,12 @@ +using System.Text.RegularExpressions; + +namespace RehauSku.Assistant +{ + static class SkuExtensions + { + public static bool IsRehauSku(this string line) + { + return Regex.IsMatch(line, @"^[1]\d{6}[1]\d{3}$"); + } + } +} \ No newline at end of file -- cgit v1.2.3 From 24024b5729c1c44bb01cb29813868743d1753e31 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Fri, 24 Dec 2021 16:22:03 +0300 Subject: MergeTool, MemoryUtil anf stuff --- Source/Assistant/MemoryCacheExtensions.cs | 30 ------------------------------ 1 file changed, 30 deletions(-) delete mode 100644 Source/Assistant/MemoryCacheExtensions.cs (limited to 'Source/Assistant') diff --git a/Source/Assistant/MemoryCacheExtensions.cs b/Source/Assistant/MemoryCacheExtensions.cs deleted file mode 100644 index 7eb1408..0000000 --- a/Source/Assistant/MemoryCacheExtensions.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System; -using System.Runtime.Caching; -using System.Threading.Tasks; - -namespace RehauSku.Assistant -{ - static class MemoryCacheExtensions - { - public static bool IsCached(this string request) - { - return MemoryCache.Default.Contains(request); - } - - public static IProduct GetFromCache(this string request) - { - return MemoryCache.Default[request] as IProduct; - } - - public static async Task RequestAndCache(this string request) - { - IProduct product = await SkuAssist.GetProductAsync(request); - - if (product == null) - return null; - - MemoryCache.Default.Add(request, product, DateTime.Now.AddMinutes(10)); - return product; - } - } -} \ No newline at end of file -- cgit v1.2.3