2021-12-03 12:57:22 +03:00
|
|
|
|
using AngleSharp.Dom;
|
2021-12-03 19:30:35 +03:00
|
|
|
|
using Newtonsoft.Json;
|
2021-11-29 21:24:44 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
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 12:57:22 +03:00
|
|
|
|
public enum ResponseOrder
|
|
|
|
|
{
|
|
|
|
|
NoSettings,
|
|
|
|
|
Relevance,
|
|
|
|
|
Name,
|
|
|
|
|
Price,
|
|
|
|
|
Series
|
|
|
|
|
}
|
|
|
|
|
|
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-03 12:57:22 +03:00
|
|
|
|
Uri uri = request.ConvertToUri(ResponseOrder.NoSettings);
|
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-03 19:30:35 +03:00
|
|
|
|
public static IProduct GetProduct(IDocument d)
|
2021-11-29 21:24:44 +03:00
|
|
|
|
{
|
2021-12-03 19:30:35 +03:00
|
|
|
|
string script = d.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-03 19:30:35 +03:00
|
|
|
|
StoreResponce storeResponse = JsonConvert.DeserializeObject<StoreResponce>(json);
|
|
|
|
|
IProduct product = storeResponse
|
|
|
|
|
.Ecommerce
|
|
|
|
|
.Impressions
|
|
|
|
|
.Where(i => Regex.IsMatch(i.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 12:57:22 +03:00
|
|
|
|
}
|