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"""""" # Manual mapping for complex ones as they don't follow the 1:1 format in the list complex_fixes = { 140: """""", 152: """""", 154: """""", 156: """""", 142: """""", 158: """""", 160: """""", 162: """""" } 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}") # Parse registry for standard factors with open("calculators_list.md", "r") as f: lines = f.readlines() for line in lines: if "|" not in line or "Calculator Name" in line or ":---" in line: continue parts = [p.strip() for p in line.split("|")] if len(parts) < 6: continue eid = parts[3] if not eid.isdigit(): continue eid = int(eid) factor_str = parts[5] # Only patch recent ones (Batch 3-6) if eid < 140 and eid != 108 and eid != 110 and eid != 112 and eid != 114 and eid != 116 and eid != 118 and eid != 120 and eid != 122 and eid != 124 and eid != 126 and eid != 128 and eid != 130 and eid != 132 and eid != 134 and eid != 136 and eid != 138: continue if eid in complex_fixes: patch_element(eid, complex_fixes[eid]) else: # standard 1:1 or offset factor = 1.0 offset = 0.0 if "Linear Offset" in factor_str: # Linear Offset (1.8x + 32) # Linear Offset (5/9x - 17.778) m = re.search(r'\((.*?)x \+ (.*?)\)', factor_str) if not m: m = re.search(r'\((.*?)x - (.*?)\)', factor_str) if m: m_val = m.group(1) if m_val == "1.8": factor = 1.8 elif m_val == "5/9": factor = 5/9 b_val = float(m.group(2)) if "-" in factor_str: offset = -b_val else: offset = b_val else: print(f"Skipping {eid}: unparseable offset {factor_str}") continue else: try: factor = float(factor_str) except: print(f"Skipping {eid}: unparseable factor {factor_str}") continue patch_element(eid, get_robust_js(factor, offset)) time.sleep(1) # Also patch Batch 6 explicitly if not in registry yet (I check previous command status) # Batch 6 Element IDs: 234, 236, 238, 240 batch_6_extra = [ (234, 0.0321507466, 0), # Grams to Apothecary Ounces (236, 5.0, 0), # Grams to Carats (238, 15.4323584, 0), # Grams to Grains (240, 0.745699872, 0) # Horsepower to Kilowatts ] for eid, f, o in batch_6_extra: if eid not in complex_fixes: patch_element(eid, get_robust_js(f, o)) time.sleep(1)