aboutsummaryrefslogtreecommitdiff
path: root/src/Assistant/ParseUtil.cs
diff options
context:
space:
mode:
authorSergey Chebotar <s.chebotar@gmail.com>2021-12-26 18:22:32 +0300
committerSergey Chebotar <s.chebotar@gmail.com>2021-12-26 18:22:32 +0300
commit54fc3320e7d64d7903b4d091fe0d5c15df01fd78 (patch)
treeac8b9aa1e883a85339a594b2797ab319cca73c4e /src/Assistant/ParseUtil.cs
parent20cfbfcca3a779c04aecdca5e4b465651e2be42a (diff)
Move to /src
Diffstat (limited to 'src/Assistant/ParseUtil.cs')
-rw-r--r--src/Assistant/ParseUtil.cs44
1 files changed, 44 insertions, 0 deletions
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