2021-12-13 20:39:41 +03:00
|
|
|
|
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)
|
|
|
|
|
{
|
2021-12-22 17:07:37 +03:00
|
|
|
|
string script = document
|
|
|
|
|
.Scripts
|
|
|
|
|
.Where(s => s.InnerHtml.Contains("dataLayer"))
|
|
|
|
|
.FirstOrDefault()
|
|
|
|
|
.InnerHtml;
|
2021-12-13 20:39:41 +03:00
|
|
|
|
|
2021-12-22 17:07:37 +03:00
|
|
|
|
string json = script
|
|
|
|
|
.Substring(script.IndexOf("push(") + 5)
|
|
|
|
|
.TrimEnd(new[] { ')', ';', '\n', ' ' });
|
2021-12-13 20:39:41 +03:00
|
|
|
|
|
2021-12-22 17:07:37 +03:00
|
|
|
|
if (!json.Contains("impressions"))
|
|
|
|
|
return null;
|
2021-12-13 20:39:41 +03:00
|
|
|
|
|
2021-12-22 17:07:37 +03:00
|
|
|
|
StoreResponce storeResponse = JsonConvert.DeserializeObject<StoreResponce>(json);
|
|
|
|
|
IProduct product = storeResponse
|
|
|
|
|
.Ecommerce
|
|
|
|
|
.Impressions
|
|
|
|
|
.Where(p => p.Id.IsRehauSku())
|
|
|
|
|
.FirstOrDefault();
|
2021-12-13 20:39:41 +03:00
|
|
|
|
|
2021-12-22 17:07:37 +03:00
|
|
|
|
return product;
|
2021-12-13 20:39:41 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|