Add Json parsing and refactoring
This commit is contained in:
parent
c748be35c4
commit
915929fa9d
@ -48,6 +48,9 @@
|
|||||||
<HintPath>packages\ExcelDna.Registration.1.5.0\lib\net452\ExcelDna.Registration.dll</HintPath>
|
<HintPath>packages\ExcelDna.Registration.1.5.0\lib\net452\ExcelDna.Registration.dll</HintPath>
|
||||||
<Private>True</Private>
|
<Private>True</Private>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||||
|
<HintPath>packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
<Reference Include="nunit.framework, Version=3.13.2.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
|
<Reference Include="nunit.framework, Version=3.13.2.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
|
||||||
<HintPath>packages\NUnit.3.13.2\lib\net45\nunit.framework.dll</HintPath>
|
<HintPath>packages\NUnit.3.13.2\lib\net45\nunit.framework.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
@ -86,9 +89,9 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Source\Assistant\HttpClientUtil.cs" />
|
<Compile Include="Source\Assistant\HttpClientUtil.cs" />
|
||||||
|
<Compile Include="Source\Assistant\StoreResponse.cs" />
|
||||||
<Compile Include="Source\ExcelDNA\AddIn.cs" />
|
<Compile Include="Source\ExcelDNA\AddIn.cs" />
|
||||||
<Compile Include="Source\Assistant\IProduct.cs" />
|
<Compile Include="Source\Assistant\IProduct.cs" />
|
||||||
<Compile Include="Source\Assistant\Product.cs" />
|
|
||||||
<Compile Include="Source\ExcelDNA\Functions.cs" />
|
<Compile Include="Source\ExcelDNA\Functions.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="Source\Assistant\SkuAssist.cs" />
|
<Compile Include="Source\Assistant\SkuAssist.cs" />
|
||||||
|
@ -4,8 +4,7 @@ namespace Rehau.Sku.Assist
|
|||||||
{
|
{
|
||||||
interface IProduct
|
interface IProduct
|
||||||
{
|
{
|
||||||
string Sku { get; }
|
string Id { get; }
|
||||||
string Name { get; }
|
string Name { get; }
|
||||||
Uri Uri { get; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,29 +0,0 @@
|
|||||||
using System;
|
|
||||||
|
|
||||||
namespace Rehau.Sku.Assist
|
|
||||||
{
|
|
||||||
public class Product : IProduct
|
|
||||||
{
|
|
||||||
public string Sku { get; }
|
|
||||||
public string Name { get; }
|
|
||||||
public Uri Uri { get; }
|
|
||||||
|
|
||||||
public Product(string sku, string name)
|
|
||||||
{
|
|
||||||
Sku = sku;
|
|
||||||
Name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Product(string sku, string name, string uri)
|
|
||||||
{
|
|
||||||
Sku = sku;
|
|
||||||
Name = name;
|
|
||||||
Uri = new Uri(uri);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString()
|
|
||||||
{
|
|
||||||
return $"{this.Name} ({this.Sku})";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,9 +1,9 @@
|
|||||||
using AngleSharp.Dom;
|
using AngleSharp.Dom;
|
||||||
using AngleSharp.Html.Dom;
|
using Newtonsoft.Json;
|
||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Rehau.Sku.Assist
|
namespace Rehau.Sku.Assist
|
||||||
{
|
{
|
||||||
@ -25,38 +25,28 @@ namespace Rehau.Sku.Assist
|
|||||||
Task<string> contentTask = Task.Run(() => HttpClientUtil.GetContentByUriAsync(uri));
|
Task<string> contentTask = Task.Run(() => HttpClientUtil.GetContentByUriAsync(uri));
|
||||||
Task<IDocument> documentTask = await contentTask.ContinueWith(content => HttpClientUtil.ContentToDocAsync(content));
|
Task<IDocument> documentTask = await contentTask.ContinueWith(content => HttpClientUtil.ContentToDocAsync(content));
|
||||||
|
|
||||||
IProduct product = await documentTask.ContinueWith(doc => SkuAssist.GetFirstProduct(doc.Result));
|
return GetProduct(documentTask.Result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IProduct GetProduct(IDocument d)
|
||||||
|
{
|
||||||
|
string script = d.Scripts
|
||||||
|
.Where(s => s.InnerHtml.Contains("dataLayer"))
|
||||||
|
.First()
|
||||||
|
.InnerHtml;
|
||||||
|
|
||||||
|
string json = script
|
||||||
|
.Substring(script.IndexOf("push(") + 5)
|
||||||
|
.TrimEnd(new[] { ')', ';', '\n', ' ' });
|
||||||
|
|
||||||
|
StoreResponce storeResponse = JsonConvert.DeserializeObject<StoreResponce>(json);
|
||||||
|
IProduct product = storeResponse
|
||||||
|
.Ecommerce
|
||||||
|
.Impressions
|
||||||
|
.Where(i => Regex.IsMatch(i.Id, @"\d{11}", RegexOptions.None))
|
||||||
|
.FirstOrDefault();
|
||||||
|
|
||||||
return product;
|
return product;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IProduct GetFirstProduct(IDocument doc)
|
|
||||||
{
|
|
||||||
return doc
|
|
||||||
.All
|
|
||||||
.Where(e => e.ClassName == "product-item__desc-top")
|
|
||||||
.Where(e => Regex.IsMatch(e.Children[0].TextContent, @"\d{11}", RegexOptions.None))
|
|
||||||
.Select(e =>
|
|
||||||
new Product(e.Children[0].TextContent,
|
|
||||||
e.Children[1].TextContent.Trim(new[] { '\n', ' ' })))
|
|
||||||
.FirstOrDefault();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Uri GetFirstResultLink(IDocument doc)
|
|
||||||
{
|
|
||||||
var link = new Uri(doc
|
|
||||||
.Links
|
|
||||||
.Where(e => e.ClassName == "product-item__title-link js-name")
|
|
||||||
.Select(l => ((IHtmlAnchorElement)l).Href)
|
|
||||||
.FirstOrDefault());
|
|
||||||
return link;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string GetFistResultImageLink(IDocument doc)
|
|
||||||
{
|
|
||||||
var imageSource = doc.Images
|
|
||||||
.Where(x => x.ClassName == "product-item__image")
|
|
||||||
.FirstOrDefault();
|
|
||||||
return imageSource != null ? imageSource.Source : "Нет ссылки";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
21
Source/Assistant/StoreResponse.cs
Normal file
21
Source/Assistant/StoreResponse.cs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Rehau.Sku.Assist
|
||||||
|
{
|
||||||
|
public class StoreResponce
|
||||||
|
{
|
||||||
|
public Ecommerce Ecommerce { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Ecommerce
|
||||||
|
{
|
||||||
|
public List<Product> Impressions { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Product : IProduct
|
||||||
|
{
|
||||||
|
public string Id { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
public string Price { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
using ExcelDna.Integration;
|
using ExcelDna.Integration;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using System.Runtime.Caching;
|
using System.Runtime.Caching;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Rehau.Sku.Assist
|
namespace Rehau.Sku.Assist
|
||||||
{
|
{
|
||||||
@ -17,7 +17,7 @@ namespace Rehau.Sku.Assist
|
|||||||
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
object result = ExcelAsyncUtil.Run("Rauname", new[] { request },
|
object result = ExcelAsyncUtil.Run("RauName", new[] { request },
|
||||||
delegate
|
delegate
|
||||||
{
|
{
|
||||||
Task<IProduct> p = Task.Run(() => SkuAssist.GetProduct(request));
|
Task<IProduct> p = Task.Run(() => SkuAssist.GetProduct(request));
|
||||||
@ -32,7 +32,6 @@ namespace Rehau.Sku.Assist
|
|||||||
|
|
||||||
IProduct product = result as IProduct;
|
IProduct product = result as IProduct;
|
||||||
MemoryCache.Default.Add(request, product, System.DateTime.Now.AddMinutes(10));
|
MemoryCache.Default.Add(request, product, System.DateTime.Now.AddMinutes(10));
|
||||||
//MemoryCache.Default.Add(product.Name, product, System.DateTime.Now.AddMinutes(10));
|
|
||||||
return product.Name;
|
return product.Name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -42,8 +41,8 @@ namespace Rehau.Sku.Assist
|
|||||||
{
|
{
|
||||||
if (MemoryCache.Default.Contains(request))
|
if (MemoryCache.Default.Contains(request))
|
||||||
{
|
{
|
||||||
IProduct result = MemoryCache.Default[request] as IProduct;
|
IProduct product = MemoryCache.Default[request] as IProduct;
|
||||||
return result.Sku;
|
return product.Id;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -62,8 +61,7 @@ namespace Rehau.Sku.Assist
|
|||||||
|
|
||||||
IProduct product = result as IProduct;
|
IProduct product = result as IProduct;
|
||||||
MemoryCache.Default.Add(request, product, System.DateTime.Now.AddMinutes(10));
|
MemoryCache.Default.Add(request, product, System.DateTime.Now.AddMinutes(10));
|
||||||
//MemoryCache.Default.Add(product.Sku, product, System.DateTime.Now.AddMinutes(10));
|
return product.Id;
|
||||||
return product.Sku;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
<package id="ExcelDna.AddIn" version="1.5.0" targetFramework="net480" />
|
<package id="ExcelDna.AddIn" version="1.5.0" targetFramework="net480" />
|
||||||
<package id="ExcelDna.Integration" version="1.5.0" targetFramework="net480" />
|
<package id="ExcelDna.Integration" version="1.5.0" targetFramework="net480" />
|
||||||
<package id="ExcelDna.Registration" version="1.5.0" targetFramework="net480" />
|
<package id="ExcelDna.Registration" version="1.5.0" targetFramework="net480" />
|
||||||
|
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net48" />
|
||||||
<package id="NUnit" version="3.13.2" targetFramework="net48" />
|
<package id="NUnit" version="3.13.2" targetFramework="net48" />
|
||||||
<package id="NUnit3TestAdapter" version="4.1.0" targetFramework="net48" />
|
<package id="NUnit3TestAdapter" version="4.1.0" targetFramework="net48" />
|
||||||
<package id="System.Buffers" version="4.5.1" targetFramework="net48" />
|
<package id="System.Buffers" version="4.5.1" targetFramework="net48" />
|
||||||
|
Loading…
Reference in New Issue
Block a user