Add Uri Converter
This commit is contained in:
parent
f3b6bfcd3e
commit
3ea18ae25e
@ -1,5 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="packages\NUnit3TestAdapter.4.1.0\build\net35\NUnit3TestAdapter.props" Condition="Exists('packages\NUnit3TestAdapter.4.1.0\build\net35\NUnit3TestAdapter.props')" />
|
||||
<Import Project="packages\NUnit.3.13.2\build\NUnit.props" Condition="Exists('packages\NUnit.3.13.2\build\NUnit.props')" />
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
@ -46,6 +48,9 @@
|
||||
<HintPath>packages\ExcelDna.Registration.1.5.0\lib\net452\ExcelDna.Registration.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<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>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
||||
<HintPath>packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
|
||||
@ -80,12 +85,13 @@
|
||||
<Reference Include="WindowsBase" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="src\ExcelDNA\AddIn.cs" />
|
||||
<Compile Include="src\Assistant\IProduct.cs" />
|
||||
<Compile Include="src\Assistant\Product.cs" />
|
||||
<Compile Include="src\ExcelDNA\Functions.cs" />
|
||||
<Compile Include="Source\ExcelDNA\AddIn.cs" />
|
||||
<Compile Include="Source\Assistant\IProduct.cs" />
|
||||
<Compile Include="Source\Assistant\Product.cs" />
|
||||
<Compile Include="Source\ExcelDNA\Functions.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="src\Assistant\SkuAssist.cs" />
|
||||
<Compile Include="Source\Assistant\SkuAssist.cs" />
|
||||
<Compile Include="Tests\SkuAssistTests.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="app.config" />
|
||||
@ -100,5 +106,7 @@
|
||||
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
|
||||
</PropertyGroup>
|
||||
<Error Condition="!Exists('packages\ExcelDna.AddIn.1.5.0\build\ExcelDna.AddIn.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\ExcelDna.AddIn.1.5.0\build\ExcelDna.AddIn.targets'))" />
|
||||
<Error Condition="!Exists('packages\NUnit.3.13.2\build\NUnit.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\NUnit.3.13.2\build\NUnit.props'))" />
|
||||
<Error Condition="!Exists('packages\NUnit3TestAdapter.4.1.0\build\net35\NUnit3TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\NUnit3TestAdapter.4.1.0\build\net35\NUnit3TestAdapter.props'))" />
|
||||
</Target>
|
||||
</Project>
|
80
Source/Assistant/SkuAssist.cs
Normal file
80
Source/Assistant/SkuAssist.cs
Normal file
@ -0,0 +1,80 @@
|
||||
using AngleSharp;
|
||||
using AngleSharp.Dom;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Rehau.Sku.Assist
|
||||
{
|
||||
static class SkuAssist
|
||||
{
|
||||
private static HttpClient _httpClient;
|
||||
private enum ResponseOrder
|
||||
{
|
||||
NoSettings,
|
||||
Relevance,
|
||||
Name,
|
||||
Price,
|
||||
Series
|
||||
}
|
||||
private static void _EnsureHttpClientRunning()
|
||||
{
|
||||
if (_httpClient == null)
|
||||
_httpClient = new HttpClient();
|
||||
}
|
||||
|
||||
public async static Task<string> GetContent(string request)
|
||||
{
|
||||
Uri uri = _ConvertToUri(request, ResponseOrder.NoSettings);
|
||||
_EnsureHttpClientRunning();
|
||||
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
|
||||
|
||||
return await _httpClient.GetStringAsync(uri);
|
||||
}
|
||||
|
||||
public async static Task<IDocument> GetDocument(Task<string> source)
|
||||
{
|
||||
IConfiguration config = Configuration.Default;
|
||||
IBrowsingContext context = BrowsingContext.New(config);
|
||||
|
||||
return await context.OpenAsync(req => req.Content(source.Result));
|
||||
}
|
||||
|
||||
public static IProduct GetProductFromDocument(IDocument document)
|
||||
{
|
||||
return document
|
||||
.All
|
||||
.Where(e => e.ClassName == "product-item__desc-top")
|
||||
.Select(e => new Product(e.Children[0].TextContent, e.Children[1].TextContent.Trim(new[] { '\n', ' ' })))
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
private static Uri _ConvertToUri(this string request, ResponseOrder order)
|
||||
{
|
||||
string cleanedRequest = request._CleanRequest();
|
||||
switch (order)
|
||||
{
|
||||
case ResponseOrder.Relevance:
|
||||
return new Uri("https://shop-rehau.ru/catalogsearch/result/index/?dir=asc&order=relevance&q=" + cleanedRequest);
|
||||
case ResponseOrder.Name:
|
||||
return new Uri("https://shop-rehau.ru/catalogsearch/result/index/?dir=asc&order=name&q=" + cleanedRequest);
|
||||
case ResponseOrder.Price:
|
||||
return new Uri("https://shop-rehau.ru/catalogsearch/result/index/?dir=asc&order=price&q=" + cleanedRequest);
|
||||
case ResponseOrder.Series:
|
||||
return new Uri("https://shop-rehau.ru/catalogsearch/result/index/?dir=asc&order=sch_product_series&q=" + cleanedRequest);
|
||||
case ResponseOrder.NoSettings:
|
||||
return new Uri("https://shop-rehau.ru/catalogsearch/result/?q=" + cleanedRequest);
|
||||
default:
|
||||
throw new ArgumentException();
|
||||
}
|
||||
}
|
||||
private static string _CleanRequest(this string input)
|
||||
{
|
||||
return input.Replace("+", " plus ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,15 +7,13 @@ namespace Rehau.Sku.Assist
|
||||
{
|
||||
public class Functions
|
||||
{
|
||||
private static HttpClient _httpClient = new HttpClient();
|
||||
|
||||
[ExcelFunction]
|
||||
public static async Task<string> RAUNAME(string request)
|
||||
{
|
||||
Task<string> contentTask = Task.Run(() => SkuAssist.GetContent(request, _httpClient));
|
||||
Task<string> contentTask = Task.Run(() => SkuAssist.GetContent(request));
|
||||
Task<IDocument> documentTask = await contentTask.ContinueWith(content => SkuAssist.GetDocument(content));
|
||||
IProduct product = await documentTask.ContinueWith(doc => SkuAssist.GetProductFromDocument(doc.Result));
|
||||
return product == null ? ExcelError.ExcelErrorNull.ToString() : product.ToString();
|
||||
return product != null ? product.ToString() : "Не найдено";
|
||||
}
|
||||
}
|
||||
}
|
15
Tests/SkuAssistTests.cs
Normal file
15
Tests/SkuAssistTests.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Rehau.Sku.Assist.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public class SkuAssistTests
|
||||
{
|
||||
[Test]
|
||||
public async void BaseTest()
|
||||
{
|
||||
var result = await Functions.RAUNAME("160001");
|
||||
Assert.AreEqual("Надвижная гильза REHAU RAUTITAN РХ (11600011001)", result);
|
||||
}
|
||||
}
|
||||
}
|
@ -4,6 +4,8 @@
|
||||
<package id="ExcelDna.AddIn" 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="NUnit" version="3.13.2" targetFramework="net48" />
|
||||
<package id="NUnit3TestAdapter" version="4.1.0" targetFramework="net48" />
|
||||
<package id="System.Buffers" version="4.5.1" targetFramework="net48" />
|
||||
<package id="System.Memory" version="4.5.4" targetFramework="net48" />
|
||||
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net48" />
|
||||
|
@ -1,45 +0,0 @@
|
||||
using AngleSharp;
|
||||
using AngleSharp.Dom;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Rehau.Sku.Assist
|
||||
{
|
||||
static class SkuAssist
|
||||
{
|
||||
public async static Task<string> GetContent(string request, HttpClient httpClient)
|
||||
{
|
||||
string uri = "https://shop-rehau.ru/catalogsearch/result/?q=" + request._CleanRequest();
|
||||
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
|
||||
|
||||
return await httpClient.GetStringAsync(uri);
|
||||
}
|
||||
|
||||
public async static Task<IDocument> GetDocument(Task<string> source)
|
||||
{
|
||||
IConfiguration config = Configuration.Default;
|
||||
IBrowsingContext context = BrowsingContext.New(config);
|
||||
|
||||
return await context.OpenAsync(req => req.Content(source.Result));
|
||||
}
|
||||
|
||||
public static IProduct GetProductFromDocument(IDocument document)
|
||||
{
|
||||
return document
|
||||
.All
|
||||
.Where(e => e.ClassName == "product-item__desc-top")
|
||||
.Select(e => new Product(e.Children[0].TextContent, e.Children[1].TextContent.Trim(new[] { '\n', ' ' })))
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
private static string _CleanRequest(this string input)
|
||||
{
|
||||
return input.Replace("+", " plus ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user