aboutsummaryrefslogtreecommitdiff
path: root/src/Assistant
diff options
context:
space:
mode:
authorSerghei Cebotari <51533848+schebotar@users.noreply.github.com>2021-12-26 19:37:46 +0300
committerGitHub <noreply@github.com>2021-12-26 19:37:46 +0300
commitfc870d19555cc77b0e08912b4dab21d496b2321f (patch)
treeac8b9aa1e883a85339a594b2797ab319cca73c4e /src/Assistant
parent0525ec1b42d5857b740dd0dae7f6d9baeb7088d1 (diff)
parent54fc3320e7d64d7903b4d091fe0d5c15df01fd78 (diff)
Merge pull request #9 from schebotar/dev
Move to /src
Diffstat (limited to 'src/Assistant')
-rw-r--r--src/Assistant/HttpClientUtil.cs53
-rw-r--r--src/Assistant/IProduct.cs9
-rw-r--r--src/Assistant/ParseUtil.cs44
-rw-r--r--src/Assistant/RequestModifier.cs67
-rw-r--r--src/Assistant/SkuAssist.cs22
-rw-r--r--src/Assistant/SkuExtensions.cs12
-rw-r--r--src/Assistant/StoreResponse.cs21
7 files changed, 228 insertions, 0 deletions
diff --git a/src/Assistant/HttpClientUtil.cs b/src/Assistant/HttpClientUtil.cs
new file mode 100644
index 0000000..316ea07
--- /dev/null
+++ b/src/Assistant/HttpClientUtil.cs
@@ -0,0 +1,53 @@
+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<string> 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();
+
+ switch (RegistryUtil.StoreResponseOrder)
+ {
+ 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;
+ default:
+ baseUri.Query = "q=" + cleanedRequest;
+ break;
+ }
+
+ return baseUri.Uri;
+ }
+ }
+} \ No newline at end of file
diff --git a/src/Assistant/IProduct.cs b/src/Assistant/IProduct.cs
new file mode 100644
index 0000000..9494eeb
--- /dev/null
+++ b/src/Assistant/IProduct.cs
@@ -0,0 +1,9 @@
+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
new file mode 100644
index 0000000..a93c658
--- /dev/null
+++ b/src/Assistant/ParseUtil.cs
@@ -0,0 +1,44 @@
+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<IDocument> 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<StoreResponce>(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
new file mode 100644
index 0000000..9f42e71
--- /dev/null
+++ b/src/Assistant/RequestModifier.cs
@@ -0,0 +1,67 @@
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Text.RegularExpressions;
+
+namespace RehauSku.Assistant
+{
+ 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._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<string> additions = new List<string>();
+
+ 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
new file mode 100644
index 0000000..6c68288
--- /dev/null
+++ b/src/Assistant/SkuAssist.cs
@@ -0,0 +1,22 @@
+using System.Threading.Tasks;
+
+namespace RehauSku.Assistant
+{
+ public enum ProductField
+ {
+ Name,
+ Id,
+ Price
+ }
+
+ static class SkuAssist
+ {
+ public static async Task<IProduct> 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/SkuExtensions.cs b/src/Assistant/SkuExtensions.cs
new file mode 100644
index 0000000..e39807b
--- /dev/null
+++ b/src/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
diff --git a/src/Assistant/StoreResponse.cs b/src/Assistant/StoreResponse.cs
new file mode 100644
index 0000000..8e1759d
--- /dev/null
+++ b/src/Assistant/StoreResponse.cs
@@ -0,0 +1,21 @@
+using System.Collections.Generic;
+
+namespace RehauSku.Assistant
+{
+ public class StoreResponce
+ {
+ public Ecommerce Ecommerce { get; set; }
+ }
+
+ public class Ecommerce
+ {
+ public List<Product> 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