2021-12-03 12:57:22 +03:00
|
|
|
|
using AngleSharp.Dom;
|
2021-12-03 22:25:20 +03:00
|
|
|
|
using ExcelDna.Integration;
|
2021-12-03 19:30:35 +03:00
|
|
|
|
using Newtonsoft.Json;
|
2021-11-29 21:24:44 +03:00
|
|
|
|
using System;
|
2021-12-05 16:32:25 +03:00
|
|
|
|
using System.Globalization;
|
2021-11-29 21:24:44 +03:00
|
|
|
|
using System.Linq;
|
2021-12-03 22:25:20 +03:00
|
|
|
|
using System.Runtime.Caching;
|
2021-12-03 12:57:22 +03:00
|
|
|
|
using System.Text.RegularExpressions;
|
2021-12-03 19:30:35 +03:00
|
|
|
|
using System.Threading.Tasks;
|
2021-11-29 21:24:44 +03:00
|
|
|
|
|
|
|
|
|
namespace Rehau.Sku.Assist
|
|
|
|
|
{
|
2021-12-03 22:25:20 +03:00
|
|
|
|
public enum ProductField
|
|
|
|
|
{
|
|
|
|
|
Name,
|
|
|
|
|
Id,
|
|
|
|
|
Price
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-29 21:24:44 +03:00
|
|
|
|
static class SkuAssist
|
|
|
|
|
{
|
2021-12-03 12:57:22 +03:00
|
|
|
|
public static async Task<IProduct> GetProduct(string request)
|
2021-11-29 21:24:44 +03:00
|
|
|
|
{
|
2021-12-05 21:19:28 +03:00
|
|
|
|
Uri uri = request.ConvertToUri();
|
2021-11-29 21:24:44 +03:00
|
|
|
|
|
2021-12-03 12:57:22 +03:00
|
|
|
|
Task<string> contentTask = Task.Run(() => HttpClientUtil.GetContentByUriAsync(uri));
|
|
|
|
|
Task<IDocument> documentTask = await contentTask.ContinueWith(content => HttpClientUtil.ContentToDocAsync(content));
|
2021-11-29 21:24:44 +03:00
|
|
|
|
|
2021-12-03 19:30:35 +03:00
|
|
|
|
return GetProduct(documentTask.Result);
|
2021-11-29 21:24:44 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-05 21:25:30 +03:00
|
|
|
|
public static IProduct GetProduct(IDocument document)
|
2021-11-29 21:24:44 +03:00
|
|
|
|
{
|
2021-12-05 21:25:30 +03:00
|
|
|
|
string script = document
|
|
|
|
|
.Scripts
|
|
|
|
|
.Where(s => s.InnerHtml.Contains("dataLayer"))
|
|
|
|
|
.First()
|
|
|
|
|
.InnerHtml;
|
2021-11-29 21:24:44 +03:00
|
|
|
|
|
2021-12-03 19:30:35 +03:00
|
|
|
|
string json = script
|
|
|
|
|
.Substring(script.IndexOf("push(") + 5)
|
|
|
|
|
.TrimEnd(new[] { ')', ';', '\n', ' ' });
|
2021-12-03 12:57:22 +03:00
|
|
|
|
|
2021-12-05 15:35:43 +03:00
|
|
|
|
if (!json.Contains("impressions"))
|
|
|
|
|
return null;
|
|
|
|
|
|
2021-12-03 19:30:35 +03:00
|
|
|
|
StoreResponce storeResponse = JsonConvert.DeserializeObject<StoreResponce>(json);
|
|
|
|
|
IProduct product = storeResponse
|
|
|
|
|
.Ecommerce
|
|
|
|
|
.Impressions
|
2021-12-03 22:25:20 +03:00
|
|
|
|
.Where(p => Regex.IsMatch(p.Id, @"\d{11}", RegexOptions.None))
|
2021-12-03 12:57:22 +03:00
|
|
|
|
.FirstOrDefault();
|
2021-12-03 19:30:35 +03:00
|
|
|
|
|
|
|
|
|
return product;
|
2021-11-29 21:24:44 +03:00
|
|
|
|
}
|
2021-12-03 22:25:20 +03:00
|
|
|
|
|
|
|
|
|
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)
|
2021-12-05 15:40:54 +03:00
|
|
|
|
return "Не найдено :(";
|
2021-12-03 22:25:20 +03:00
|
|
|
|
|
|
|
|
|
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:
|
2021-12-05 16:32:25 +03:00
|
|
|
|
return double.Parse((string)product.Price, CultureInfo.InvariantCulture);
|
2021-12-03 22:25:20 +03:00
|
|
|
|
default:
|
|
|
|
|
return ExcelError.ExcelErrorValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-29 21:24:44 +03:00
|
|
|
|
}
|
2021-12-03 12:57:22 +03:00
|
|
|
|
}
|