Made progress on implementing some shops.
Performed some folder restructuring as well.
This commit is contained in:
68
Props-Modules/Props.Shop/Adafruit/AdafruitShop.cs
Normal file
68
Props-Modules/Props.Shop/Adafruit/AdafruitShop.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Text.Json;
|
||||
using Props.Shop.Adafruit.Api;
|
||||
using Props.Shop.Framework;
|
||||
|
||||
namespace Props.Shop.Adafruit
|
||||
{
|
||||
public class AdafruitShop : IShop
|
||||
{
|
||||
private ProductListingManager productListingManager;
|
||||
private Configuration configuration;
|
||||
private HttpClient http;
|
||||
private bool disposedValue;
|
||||
|
||||
public string ShopName => "Adafruit";
|
||||
|
||||
public string ShopDescription => "A electronic component online hardware company.";
|
||||
|
||||
public string ShopModuleAuthor => "Reslate";
|
||||
|
||||
public SupportedFeatures SupportedFeatures => new SupportedFeatures(
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
true
|
||||
);
|
||||
|
||||
public byte[] GetDataForPersistence()
|
||||
{
|
||||
return JsonSerializer.SerializeToUtf8Bytes(configuration);
|
||||
}
|
||||
|
||||
public IEnumerable<IOption> Initialize(byte[] data)
|
||||
{
|
||||
http = new HttpClient();
|
||||
http.BaseAddress = new Uri("http://www.adafruit.com/api");
|
||||
configuration = JsonSerializer.Deserialize<Configuration>(data);
|
||||
this.productListingManager = new ProductListingManager();
|
||||
return null;
|
||||
}
|
||||
|
||||
public IAsyncEnumerable<ProductListing> Search(string query, Filters filters)
|
||||
{
|
||||
return productListingManager.Search(query, configuration.Similarity, http);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!disposedValue)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
http.Dispose();
|
||||
}
|
||||
disposedValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(disposing: true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
44
Props-Modules/Props.Shop/Adafruit/Api/ListingsParser.cs
Normal file
44
Props-Modules/Props.Shop/Adafruit/Api/ListingsParser.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Props.Shop.Framework;
|
||||
|
||||
namespace Props.Shop.Adafruit.Api
|
||||
{
|
||||
public class ListingsParser
|
||||
{
|
||||
public IEnumerable<ProductListing> ProductListings { get; private set; }
|
||||
public ListingsParser(string json)
|
||||
{
|
||||
dynamic data = JArray.Parse(json);
|
||||
List<ProductListing> parsed = new List<ProductListing>();
|
||||
foreach (dynamic item in data)
|
||||
{
|
||||
if (item.products_discontinued == 0)
|
||||
{
|
||||
ProductListing res = new ProductListing();
|
||||
res.Name = item.product_name;
|
||||
res.LowerPrice = item.product_price;
|
||||
res.UpperPrice = res.LowerPrice;
|
||||
foreach (dynamic discount in item.discount_pricing)
|
||||
{
|
||||
if (discount.discounted_price < res.LowerPrice)
|
||||
{
|
||||
res.LowerPrice = discount.discounted_price;
|
||||
}
|
||||
if (discount.discounted_price > res.UpperPrice)
|
||||
{
|
||||
res.UpperPrice = discount.discounted_price;
|
||||
}
|
||||
}
|
||||
res.URL = item.product_url;
|
||||
res.InStock = item.product_stock > 0;
|
||||
parsed.Add(res);
|
||||
}
|
||||
}
|
||||
ProductListings = parsed;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using FuzzySharp;
|
||||
using FuzzySharp.Extractor;
|
||||
using Props.Shop.Framework;
|
||||
|
||||
namespace Props.Shop.Adafruit.Api
|
||||
{
|
||||
public class ProductListingManager
|
||||
{
|
||||
private double minutesPerRequest;
|
||||
private Dictionary<string, List<ProductListing>> listings = new Dictionary<string, List<ProductListing>>();
|
||||
private bool requested = false;
|
||||
public DateTime TimeOfLastRequest { get; private set; }
|
||||
public bool RequestReady => DateTime.Now - TimeOfLastRequest > TimeSpan.FromMinutes(minutesPerRequest);
|
||||
|
||||
public ProductListingManager(int requestsPerMinute = 5)
|
||||
{
|
||||
this.minutesPerRequest = 1 / requestsPerMinute;
|
||||
}
|
||||
|
||||
public async Task RefreshListings(HttpClient http)
|
||||
{
|
||||
requested = true;
|
||||
TimeOfLastRequest = DateTime.Now;
|
||||
HttpResponseMessage response = await http.GetAsync("/products");
|
||||
SetListings(await response.Content.ReadAsStringAsync());
|
||||
}
|
||||
|
||||
public void SetListings(string data)
|
||||
{
|
||||
ListingsParser listingsParser = new ListingsParser(data);
|
||||
foreach (ProductListing listing in listingsParser.ProductListings)
|
||||
{
|
||||
List<ProductListing> similar = listings.GetValueOrDefault(listing.Name, new List<ProductListing>());
|
||||
similar.Add(listing);
|
||||
listings[listing.Name] = similar;
|
||||
}
|
||||
}
|
||||
|
||||
public async IAsyncEnumerable<ProductListing> Search(string query, float similarity, HttpClient httpClient = null)
|
||||
{
|
||||
if (RequestReady && httpClient != null) await RefreshListings(httpClient);
|
||||
IEnumerable<ExtractedResult<string>> resultNames = Process.ExtractAll(query, listings.Keys, cutoff: (int)similarity * 100);
|
||||
foreach (ExtractedResult<string> resultName in resultNames)
|
||||
{
|
||||
foreach (ProductListing product in listings[resultName.Value])
|
||||
{
|
||||
yield return product;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
7
Props-Modules/Props.Shop/Adafruit/Configuration.cs
Normal file
7
Props-Modules/Props.Shop/Adafruit/Configuration.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Props.Shop.Adafruit
|
||||
{
|
||||
public class Configuration
|
||||
{
|
||||
public float Similarity { get; set; }
|
||||
}
|
||||
}
|
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using Props.Shop.Framework;
|
||||
|
||||
namespace Props.Shop.Adafruit.Options
|
||||
{
|
||||
public class SimilarityOption : IOption
|
||||
{
|
||||
private Configuration configuration;
|
||||
public string Name => "Query Similarity";
|
||||
|
||||
public string Description => "The minimum level of similarity for a listing to be returned.";
|
||||
|
||||
public bool Required => true;
|
||||
|
||||
public Type Type => typeof(float);
|
||||
|
||||
internal SimilarityOption(Configuration configuration)
|
||||
{
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
public string GetValue()
|
||||
{
|
||||
return configuration.Similarity.ToString();
|
||||
}
|
||||
|
||||
public bool SetValue(string value)
|
||||
{
|
||||
float parsed;
|
||||
bool success = float.TryParse(value, out parsed);
|
||||
configuration.Similarity = parsed;
|
||||
return success;
|
||||
}
|
||||
}
|
||||
}
|
16
Props-Modules/Props.Shop/Adafruit/Props.Shop.Adafruit.csproj
Normal file
16
Props-Modules/Props.Shop/Adafruit/Props.Shop.Adafruit.csproj
Normal file
@@ -0,0 +1,16 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FuzzySharp" Version="2.0.2" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Framework\Props.Shop.Framework.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
Reference in New Issue
Block a user