Made progress on implementing some shops.
Performed some folder restructuring as well.
This commit is contained in:
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user