diff options
author | Sergey Chebotar <s.chebotar@gmail.com> | 2021-12-13 20:39:41 +0300 |
---|---|---|
committer | Sergey Chebotar <s.chebotar@gmail.com> | 2021-12-13 20:39:41 +0300 |
commit | e175a634cefc6e8c0ecd49514a89b1d4f30ce33b (patch) | |
tree | e4265926b772d31e2eff58d52ee3e1ec159af2a8 /Source/Assistant/SkuAssist.cs | |
parent | b7c65d64e98092049fddc1b482bfc7aa97759d60 (diff) |
Refactoring. ExcelDNA.IntelliSense library add.
Add description to Excel functions.
Diffstat (limited to 'Source/Assistant/SkuAssist.cs')
-rw-r--r-- | Source/Assistant/SkuAssist.cs | 98 |
1 files changed, 5 insertions, 93 deletions
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<IProduct> GetProduct(string request) + public static async Task<IProduct> GetProductAsync(string request) { - Uri uri = request.ConvertToUri(); + var content = await HttpClientUtil.GetContentByRequest(request); + var document = await ParseUtil.ContentToDocAsync(content); - Task<string> contentTask = Task.Run(() => HttpClientUtil.GetContentByUriAsync(uri)); - Task<IDocument> 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<StoreResponce>(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<IProduct> 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 |