import urllib.request import json import base64 import time import re url_base_kadence = "https://howdoyouconvert.com/wp-json/wp/v2/kadence_element/" creds = base64.b64encode(b"ben:6YGf wVxu gBpz pkqx BGZO lfVP").decode("utf-8") headers = { "Content-Type": "application/json", "Authorization": "Basic " + creds, "User-Agent": "Mozilla/5.0" } def get_robust_js(factor, offset): return f"""""" # Complex mapping (same as before) complex_fixes_by_slug = { "ascii-to-binary": """""", "binary-to-ascii": """""", "binary-to-decimal": """""", "binary-to-hex": """""", "amps-to-volts": """""", "amps-to-watts": """""", "amps-to-kilowatts": """""", "amps-to-kva": """""" } def patch_element(eid, js): print(f"Patching EID {eid}...") data = {"content": js} req = urllib.request.Request(f"{url_base_kadence}{eid}", data=json.dumps(data).encode("utf-8"), headers=headers, method="PUT") try: urllib.request.urlopen(req, timeout=30) print("--> Done") except Exception as e: print(f"--> Error: {e}") # 1. Load factors from registry registry = {} with open("calculators_list.md", "r") as f: for line in f: if "|" not in line or "Calculators Registry" in line or "---" in line: continue parts = [p.strip() for p in line.split("|")] if len(parts) < 6: continue slug = parts[4].replace("-2", "") factor_str = parts[5] registry[slug] = factor_str # 2. Load mapping with open("/tmp/element_mapping.json", "r") as f: mappings = json.load(f) for m in mappings: eid = m['eid'] raw_slug = m['slug'] base_slug = raw_slug.replace("-2", "") if base_slug in complex_fixes_by_slug: patch_element(eid, complex_fixes_by_slug[base_slug]) continue if base_slug in registry: factor_str = registry[base_slug] factor = 1.0 offset = 0.0 if "Linear Offset" in factor_str: match = re.search(r'\((.*?)x \+ (.*?)\)', factor_str) if not match: match = re.search(r'\((.*?)x - (.*?)\)', factor_str) if match: m_val = match.group(1) if m_val == "1.8": factor = 1.8 elif m_val == "5/9": factor = 5/9 b_val = float(match.group(2)) if "-" in factor_str: offset = -b_val else: offset = b_val else: try: factor = float(factor_str) except: print(f"Skipping {eid} ({base_slug}): unparseable factor {factor_str}") continue patch_element(eid, get_robust_js(factor, offset)) else: print(f"Warning: Slug {base_slug} (EID {eid}) not in registry. Skipping.") print("UNIVERSAL PATCH COMPLETE")