RhSolutions-AddIn/Source/Assistant/SkuAssist.cs

112 lines
3.5 KiB
C#
Raw Normal View History

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;
using System.Text.RegularExpressions;
2021-12-03 19:30:35 +03:00
using System.Threading.Tasks;
2021-12-07 08:48:35 +03:00
using System.Windows.Forms;
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
{
public static async Task<IProduct> GetProduct(string request)
2021-11-29 21:24:44 +03:00
{
Uri uri = request.ConvertToUri();
2021-11-29 21:24:44 +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-07 08:48:35 +03:00
try
{
string script = document
.Scripts
.Where(s => s.InnerHtml.Contains("dataLayer"))
.FirstOrDefault()
.InnerHtml;
2021-11-29 21:24:44 +03:00
2021-12-07 08:48:35 +03:00
string json = script
.Substring(script.IndexOf("push(") + 5)
.TrimEnd(new[] { ')', ';', '\n', ' ' });
2021-12-07 08:48:35 +03:00
if (!json.Contains("impressions"))
return null;
2021-12-05 15:35:43 +03:00
2021-12-07 08:48:35 +03:00
StoreResponce storeResponse = JsonConvert.DeserializeObject<StoreResponce>(json);
IProduct product = storeResponse
.Ecommerce
.Impressions
.Where(p => p.Id.IsRehauSku())
.FirstOrDefault();
return product;
}
2021-12-03 19:30:35 +03:00
2021-12-07 08:48:35 +03:00
catch (NullReferenceException e)
{
MessageBox.Show(e.Message, "Ошибка получения данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
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-12-06 19:50:00 +03:00
public static bool IsRehauSku(this string line)
{
return Regex.IsMatch(line, @"\d{11}") &&
line[0].Equals('1') &&
line[7].Equals('1');
}
2021-11-29 21:24:44 +03:00
}
}