diff options
author | Sergey Chebotar <s.chebotar@gmail.com> | 2021-12-13 20:39:41 +0300 |
---|---|---|
committer | Sergey Chebotar <s.chebotar@gmail.com> | 2021-12-13 20:39:41 +0300 |
commit | e175a634cefc6e8c0ecd49514a89b1d4f30ce33b (patch) | |
tree | e4265926b772d31e2eff58d52ee3e1ec159af2a8 /Source/Assistant/ParseUtil.cs | |
parent | b7c65d64e98092049fddc1b482bfc7aa97759d60 (diff) |
Refactoring. ExcelDNA.IntelliSense library add.
Add description to Excel functions.
Diffstat (limited to 'Source/Assistant/ParseUtil.cs')
-rw-r--r-- | Source/Assistant/ParseUtil.cs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/Source/Assistant/ParseUtil.cs b/Source/Assistant/ParseUtil.cs new file mode 100644 index 0000000..571c6b0 --- /dev/null +++ b/Source/Assistant/ParseUtil.cs @@ -0,0 +1,55 @@ +using AngleSharp; +using AngleSharp.Dom; +using Newtonsoft.Json; +using System; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; + +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) + { + try + { + 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; + } + + catch (NullReferenceException e) + { + MessageBox.Show(e.Message, "Ошибка получения данных", MessageBoxButtons.OK, MessageBoxIcon.Error); + return null; + } + } + } +}
\ No newline at end of file |