diff --git a/src/AddIn/AddIn.cs b/src/AddIn/AddIn.cs index 035e50f..1606624 100644 --- a/src/AddIn/AddIn.cs +++ b/src/AddIn/AddIn.cs @@ -9,14 +9,10 @@ namespace RehauSku { class AddIn : IExcelAddIn { - public static HttpClient httpClient; - public static MemoryCache memoryCache; public static Application Excel; public void AutoOpen() { - httpClient = new HttpClient(); - memoryCache = new MemoryCache("RehauSku"); Excel = (Application)ExcelDnaUtil.Application; RegisterFunctions(); IntelliSenseServer.Install(); @@ -29,7 +25,6 @@ namespace RehauSku IntelliSenseServer.Uninstall(); RegistryUtil.Uninitialize(); EventsUtil.Uninitialize(); - memoryCache.Dispose(); } void RegisterFunctions() diff --git a/src/AddIn/Functions.cs b/src/AddIn/Functions.cs index efdec66..c202a3b 100644 --- a/src/AddIn/Functions.cs +++ b/src/AddIn/Functions.cs @@ -1,58 +1,9 @@ using ExcelDna.Integration; -using RehauSku.Assistant; namespace RehauSku { public class Functions { - [ExcelFunction(description: "Получение названия первого продукта в поиске")] - public static object RAUNAME([ExcelArgument(Name = "\"Запрос\"", Description = "в свободной форме или ячейка с запросом")] string request) - => MakeRequest(request, ProductField.Name); - - [ExcelFunction(Description = "Получение артикула первого продукта в поиске")] - public static object RAUSKU([ExcelArgument(Name = "\"Запрос\"", Description = "в свободной форме или ячейка с запросом")] string request) - => MakeRequest(request, ProductField.Id); - - [ExcelFunction(Description = "Получение цены первого продукта в поиске")] - public static object RAUPRICE([ExcelArgument(Name = "\"Запрос\"", Description = "в свободной форме или ячейка с запросом")] string request) - => MakeRequest(request, ProductField.Price); - - private static object MakeRequest(string request, ProductField field) - { - object result; - - if (request.IsCached()) - result = request.GetFromCache(); - - else - { - result = ExcelAsyncUtil.Run("Request", request, delegate - { - return request.RequestAndCache().GetAwaiter().GetResult(); - }); - } - - if (result == null) - return "Не найдено :("; - - if (result.Equals(ExcelError.ExcelErrorNA)) - return "Загрузка..."; - - IProduct product = result as IProduct; - - switch (field) - { - case ProductField.Name: - return product.Name; - case ProductField.Id: - return product.Id; - case ProductField.Price: - return double.Parse(product.Price, System.Globalization.CultureInfo.InvariantCulture); - default: - return null; - } - } - [ExcelFunction(Description = "Получение корректного артикула из строки")] public static object GETRAUSKU([ExcelArgument(Name = "\"Строка\"", Description = "строка, содержащая актикул")] string line) { diff --git a/src/AddIn/MemoryCacheUtil.cs b/src/AddIn/MemoryCacheUtil.cs deleted file mode 100644 index 1d42e14..0000000 --- a/src/AddIn/MemoryCacheUtil.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System; -using System.Runtime.Caching; -using System.Threading.Tasks; -using RehauSku.Assistant; - -namespace RehauSku -{ - static class MemoryCacheUtil - { - public static bool IsCached(this string request) - { - return AddIn.memoryCache.Contains(request); - } - - public static IProduct GetFromCache(this string request) - { - return AddIn.memoryCache[request] as IProduct; - } - - public static async Task RequestAndCache(this string request) - { - IProduct product = await SkuAssist.GetProductAsync(request); - - if (product == null) - return null; - - AddIn.memoryCache.Add(request, product, DateTime.Now.AddMinutes(10)); - return product; - } - - public static void ClearCache() - { - AddIn.memoryCache.Dispose(); - AddIn.memoryCache = new MemoryCache("RehauSku"); - } - } -} \ No newline at end of file diff --git a/src/Assistant/HttpClientUtil.cs b/src/Assistant/HttpClientUtil.cs deleted file mode 100644 index a5a961e..0000000 --- a/src/Assistant/HttpClientUtil.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using System.Net; -using System.Net.Http; -using System.Threading.Tasks; - -namespace RehauSku.Assistant -{ - static class HttpClientUtil - { - private static HttpClient _httpClient = AddIn.httpClient; - - public async static Task GetContentByRequest(string request) - { - Uri uri = request.ConvertToUri(); - - ServicePointManager.SecurityProtocol = - SecurityProtocolType.Tls12 | - SecurityProtocolType.Tls11 | - SecurityProtocolType.Tls; - - return await _httpClient.GetStringAsync(uri); - } - - private static Uri ConvertToUri(this string request) - { - UriBuilder baseUri = new UriBuilder("https", "shop-rehau.ru"); - - baseUri.Path = "/catalogsearch/result/index/"; - string cleanedRequest = request.CleanRequest(); - baseUri.Query = "q=" + cleanedRequest; - - return baseUri.Uri; - } - } -} \ No newline at end of file diff --git a/src/Assistant/IProduct.cs b/src/Assistant/IProduct.cs deleted file mode 100644 index 9494eeb..0000000 --- a/src/Assistant/IProduct.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace RehauSku.Assistant -{ - interface IProduct - { - string Id { get; } - string Name { get; } - string Price { get; } - } -} diff --git a/src/Assistant/ParseUtil.cs b/src/Assistant/ParseUtil.cs deleted file mode 100644 index a93c658..0000000 --- a/src/Assistant/ParseUtil.cs +++ /dev/null @@ -1,44 +0,0 @@ -using AngleSharp; -using AngleSharp.Dom; -using Newtonsoft.Json; -using System.Linq; -using System.Threading.Tasks; - -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) - { - 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; - } - } -} \ No newline at end of file diff --git a/src/Assistant/RequestModifier.cs b/src/Assistant/RequestModifier.cs deleted file mode 100644 index c2c3436..0000000 --- a/src/Assistant/RequestModifier.cs +++ /dev/null @@ -1,67 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Text.RegularExpressions; - -namespace RehauSku.Assistant -{ - static class RequestModifier - { - public static string CleanRequest(this string input) - { - string replace = new StringBuilder(input) - .Replace("+", " plus ") - .Replace("РХ", "") - .Replace("º", " ") - .Replace(".", " ") - .Replace("Ø", " ") - .ToString(); - - return replace._tPieceNormalize(); - } - - private static string _tPieceNormalize(this string line) - { - Regex regex = new Regex(@"\d{2}.\d{2}.\d{2}"); - - if (!regex.IsMatch(line)) - return line; - - string match = regex.Match(line).Value; - - int side = int.Parse($"{match[3]}{match[4]}"); - int[] endFaces = new int[] - { - int.Parse($"{match[0]}{match[1]}"), - 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)) - additions.Add("увеличенный боковой"); - - else - { - if (new[] { endFaces[0], endFaces[1], side }.Distinct().Count() == 1) - additions.Add("равнопроходной"); - else - additions.Add("уменьшенный"); - - if (endFaces.Any(x => x > side)) - additions.Add("боковой"); - - if (endFaces[0] != endFaces[1]) - additions.Add("торцевой"); - } - - string piece = $" {endFaces.Max()}-{side}-{endFaces.Min()} "; - string modifiedMatch = string.Join(" ", additions) + piece; - - return line.Replace(match, modifiedMatch); - } - } -} \ No newline at end of file diff --git a/src/Assistant/SkuAssist.cs b/src/Assistant/SkuAssist.cs deleted file mode 100644 index 85a084c..0000000 --- a/src/Assistant/SkuAssist.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System.Threading.Tasks; - -namespace RehauSku.Assistant -{ - enum ProductField - { - Name, - Id, - Price - } - - static class SkuAssist - { - public static async Task GetProductAsync(string request) - { - var content = await HttpClientUtil.GetContentByRequest(request); - var document = await ParseUtil.ContentToDocAsync(content); - - return ParseUtil.GetProduct(document); - } - } -} \ No newline at end of file diff --git a/src/Assistant/StoreResponse.cs b/src/Assistant/StoreResponse.cs deleted file mode 100644 index 1a9b1c5..0000000 --- a/src/Assistant/StoreResponse.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System.Collections.Generic; - -namespace RehauSku.Assistant -{ - class StoreResponce - { - public Ecommerce Ecommerce { get; set; } - } - - class Ecommerce - { - public List Impressions { get; set; } - } - - 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/src/RehauSku.Assist-AddIn.dna b/src/RehauSku.Assist-AddIn.dna index 1bef6e1..0b173b3 100644 --- a/src/RehauSku.Assist-AddIn.dna +++ b/src/RehauSku.Assist-AddIn.dna @@ -1,13 +1,6 @@ - - - - - - - \ No newline at end of file diff --git a/src/RehauSku.Assist.csproj b/src/RehauSku.Assist.csproj index 16ae27e..4fe5613 100644 --- a/src/RehauSku.Assist.csproj +++ b/src/RehauSku.Assist.csproj @@ -34,76 +34,37 @@ 4 - - packages\AngleSharp.0.16.1\lib\net472\AngleSharp.dll - True - - packages\ExcelDna.Integration.1.5.1\lib\net452\ExcelDna.Integration.dll - True + ..\packages\ExcelDna.Integration.1.5.1\lib\net452\ExcelDna.Integration.dll - packages\ExcelDna.IntelliSense.1.5.1\lib\net452\ExcelDna.IntelliSense.dll + ..\packages\ExcelDna.IntelliSense.1.5.1\lib\net452\ExcelDna.IntelliSense.dll - packages\ExcelDna.Registration.1.5.1\lib\net452\ExcelDna.Registration.dll - True + ..\packages\ExcelDna.Registration.1.5.1\lib\net452\ExcelDna.Registration.dll - packages\ExcelDna.Interop.14.0.1\lib\Microsoft.Office.Interop.Excel.dll + ..\packages\ExcelDna.Interop.14.0.1\lib\Microsoft.Office.Interop.Excel.dll True - True - packages\ExcelDna.Interop.14.0.1\lib\Microsoft.Vbe.Interop.dll + ..\packages\ExcelDna.Interop.14.0.1\lib\Microsoft.Vbe.Interop.dll True - - packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll - - packages\ExcelDna.Interop.14.0.1\lib\Office.dll + ..\packages\ExcelDna.Interop.14.0.1\lib\Office.dll True - - packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll - - - packages\System.Configuration.ConfigurationManager.6.0.0\lib\net461\System.Configuration.ConfigurationManager.dll - - - packages\System.Memory.4.5.4\lib\net461\System.Memory.dll - - - packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll - - - packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll - True - - - packages\System.Security.AccessControl.6.0.0\lib\net461\System.Security.AccessControl.dll - - - packages\System.Security.Permissions.6.0.0\lib\net461\System.Security.Permissions.dll - - - packages\System.Security.Principal.Windows.5.0.0\lib\net461\System.Security.Principal.Windows.dll - - - packages\System.Text.Encoding.CodePages.6.0.0\lib\net461\System.Text.Encoding.CodePages.dll - True - @@ -120,9 +81,6 @@ - - - @@ -136,15 +94,11 @@ - - - - @@ -155,10 +109,11 @@ + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - + \ No newline at end of file diff --git a/src/packages.config b/src/packages.config index b92606b..95e3f63 100644 --- a/src/packages.config +++ b/src/packages.config @@ -1,20 +1,8 @@  - - - - - - - - - - - - \ No newline at end of file