diff --git a/Rehau.Sku.Assist-AddIn-packed.xll b/Rehau.Sku.Assist-AddIn-packed.xll
deleted file mode 100644
index aea355f..0000000
Binary files a/Rehau.Sku.Assist-AddIn-packed.xll and /dev/null differ
diff --git a/Rehau.Sku.Assist.csproj b/Rehau.Sku.Assist.csproj
index 5b7438a..87bd1e0 100644
--- a/Rehau.Sku.Assist.csproj
+++ b/Rehau.Sku.Assist.csproj
@@ -1,5 +1,7 @@
+
+
Debug
@@ -46,6 +48,12 @@
packages\ExcelDna.Registration.1.5.0\lib\net452\ExcelDna.Registration.dll
True
+
+ packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll
+
+
+ packages\NUnit.3.13.2\lib\net45\nunit.framework.dll
+
packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll
@@ -80,12 +88,14 @@
-
-
-
-
+
+
+
+
+
-
+
+
@@ -100,5 +110,7 @@
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/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/src/Assistant/IProduct.cs b/Source/Assistant/IProduct.cs
similarity index 63%
rename from src/Assistant/IProduct.cs
rename to Source/Assistant/IProduct.cs
index aca3ff5..d5db286 100644
--- a/src/Assistant/IProduct.cs
+++ b/Source/Assistant/IProduct.cs
@@ -2,8 +2,8 @@
{
interface IProduct
{
- string Sku { get; }
+ string Id { get; }
string Name { get; }
- string Uri { get; }
+ string Price { get; }
}
}
diff --git a/Source/Assistant/SkuAssist.cs b/Source/Assistant/SkuAssist.cs
new file mode 100644
index 0000000..69c1a81
--- /dev/null
+++ b/Source/Assistant/SkuAssist.cs
@@ -0,0 +1,102 @@
+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;
+
+namespace Rehau.Sku.Assist
+{
+ public enum ResponseOrder
+ {
+ NoSettings,
+ Relevance,
+ Name,
+ Price,
+ Series
+ }
+
+ public enum ProductField
+ {
+ Name,
+ Id,
+ Price
+ }
+
+ static class SkuAssist
+ {
+ public static async Task GetProduct(string request)
+ {
+ Uri uri = request.ConvertToUri(ResponseOrder.NoSettings);
+
+ 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 d)
+ {
+ string script = d.Scripts
+ .Where(s => s.InnerHtml.Contains("dataLayer"))
+ .First()
+ .InnerHtml;
+
+ string json = script
+ .Substring(script.IndexOf("push(") + 5)
+ .TrimEnd(new[] { ')', ';', '\n', ' ' });
+
+ StoreResponce storeResponse = JsonConvert.DeserializeObject(json);
+ IProduct product = storeResponse
+ .Ecommerce
+ .Impressions
+ .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));
+ }
+
+ 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
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
diff --git a/src/ExcelDNA/AddIn.cs b/Source/ExcelDNA/AddIn.cs
similarity index 82%
rename from src/ExcelDNA/AddIn.cs
rename to Source/ExcelDNA/AddIn.cs
index dd99667..0505e5b 100644
--- a/src/ExcelDNA/AddIn.cs
+++ b/Source/ExcelDNA/AddIn.cs
@@ -1,13 +1,17 @@
using ExcelDna.Integration;
using ExcelDna.Registration;
+using System.Net.Http;
namespace Rehau.Sku.Assist
{
public class AddIn : IExcelAddIn
{
+ public static HttpClient httpClient;
+
public void AutoOpen()
{
RegisterFunctions();
+ httpClient = new HttpClient();
}
public void AutoClose()
diff --git a/Source/ExcelDNA/Functions.cs b/Source/ExcelDNA/Functions.cs
new file mode 100644
index 0000000..a9bdfca
--- /dev/null
+++ b/Source/ExcelDNA/Functions.cs
@@ -0,0 +1,19 @@
+using ExcelDna.Integration;
+
+namespace Rehau.Sku.Assist
+{
+ public class Functions
+ {
+ [ExcelFunction]
+ public static object RAUNAME(string request)
+ => SkuAssist.GetProduct(request, ProductField.Name);
+
+ [ExcelFunction]
+ public static object RAUSKU(string request)
+ => SkuAssist.GetProduct(request, ProductField.Id);
+
+ [ExcelFunction]
+ public static object RAUPRICE(string request)
+ => SkuAssist.GetProduct(request, ProductField.Price);
+ }
+}
\ No newline at end of file
diff --git a/Tests/SkuAssistTests.cs b/Tests/SkuAssistTests.cs
new file mode 100644
index 0000000..19f8a0c
--- /dev/null
+++ b/Tests/SkuAssistTests.cs
@@ -0,0 +1,15 @@
+using NUnit.Framework;
+
+namespace Rehau.Sku.Assist.Tests
+{
+ [TestFixture]
+ public class SkuAssistTests
+ {
+ [Test]
+ public static void BaseTest()
+ {
+ var result = Functions.RAUNAME("160001");
+ Assert.AreEqual("Надвижная гильза REHAU RAUTITAN РХ (11600011001)", result);
+ }
+ }
+}
diff --git a/packages.config b/packages.config
index cc383d8..b42dcc6 100644
--- a/packages.config
+++ b/packages.config
@@ -4,6 +4,9 @@
+
+
+
diff --git a/src/Assistant/Product.cs b/src/Assistant/Product.cs
deleted file mode 100644
index 17a7065..0000000
--- a/src/Assistant/Product.cs
+++ /dev/null
@@ -1,21 +0,0 @@
-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/src/Assistant/SkuAssist.cs b/src/Assistant/SkuAssist.cs
deleted file mode 100644
index dc36dc0..0000000
--- a/src/Assistant/SkuAssist.cs
+++ /dev/null
@@ -1,45 +0,0 @@
-using AngleSharp;
-using AngleSharp.Dom;
-using System.Linq;
-using System.Net;
-using System.Net.Http;
-using System.Text.RegularExpressions;
-using System.Threading.Tasks;
-
-namespace Rehau.Sku.Assist
-{
- static class SkuAssist
- {
- public async static Task GetContent(string request, HttpClient httpClient)
- {
- string uri = "https://shop-rehau.ru/catalogsearch/result/?q=" + request._CleanRequest();
- 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 string _CleanRequest(this string input)
- {
- return input.Replace("+", " plus ");
- }
- }
-}
-
-
diff --git a/src/ExcelDNA/Functions.cs b/src/ExcelDNA/Functions.cs
deleted file mode 100644
index ec9c607..0000000
--- a/src/ExcelDNA/Functions.cs
+++ /dev/null
@@ -1,21 +0,0 @@
-using AngleSharp.Dom;
-using ExcelDna.Integration;
-using System.Net.Http;
-using System.Threading.Tasks;
-
-namespace Rehau.Sku.Assist
-{
- public class Functions
- {
- private static HttpClient _httpClient = new HttpClient();
-
- [ExcelFunction]
- public static async Task RAUNAME(string request)
- {
- Task contentTask = Task.Run(() => SkuAssist.GetContent(request, _httpClient));
- Task documentTask = await contentTask.ContinueWith(content => SkuAssist.GetDocument(content));
- IProduct product = await documentTask.ContinueWith(doc => SkuAssist.GetProductFromDocument(doc.Result));
- return product == null ? ExcelError.ExcelErrorNull.ToString() : product.ToString();
- }
- }
-}
\ No newline at end of file