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; 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(CancellationToken token, params string[] arguments) { IEnumerable products = await _shopifyClient.Products.Get(new Dictionary { { "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("
"); if (parts.Length > 2) { string ul = product.BodyHtml.Substring(0, product.BodyHtml.IndexOf("") + "".Length); string seoData = "
" + parts[1].Substring(0, parts[1].IndexOf("") + "".Length) + "
"; string vehicleIds = new Regex("
").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(); } } } } }