Current state, whatever that means

This commit is contained in:
2022-03-17 20:04:12 -04:00
parent fb6dbdfaa7
commit 60edbee0b8
22 changed files with 1087 additions and 156 deletions

View File

@@ -0,0 +1,65 @@
using Ratermania.Automation.Interfaces;
using Ratermania.Shopify;
using Ratermania.Shopify.Resources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace PartSource.Automation.Jobs.POC
{
public class FixMultipleSeoTables : IAutomationJob
{
private readonly ShopifyClient _shopifyClient;
public FixMultipleSeoTables(ShopifyClient shopifyClient)
{
_shopifyClient = shopifyClient;
}
public async Task Run()
{
IEnumerable<Product> products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 }, { "product_type", "CA112-SC137-FL13750_Intake Manifolds" } });
while (products != null && products.Any())
{
foreach (Product product in products)
{
try
{
string[] parts = product.BodyHtml.Split("<div id=\"seoData\">");
if (parts.Length > 2)
{
string ul = product.BodyHtml.Substring(0, product.BodyHtml.IndexOf("</ul>") + "</ul>".Length);
string seoData = "<div id=\"seoData\">" + parts[1].Substring(0, parts[1].IndexOf("</table>") + "</table>".Length) + "</div>";
string vehicleIds = new Regex("<div id=\"vehicleIDs\".*</div>").Match(product.BodyHtml).Value;
product.BodyHtml = ul + seoData + vehicleIds;
await _shopifyClient.Products.Update(product);
}
}
catch
{
Console.WriteLine($"Failed to update {product.Id}");
}
}
try
{
Console.WriteLine("Did 250");
products = await _shopifyClient.Products.GetNext();
}
catch (Exception ex)
{
products = await _shopifyClient.Products.GetPrevious();
}
}
}
}
}

View File

@@ -0,0 +1,145 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using PartSource.Automation.Models;
using PartSource.Data;
using PartSource.Data.Contexts;
using PartSource.Data.Models;
using PartSource.Services;
using Ratermania.Automation.Interfaces;
using Ratermania.Shopify;
using Ratermania.Shopify.Exceptions;
using Ratermania.Shopify.Resources;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace PartSource.Automation.Jobs
{
public class UpdateFitmentHtml : IAutomationJob
{
private readonly ILogger<UpdateFitmentHtml> _logger;
private readonly ShopifyClient _shopifyClient;
private readonly PartSourceContext _partSourceContext;
private readonly FitmentContext _fitmentContext;
private readonly VehicleService _vehicleService;
public UpdateFitmentHtml(ILogger<UpdateFitmentHtml> logger, PartSourceContext partSourceContext, FitmentContext fitmentContext, ShopifyClient shopifyClient, VehicleService vehicleService)
{
_logger = logger;
_partSourceContext = partSourceContext;
_fitmentContext = fitmentContext;
_shopifyClient = shopifyClient;
_vehicleService = vehicleService;
}
public async Task Run()
{
IEnumerable<Product> products = null;
try
{
products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 }, { "vendor", "FRAM" } });
}
catch (Exception ex)
{
_logger.LogError("Failed to get products from Shopify", ex);
throw;
}
int i = 1;
while (products != null && products.Any())
{
foreach (Product product in products)
{
ImportData importData = null;
try
{
importData = await _partSourceContext.ImportData.FirstOrDefaultAsync(parts => parts.VariantSku == product.Variants[0].Sku);
if (importData == null)
{
continue;
}
product.BodyHtml = importData.BodyHtml;
IList<Vehicle> vehicles = _vehicleService.GetVehiclesForPart(importData.PartNumber, importData.LineCode);
IList<string> ymmFitment = _vehicleService.GetYmmFitment(vehicles);
if (ymmFitment.Count > 0)
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine("<table><tr><th colspan=\"2\">This Part Fits</th></tr>");
foreach (string fitment in ymmFitment)
{
try
{
string[] parts = fitment.Split(' ', 2);
stringBuilder.AppendLine($"<tr><td>{parts[1]}</td><td>{parts[0].Replace("-", ", ")}</td></tr>");
}
catch
{
// This is still a POC at this point. Oh well...
}
}
stringBuilder.AppendLine("</table>");
product.BodyHtml += $"<div id=\"seoData\">{stringBuilder.ToString()}</div>";
}
IList<int> vehicleIdFitment = _vehicleService.GetVehicleIdFitment(vehicles);
if (vehicleIdFitment.Count > 0)
{
string vehicleIdString = string.Join('-', vehicleIdFitment.Select(j => $"v{j}"));
product.BodyHtml += $"<div id=\"vehicleIDs\" style=\"display:none;\">{vehicleIdString}</div>";
}
List<string> tags = new List<string>
{
importData.LineCode,
importData.PartNumber,
"zzzIsFitment=true"
};
product.Tags = string.Join(',', tags);
await _shopifyClient.Products.Update(product);
}
catch (Exception ex)
{
_logger.LogError($"Failed to updated fitment data for SKU {importData?.VariantSku} - {ex.Message}", ex);
}
}
try
{
Console.WriteLine(i);
products = await _shopifyClient.Products.GetNext();
i++;
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to get the next set of products. Retrying");
products = await _shopifyClient.Products.GetPrevious();
}
}
}
}
}

