317 lines
11 KiB
Python
317 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>"""
|
|
|
|
# Manual mapping for complex ones as they don't follow the 1:1 format in the list
|
|
complex_fixes = {
|
|
140: """<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>""",
|
|
152: """<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>""",
|
|
154: """<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>""",
|
|
156: """<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>""",
|
|
142: """<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>""",
|
|
158: """<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>""",
|
|
160: """<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>""",
|
|
162: """<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}")
|
|
|
|
# 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)
|