Added identifier and fetch time product listings; Added persistence to AdafruitShop.
Implemented in AdafruitShop. AdafruitShop Product listing data now persisted. AdafruitShop configuration now persisted.
This commit is contained in:
@@ -7,11 +7,11 @@ namespace Props.Shop.Adafruit.Api
|
||||
{
|
||||
public interface IProductListingManager : IDisposable
|
||||
{
|
||||
public Task<IDictionary<string, IList<ProductListing>>> ProductListings { get; }
|
||||
public Task<IReadOnlyDictionary<string, IList<ProductListing>>> ProductListings { get; }
|
||||
public void RefreshProductListings();
|
||||
public void StartUpdateTimer(int delay = 1000 * 60 * 5, int period = 1000 * 60 * 5);
|
||||
public void StopUpdateTimer();
|
||||
|
||||
public Task<ProductListing> GetProductListingFromUrl(string url);
|
||||
public Task<ProductListing> GetProductListingFromIdentifier(string url);
|
||||
}
|
||||
}
|
@@ -16,6 +16,7 @@ namespace Props.Shop.Adafruit.Api
|
||||
{
|
||||
using (StreamReader streamReader = new StreamReader(stream))
|
||||
{
|
||||
DateTime startTime = DateTime.UtcNow;
|
||||
dynamic data = JArray.Load(new JsonTextReader(streamReader));
|
||||
List<ProductListing> parsed = new List<ProductListing>();
|
||||
foreach (dynamic item in data)
|
||||
@@ -23,6 +24,7 @@ namespace Props.Shop.Adafruit.Api
|
||||
if (item.products_discontinued == 0)
|
||||
{
|
||||
ProductListing res = new ProductListing();
|
||||
res.TimeFetchedUtc = startTime;
|
||||
res.Name = item.product_name;
|
||||
res.LowerPrice = item.product_price;
|
||||
res.UpperPrice = res.LowerPrice;
|
||||
@@ -40,6 +42,7 @@ namespace Props.Shop.Adafruit.Api
|
||||
res.URL = item.product_url;
|
||||
res.InStock = item.product_stock > 0;
|
||||
parsed.Add(res);
|
||||
res.Identifier = res.URL;
|
||||
}
|
||||
}
|
||||
ProductListings = parsed;
|
||||
|
@@ -16,20 +16,21 @@ namespace Props.Shop.Adafruit.Api
|
||||
private int minDownloadInterval;
|
||||
private DateTime? lastDownload;
|
||||
private object refreshLock = new object();
|
||||
private volatile Task<IDictionary<string, IList<ProductListing>>> productListingsTask;
|
||||
private volatile Task<IReadOnlyDictionary<string, IList<ProductListing>>> productListingsTask;
|
||||
|
||||
public Task<IDictionary<string, IList<ProductListing>>> ProductListings => productListingsTask;
|
||||
private readonly ConcurrentDictionary<string, ProductListing> activeProductListingUrls = new ConcurrentDictionary<string, ProductListing>();
|
||||
public Task<IReadOnlyDictionary<string, IList<ProductListing>>> ProductListings => productListingsTask;
|
||||
private readonly ConcurrentDictionary<string, ProductListing> identifierMap = new ConcurrentDictionary<string, ProductListing>();
|
||||
|
||||
private ProductListingsParser parser = new ProductListingsParser();
|
||||
private HttpClient httpClient;
|
||||
private Timer updateTimer;
|
||||
|
||||
public LiveProductListingManager(HttpClient httpClient, ILogger<LiveProductListingManager> logger, int minDownloadInterval = 5 * 60 * 1000)
|
||||
public LiveProductListingManager(HttpClient httpClient, ILogger<LiveProductListingManager> logger, IReadOnlyDictionary<string, IList<ProductListing>> productListings = null, int minDownloadInterval = 5 * 60 * 1000)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.minDownloadInterval = minDownloadInterval;
|
||||
this.httpClient = httpClient;
|
||||
productListingsTask = Task.FromResult(productListings);
|
||||
}
|
||||
|
||||
public void RefreshProductListings()
|
||||
@@ -44,14 +45,14 @@ namespace Props.Shop.Adafruit.Api
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ProductListing> GetProductListingFromUrl(string url)
|
||||
public async Task<ProductListing> GetProductListingFromIdentifier(string identifier)
|
||||
{
|
||||
if (disposedValue) throw new ObjectDisposedException("ProductListingManager");
|
||||
await productListingsTask;
|
||||
return activeProductListingUrls[url];
|
||||
return identifierMap[identifier];
|
||||
}
|
||||
|
||||
private async Task<IDictionary<string, IList<ProductListing>>> DownloadListings()
|
||||
private async Task<IReadOnlyDictionary<string, IList<ProductListing>>> DownloadListings()
|
||||
{
|
||||
if (disposedValue) throw new ObjectDisposedException("ProductListingManager");
|
||||
logger.LogDebug("Beginning listing database download.");
|
||||
@@ -59,10 +60,10 @@ namespace Props.Shop.Adafruit.Api
|
||||
parser.BuildProductListings(responseMessage.Content.ReadAsStream());
|
||||
logger.LogDebug("Listing database parsed.");
|
||||
Dictionary<string, IList<ProductListing>> listingNames = new Dictionary<string, IList<ProductListing>>();
|
||||
activeProductListingUrls.Clear();
|
||||
identifierMap.Clear();
|
||||
foreach (ProductListing product in parser.ProductListings)
|
||||
{
|
||||
activeProductListingUrls.TryAdd(product.URL, product);
|
||||
identifierMap.TryAdd(product.Identifier, product);
|
||||
IList<ProductListing> sameProducts = listingNames.GetValueOrDefault(product.Name);
|
||||
if (sameProducts == null)
|
||||
{
|
||||
|
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using FuzzySharp;
|
||||
@@ -26,7 +27,8 @@ namespace Props.Shop.Adafruit.Api
|
||||
if (ProductListingManager.ProductListings == null) {
|
||||
ProductListingManager.RefreshProductListings();
|
||||
}
|
||||
IDictionary<string, IList<ProductListing>> productListings = await ProductListingManager.ProductListings;
|
||||
IReadOnlyDictionary<string, IList<ProductListing>> productListings = await ProductListingManager.ProductListings;
|
||||
if (productListings == null) throw new InvalidAsynchronousStateException("productListings can't be null");
|
||||
foreach (ExtractedResult<string> listingNames in Process.ExtractAll(query, productListings.Keys, cutoff: (int)(Similarity * 100)))
|
||||
{
|
||||
foreach (ProductListing same in productListings[listingNames.Value])
|
||||
|
Reference in New Issue
Block a user