2021-07-20 22:51:43 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2021-08-05 06:22:19 +00:00
|
|
|
using System.IO;
|
2021-07-20 22:51:43 +00:00
|
|
|
using System.Net.Http;
|
2021-08-05 06:22:19 +00:00
|
|
|
using Newtonsoft.Json;
|
2021-07-20 22:51:43 +00:00
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
using Props.Shop.Framework;
|
|
|
|
|
|
|
|
namespace Props.Shop.Adafruit.Api
|
|
|
|
{
|
2021-08-05 06:22:19 +00:00
|
|
|
public class ProductListingsParser
|
2021-07-20 22:51:43 +00:00
|
|
|
{
|
|
|
|
public IEnumerable<ProductListing> ProductListings { get; private set; }
|
2021-08-05 06:22:19 +00:00
|
|
|
public void BuildProductListings(Stream stream)
|
2021-07-20 22:51:43 +00:00
|
|
|
{
|
2021-08-05 06:22:19 +00:00
|
|
|
using (StreamReader streamReader = new StreamReader(stream))
|
2021-07-20 22:51:43 +00:00
|
|
|
{
|
2021-08-05 06:22:19 +00:00
|
|
|
dynamic data = JArray.Load(new JsonTextReader(streamReader));
|
|
|
|
List<ProductListing> parsed = new List<ProductListing>();
|
|
|
|
foreach (dynamic item in data)
|
2021-07-20 22:51:43 +00:00
|
|
|
{
|
2021-08-05 06:22:19 +00:00
|
|
|
if (item.products_discontinued == 0)
|
2021-07-20 22:51:43 +00:00
|
|
|
{
|
2021-08-05 06:22:19 +00:00
|
|
|
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)
|
2021-07-20 22:51:43 +00:00
|
|
|
{
|
2021-08-05 06:22:19 +00:00
|
|
|
if (discount.discounted_price < res.LowerPrice)
|
|
|
|
{
|
|
|
|
res.LowerPrice = discount.discounted_price;
|
|
|
|
}
|
|
|
|
if (discount.discounted_price > res.UpperPrice)
|
|
|
|
{
|
|
|
|
res.UpperPrice = discount.discounted_price;
|
|
|
|
}
|
2021-07-20 22:51:43 +00:00
|
|
|
}
|
2021-08-05 06:22:19 +00:00
|
|
|
res.URL = item.product_url;
|
|
|
|
res.InStock = item.product_stock > 0;
|
|
|
|
parsed.Add(res);
|
2021-07-20 22:51:43 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-05 06:22:19 +00:00
|
|
|
ProductListings = parsed;
|
2021-07-20 22:51:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|