Whatever this is

This commit is contained in:
2025-02-12 18:12:19 -05:00
parent aed30707be
commit cc2cbd09e1
20 changed files with 496 additions and 434 deletions

View File

@@ -5,6 +5,7 @@ using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using PartSource.Automation.Services;
@@ -13,6 +14,7 @@ using PartSource.Data.Models;
using Ratermania.Automation.Interfaces;
using Ratermania.Shopify;
using Ratermania.Shopify.Resources;
using System.Web;
namespace PartSource.Automation.Jobs
{
@@ -36,52 +38,22 @@ namespace PartSource.Automation.Jobs
public async Task Run(CancellationToken token, params string[] arguments)
{
IEnumerable<Product> products = null;
IList<string> productTypes = await _fitmentContext.ProductTypes
.Where(p => p.Active)
.Select(p => HttpUtility.UrlEncode(p.Name))
.ToListAsync();
IList<string> partTypes = new List<string>
foreach (string productType in productTypes)
{
"CA115-SC118-FL11803_Custom Lighting Accessories",
"CA117-SC141-FL14106_Jeep Accessories",
"CA117-SC141-FL14134_Truck Running Board and Steps",
"CA117-SC141-FL14199_Bumpers, Bull Bars & Brush Guards",
"CA117-SC157-FL15704_Headache Rack Frames",
"CA117-SC158-FL15802_Salters and Plow Accessories",
"CA117-SC699-FL69902_Crossover Boxes",
"CA117-SC699-FL69903_Specialty Boxes",
"CA117-SC699-FL69904_Transfer Tanks",
"CA135-SC176-FL17601_Trailer Lighting, Stop, Turn, Tail",
"CA135-SC186-FL18607_Roof Racks",
"CA135-SC186-FL18608_Bike Carriers",
"CA135-SC186-FL18609_Cargo Accessories",
"CA135-SC186-FL18610_Cargo Carriers",
"CA135-SC186-FL18611_Watersport Carriers",
"CA135-SC192-FL19201_Class 1 Hitches",
"CA135-SC192-FL19202_Class 2 Hitches",
"CA135-SC192-FL19203_Class 3 Hitches",
"CA135-SC192-FL19204_Towing, Heavy Duty",
"CA135-SC192-FL19205_Towing Electrical, Vehicle Specific",
"CA135-SC192-FL19206_Trailer Parts & Accessories",
"CA135-SC192-FL19207_Trailer Winches, Jacks & Couplers",
"CA135-SC192-FL19208_Class 5 Hitches",
"CA135-SC192-FL19221_Towing Electrical, Connectors & Adapters",
"CA135-SC192-FL19230_Towing Electrical, Controls & Converters",
"CA135-SC192-FL19235_Towing Electrical, Harnesses",
"CA135-SC192-FL19240_Towing Security, Non-Locking",
"CA135-SC192-FL19245_Towing Class V",
"CA135-SC192-FL19280_Towing Balls",
"CA135-SC192-FL19281_Towing Ball Mounts",
"CA135-SC192-FL19282_Towing Security, Locking",
"CA135-SC192-FL19283_Towing Kits & Acc"
};
_logger.LogInformation("Processing {productType}", HttpUtility.UrlDecode(productType));
foreach (string partType in partTypes)
{
IEnumerable<Product> products = null;
try
{
products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 }, { "product_type", partType } });
products = await _shopifyClient.Products.Get(new Dictionary<string, object> { { "limit", 250 }, { "product_type", productType } });
//products = new List<Product>
//{
// await _shopifyClient.Products.GetById(4388919574575)
// await _shopifyClient.Products.GetById(7458071052335)
//};
}
@@ -91,13 +63,12 @@ namespace PartSource.Automation.Jobs
throw;
}
int i = 1;
while (products != null && products.Any())
{
foreach (Product product in products)
{
ImportData importData = null;
bool isFitment = false;
try
{
@@ -109,48 +80,32 @@ namespace PartSource.Automation.Jobs
VariantSku = product.Variants[0].Sku // They know we can't do fitment for variants
};
bool isFitment = false;
string bodyHtml = product.BodyHtml.Substring(0, product.BodyHtml.IndexOf("</ul>") + "</ul>".Length);
importData.PartNumber = importData.PartNumber.Replace("-", string.Empty);
//If the line code is numeric, it cannot have fitment data associated with it.
if (int.TryParse(importData.LineCode, out _))
{
continue;
}
// Extract Partsource bullet points if present.
string bodyHtml = string.IsNullOrEmpty(product.BodyHtml)
? string.Empty
: product.BodyHtml.Substring(0, product.BodyHtml.IndexOf("</ul>") + "</ul>".Length);
IList<Vehicle> vehicles = _vehicleFitmentService.GetVehiclesForPart(importData.PartNumber, importData.LineCode);
IList<int> vehicleIdFitment = _vehicleFitmentService.GetVehicleIdFitment(vehicles);
if (!vehicleIdFitment.Any())
{
Console.WriteLine($"No fitment data for SKU {importData.VariantSku}");
Console.WriteLine($"No fitment data for {importData.LineCode} {importData.PartNumber}");
continue;
}
if (vehicleIdFitment.Count > 0)
{
string vehicleIdString = string.Join(',', vehicleIdFitment.Select(j => $"v{j}"));
string vehicleIdString = string.Join(',', vehicleIdFitment.Select(j => $"v{j}"));
bodyHtml += $"<div id=\"vehicleIDs\" style=\"display:none;\">{vehicleIdString}</div>";
isFitment = true;
string json = JsonConvert.SerializeObject(vehicleIdFitment);
if (json.Length < 100000)
{
Metafield vehicleMetafield = new Metafield
{
Namespace = "fitment",
Key = "ids",
Value = json,
Type = "json_string",
OwnerResource = "product",
OwnerId = product.Id
};
await _shopifyClient.Metafields.Add(vehicleMetafield);
}
else
{
_logger.LogWarning($"Vehicle ID fitment data for SKU {importData.VariantSku} is too large for Shopify and cannot be added.");
continue;
}
}
bodyHtml += $"<div id=\"vehicleIDs\" style=\"display:none;\">{vehicleIdString}</div>";
isFitment = true;
IList<string> ymmFitment = _vehicleFitmentService.GetYmmFitment(vehicles);
if (ymmFitment.Count > 0)
@@ -165,79 +120,20 @@ namespace PartSource.Automation.Jobs
try
{
string[] parts = fitment.Split(' ', 2);
stringBuilder.AppendLine($"<tr><td>{parts[1]}</td><td>{parts[0].Replace("-", ", ")}</td></tr>");
}
catch
catch (Exception ex)
{
// This is still a POC at this point. Oh well...
_logger.LogWarning(ex, "YMM fitment for {fitment} was in an invalid format", fitment);
}
}
stringBuilder.AppendLine("</table>");
bodyHtml += $"<div id=\"seoData\">{stringBuilder.ToString()}</div>";
string json = JsonConvert.SerializeObject(ymmFitment);
if (json.Length < 100000)
{
Metafield ymmMetafield = new Metafield
{
Namespace = "fitment",
Key = "seo",
Value = json,
Type = "json_string",
OwnerResource = "product",
OwnerId = product.Id
};
await _shopifyClient.Metafields.Add(ymmMetafield);
}
else
{
_logger.LogWarning($"Year/make/model fitment data for SKU {importData.VariantSku} is too large for Shopify and cannot be added.");
continue;
}
bodyHtml += $"<div id=\"seoData\">{stringBuilder}</div>";
}
Metafield isFitmentMetafield = new Metafield
{
Namespace = "Flags",
Key = "IsFitment",
Value = isFitment.ToString(),
Type = "string",
OwnerResource = "product",
OwnerId = product.Id
};
await _shopifyClient.Metafields.Add(isFitmentMetafield);
//Metafield lineCodeMetafield = new Metafield
//{
// Namespace = "google",
// Key = "custom_label_0",
// Value = importData.LineCode,
// Type = "string",
// OwnerResource = "product",
// OwnerId = product.Id
//};
//await _shopifyClient.Metafields.Add(lineCodeMetafield);
//Metafield partNumberMetafield = new Metafield
//{
// Namespace = "google",
// Key = "custom_label_1",
// Value = importData.PartNumber,
// Type = "string",
// OwnerResource = "product",
// OwnerId = product.Id
//};
//await _shopifyClient.Metafields.Add(partNumberMetafield);
List<string> tags = new List<string>();
for (int j = 0; j < vehicleIdFitment.Count; j += 25)
@@ -275,12 +171,8 @@ namespace PartSource.Automation.Jobs
}
try
{
Console.WriteLine(i);
_partSourceContext.SaveChanges();
products = await _shopifyClient.Products.GetNext();
i++;
}
catch (Exception ex)
@@ -289,8 +181,6 @@ namespace PartSource.Automation.Jobs
products = await _shopifyClient.Products.GetPrevious();
}
}
Console.WriteLine($"Finished {partType}");
}
}
}