View File

@@ -0,0 +1,144 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using PartSource.Automation.Models;
using PartSource.Data;
using PartSource.Data.Contexts;
using PartSource.Data.Dtos;
using PartSource.Data.Models;
using PartSource.Services;
using Ratermania.Automation.Interfaces;
using Ratermania.Shopify;
using Ratermania.Shopify.Exceptions;
using Ratermania.Shopify.Resources;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace PartSource.Automation.Jobs.POC
{
public class UpdateFitmentScratchpad : IAutomationJob
{
private readonly ILogger<UpdateFitmentScratchpad> _logger;
private readonly ShopifyClient _shopifyClient;
private readonly PartSourceContext _partSourceContext;
private readonly FitmentContext _fitmentContext;
private readonly VehicleService _vehicleService;
public UpdateFitmentScratchpad(ILogger<UpdateFitmentScratchpad> logger, PartSourceContext partSourceContext, FitmentContext fitmentContext, ShopifyClient shopifyClient, VehicleService vehicleService)
{
_logger = logger;
_partSourceContext = partSourceContext;
_fitmentContext = fitmentContext;
_shopifyClient = shopifyClient;
_vehicleService = vehicleService;
}
public async Task Run()
{
IList<string> productTypes = new List<string>
{
"C172-S231-F23107_(PS) Wipers - TRICO Neoform",
"CA172-SC231-FL23106_Wiper Blades - Bosch Icon",
"CA172-SC231-FL23107_(PS) Wipers - TRICO Neoform",
"CA172-SC231-FL23109_(PS) Wipers - TRICO Tech/Exact Fit",
"CA172-SC231-FL23110_Wiper Accessories",
"CA172-SC231-FL23114_Wiper Blades - Rear",
"CA172-SC231-FL23116_(PS) Wipers - Bosch Insight (Hybrid)",
"CA172-SC231-FL23117_(PS) Wipers - Bosch Clear Advantage (Beam)",
"CA172-SC231-FL23118_(PS) Wipers - Winter",
"CA172-SC231-FL23125_Wiper Blades - Bosch Areotwin"
};
IList<string> csvData = new List<string>
{
"\"Line Code\", \"Part Number\", \"Year\", \"Make\", \"Model\", \"Position\""
};
foreach (string type in productTypes)
{
IEnumerable<Product> products = null;
try
{
products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 }, { "product_type", type } });
}
catch (Exception ex)
{
_logger.LogError("Failed to get products from Shopify", ex);
throw;
}
while (products != null && products.Any())
{
foreach (Product product in products)
{
ImportData importData = null;
try
{
IEnumerable<Metafield> metafields = await _shopifyClient.Metafields.Get(new Dictionary<string, object> { { "metafield[owner_id]", product.Id }, { "metafield[owner_resource]", "product" } });
importData = new ImportData
{
LineCode = metafields.FirstOrDefault(m => m.Key == "custom_label_0").Value ?? string.Empty,
PartNumber = product.Title.Split(' ')[0],
VariantSku = product.Variants[0].Sku // They know we can't do fitment for variants
};
string csvRow = product.BodyHtml.Substring(0, product.BodyHtml.IndexOf("</ul>") + "</ul>".Length);
IList<VehicleFitmentDto> vehicles = _vehicleService.GetVehicleFitmentForPart(importData.PartNumber, importData.LineCode);
if (vehicles.Count > 0)
{
foreach (VehicleFitmentDto vehicle in vehicles)
{
try
{
csvData.Add($"\"{importData.LineCode}\", \"{importData.PartNumber}\", \"{vehicle.Vehicle.Year}\", \"{vehicle.Vehicle.MakeName}\", \"{vehicle.Vehicle.ModelName}\", \"{vehicle.Fitment.Position}\"");
}
catch
{
// This is still a POC at this point. Oh well...
}
}
}
}
catch (Exception ex)
{
_logger.LogError($"Failed to updated fitment data for SKU {importData?.VariantSku} - {ex.Message}", ex);
}
}
try
{
Console.WriteLine($"Did an iteration of {type}");
products = await _shopifyClient.Products.GetNext();
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to get the next set of products. Retrying");
products = await _shopifyClient.Products.GetPrevious();
}
}
}
await File.WriteAllLinesAsync("C:\\users\\Tommy\\desktop\\Wiper Fitment.csv", csvData);
;
}
}
}