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