305 lines
11 KiB
Python
305 lines
11 KiB
Python
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"""<script>
|
|
(function() {{
|
|
function init() {{
|
|
var i1 = document.getElementById("input-1");
|
|
var i2 = document.getElementById("input-2");
|
|
if (!i1 || !i2) {{
|
|
if (window.initRetries === undefined) window.initRetries = 0;
|
|
if (window.initRetries < 50) {{
|
|
window.initRetries++;
|
|
setTimeout(init, 100);
|
|
}}
|
|
return;
|
|
}}
|
|
|
|
i1.oninput = function() {{
|
|
var v = parseFloat(i1.value);
|
|
if (isNaN(v)) {{ i2.value = ""; return; }}
|
|
i2.value = parseFloat((v * {factor} + {offset}).toFixed(8));
|
|
}};
|
|
|
|
i2.oninput = function() {{
|
|
var v = parseFloat(i2.value);
|
|
if (isNaN(v)) {{ i1.value = ""; return; }}
|
|
i1.value = parseFloat(((v - {offset}) / {factor}).toFixed(8));
|
|
}};
|
|
|
|
var p = new URLSearchParams(window.location.search);
|
|
if (p.has('v1')) {{ i1.value = p.get('v1'); i1.oninput(); }}
|
|
else if (p.has('v2')) {{ i2.value = p.get('v2'); i2.oninput(); }}
|
|
}}
|
|
init();
|
|
}})();
|
|
</script>"""
|
|
|
|
# Complex mapping (same as before)
|
|
complex_fixes_by_slug = {
|
|
"ascii-to-binary": """<script>
|
|
(function() {
|
|
function init() {
|
|
var i1 = document.getElementById("input-1");
|
|
var i2 = document.getElementById("input-2");
|
|
if (!i1 || !i2) { setTimeout(init, 100); return; }
|
|
i1.oninput = function() {
|
|
let val = i1.value;
|
|
let binary = "";
|
|
for (let i = 0; i < val.length; i++) {
|
|
let bin = val[i].charCodeAt(0).toString(2);
|
|
binary += ("00000000" + bin).slice(-8) + " ";
|
|
}
|
|
i2.value = binary.trim();
|
|
};
|
|
i2.oninput = function() {
|
|
let val = i2.value.replace(/\\s/g, "");
|
|
let text = "";
|
|
for (let i = 0; i < val.length; i += 8) {
|
|
let byte = val.substr(i, 8);
|
|
if (byte.length === 8) text += String.fromCharCode(parseInt(byte, 2));
|
|
}
|
|
i1.value = text;
|
|
};
|
|
}
|
|
init();
|
|
})();
|
|
</script>""",
|
|
"binary-to-ascii": """<script>
|
|
(function() {
|
|
function init() {
|
|
var i1 = document.getElementById("input-1");
|
|
var i2 = document.getElementById("input-2");
|
|
if (!i1 || !i2) { setTimeout(init, 100); return; }
|
|
i2.oninput = function() {
|
|
let val = i2.value;
|
|
let binary = "";
|
|
for (let i = 0; i < val.length; i++) {
|
|
let bin = val[i].charCodeAt(0).toString(2);
|
|
binary += ("00000000" + bin).slice(-8) + " ";
|
|
}
|
|
i1.value = binary.trim();
|
|
};
|
|
i1.oninput = function() {
|
|
let val = i1.value.replace(/\\s/g, "");
|
|
let text = "";
|
|
for (let i = 0; i < val.length; i += 8) {
|
|
let byte = val.substr(i, 8);
|
|
if (byte.length === 8) text += String.fromCharCode(parseInt(byte, 2));
|
|
}
|
|
i2.value = text;
|
|
};
|
|
}
|
|
init();
|
|
})();
|
|
</script>""",
|
|
"binary-to-decimal": """<script>
|
|
(function() {
|
|
function init() {
|
|
var i1 = document.getElementById("input-1");
|
|
var i2 = document.getElementById("input-2");
|
|
if (!i1 || !i2) { setTimeout(init, 100); return; }
|
|
i1.oninput = function() {
|
|
let val = i1.value.replace(/\\s/g, "");
|
|
if (val) i2.value = parseInt(val, 2).toString(10);
|
|
else i2.value = "";
|
|
};
|
|
i2.oninput = function() {
|
|
let val = i2.value;
|
|
if (val) i1.value = parseInt(val, 10).toString(2);
|
|
else i1.value = "";
|
|
};
|
|
}
|
|
init();
|
|
})();
|
|
</script>""",
|
|
"binary-to-hex": """<script>
|
|
(function() {
|
|
function init() {
|
|
var i1 = document.getElementById("input-1");
|
|
var i2 = document.getElementById("input-2");
|
|
if (!i1 || !i2) { setTimeout(init, 100); return; }
|
|
i1.oninput = function() {
|
|
let val = i1.value.replace(/\\s/g, "");
|
|
if (val) i2.value = parseInt(val, 2).toString(16).toUpperCase();
|
|
else i2.value = "";
|
|
};
|
|
i2.oninput = function() {
|
|
let val = i2.value;
|
|
if (val) i1.value = parseInt(val, 16).toString(2);
|
|
else i1.value = "";
|
|
};
|
|
}
|
|
init();
|
|
})();
|
|
</script>""",
|
|
"amps-to-volts": """<script>
|
|
(function() {
|
|
function init() {
|
|
var a = document.getElementById("input-1");
|
|
var v = document.getElementById("input-2");
|
|
var w = document.getElementById("input-3");
|
|
if (!a || !v || !w) { setTimeout(init, 100); return; }
|
|
function solve(lastId) {
|
|
let av = parseFloat(a.value), vv = parseFloat(v.value), wv = parseFloat(w.value);
|
|
if (lastId === '1' || lastId === '2') {
|
|
if (!isNaN(av) && !isNaN(vv)) w.value = parseFloat((av * vv).toFixed(3));
|
|
} else if (lastId === '3') {
|
|
if (!isNaN(wv)) {
|
|
if (!isNaN(av) && av !== 0) v.value = parseFloat((wv / av).toFixed(2));
|
|
else if (!isNaN(vv) && vv !== 0) a.value = parseFloat((wv / vv).toFixed(2));
|
|
}
|
|
}
|
|
}
|
|
a.oninput = () => solve('1'); v.oninput = () => solve('2'); w.oninput = () => solve('3');
|
|
}
|
|
init();
|
|
})();
|
|
</script>""",
|
|
"amps-to-watts": """<script>
|
|
(function() {
|
|
function init() {
|
|
var a = document.getElementById("input-1");
|
|
var v = document.getElementById("input-2");
|
|
var w = document.getElementById("input-3");
|
|
if (!a || !v || !w) { setTimeout(init, 100); return; }
|
|
function solve(lastId) {
|
|
let av = parseFloat(a.value), vv = parseFloat(v.value), wv = parseFloat(w.value);
|
|
if (lastId === '1' || lastId === '2') {
|
|
if (!isNaN(av) && !isNaN(vv)) w.value = parseFloat((av * vv).toFixed(3));
|
|
} else if (lastId === '3') {
|
|
if (!isNaN(wv)) {
|
|
if (!isNaN(av) && av !== 0) v.value = parseFloat((wv / av).toFixed(2));
|
|
else if (!isNaN(vv) && vv !== 0) a.value = parseFloat((wv / vv).toFixed(2));
|
|
}
|
|
}
|
|
}
|
|
a.oninput = () => solve('1'); v.oninput = () => solve('2'); w.oninput = () => solve('3');
|
|
}
|
|
init();
|
|
})();
|
|
</script>""",
|
|
"amps-to-kilowatts": """<script>
|
|
(function() {
|
|
function init() {
|
|
var a = document.getElementById("input-1");
|
|
var v = document.getElementById("input-2");
|
|
var w = document.getElementById("input-3");
|
|
if (!a || !v || !w) { setTimeout(init, 100); return; }
|
|
function solve(lastId) {
|
|
let av = parseFloat(a.value), vv = parseFloat(v.value), wv = parseFloat(w.value);
|
|
if (lastId === '1' || lastId === '2') {
|
|
if (!isNaN(av) && !isNaN(vv)) w.value = parseFloat((av * vv / 1000).toFixed(4));
|
|
} else if (lastId === '3') {
|
|
if (!isNaN(wv)) {
|
|
if (!isNaN(av) && av !== 0) v.value = parseFloat((wv * 1000 / av).toFixed(2));
|
|
else if (!isNaN(vv) && vv !== 0) a.value = parseFloat((wv * 1000 / vv).toFixed(2));
|
|
}
|
|
}
|
|
}
|
|
a.oninput = () => solve('1'); v.oninput = () => solve('2'); w.oninput = () => solve('3');
|
|
}
|
|
init();
|
|
})();
|
|
</script>""",
|
|
"amps-to-kva": """<script>
|
|
(function() {
|
|
function init() {
|
|
var a = document.getElementById("input-1");
|
|
var v = document.getElementById("input-2");
|
|
var w = document.getElementById("input-3");
|
|
if (!a || !v || !w) { setTimeout(init, 100); return; }
|
|
function solve(lastId) {
|
|
let av = parseFloat(a.value), vv = parseFloat(v.value), wv = parseFloat(w.value);
|
|
if (lastId === '1' || lastId === '2') {
|
|
if (!isNaN(av) && !isNaN(vv)) w.value = parseFloat((av * vv / 1000).toFixed(4));
|
|
} else if (lastId === '3') {
|
|
if (!isNaN(wv)) {
|
|
if (!isNaN(av) && av !== 0) v.value = parseFloat((wv * 1000 / av).toFixed(2));
|
|
else if (!isNaN(vv) && vv !== 0) a.value = parseFloat((wv * 1000 / vv).toFixed(2));
|
|
}
|
|
}
|
|
}
|
|
a.oninput = () => solve('1'); v.oninput = () => solve('2'); w.oninput = () => solve('3');
|
|
}
|
|
init();
|
|
})();
|
|
</script>"""
|
|
}
|
|
|
|
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")
|