feat: Add batch processing scripts for complex calculator creation and verification, and update the calculator list.
This commit is contained in:
349
complex_batch.py
Normal file
349
complex_batch.py
Normal file
@@ -0,0 +1,349 @@
|
||||
import urllib.request
|
||||
import json
|
||||
import base64
|
||||
import time
|
||||
|
||||
url_base_calc = "https://howdoyouconvert.com/wp-json/wp/v2/calculator/"
|
||||
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 (Windows NT 10.0; Win64; x64)"
|
||||
}
|
||||
|
||||
# --- BLUEPRINTS ---
|
||||
|
||||
def get_text_ui(item):
|
||||
slug_raw = item['slug'].replace("-", "")
|
||||
return f"""
|
||||
<!-- wp:kadence/rowlayout {{"uniqueID":"{item['slug']}_outer","bgColor":"#f5f7f9","padding":["2rem","2rem","2rem","2rem"],"borderRadius":["8px","8px","8px","8px"]}} -->
|
||||
<div class="wp-block-kadence-rowlayout alignnone"><div class="kt-row-column-wrap kt-has-1-columns kt-row-layout-equal kt-tab-layout-inherit kt-mobile-layout-row kt-row-valign-top" style="background-color:#f5f7f9;border-radius:8px;padding:2rem;">
|
||||
|
||||
<!-- wp:kadence/column {{"uniqueID":"{item['slug']}_inner"}} -->
|
||||
<div class="wp-block-kadence-column"><div class="kt-inside-inner-col">
|
||||
|
||||
<style>
|
||||
.calc-container-{slug_raw} {{
|
||||
display: grid;
|
||||
grid-template-columns: 1fr;
|
||||
gap: 1.5rem;
|
||||
max-width: 600px;
|
||||
margin: 0 auto;
|
||||
}}
|
||||
.calc-group {{
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}}
|
||||
.calc-label {{
|
||||
font-weight: 600;
|
||||
color: #333333;
|
||||
margin-bottom: 8px;
|
||||
}}
|
||||
.calc-textarea {{
|
||||
width: 100%;
|
||||
padding: 12px;
|
||||
border: 1px solid #ccc;
|
||||
border-radius: 4px;
|
||||
font-size: 1.1rem;
|
||||
min-height: 120px;
|
||||
font-family: monospace;
|
||||
}}
|
||||
</style>
|
||||
|
||||
<div class="calc-container-{slug_raw}">
|
||||
<div class="calc-group">
|
||||
<label class="calc-label" for="input-1">{item['label1']}</label>
|
||||
<textarea id="input-1" class="calc-textarea" placeholder="Enter {item['label1']}..."></textarea>
|
||||
</div>
|
||||
<div class="calc-group">
|
||||
<label class="calc-label" for="input-2">{item['label2']}</label>
|
||||
<textarea id="input-2" class="calc-textarea" placeholder="{item['label2']} result..."></textarea>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div></div>
|
||||
<!-- /wp:kadence/column -->
|
||||
</div></div>
|
||||
<!-- /wp:kadence/rowlayout -->
|
||||
|
||||
{item['seo_text']}
|
||||
"""
|
||||
|
||||
def get_triple_ui(item):
|
||||
slug_raw = item['slug'].replace("-", "")
|
||||
return f"""
|
||||
<!-- wp:kadence/rowlayout {{"uniqueID":"{item['slug']}_outer","bgColor":"#f5f7f9","padding":["2rem","2rem","2rem","2rem"],"borderRadius":["8px","8px","8px","8px"]}} -->
|
||||
<div class="wp-block-kadence-rowlayout alignnone"><div class="kt-row-column-wrap kt-has-1-columns kt-row-layout-equal kt-tab-layout-inherit kt-mobile-layout-row kt-row-valign-top" style="background-color:#f5f7f9;border-radius:8px;padding:2rem;">
|
||||
|
||||
<!-- wp:kadence/column {{"uniqueID":"{item['slug']}_inner"}} -->
|
||||
<div class="wp-block-kadence-column"><div class="kt-inside-inner-col">
|
||||
|
||||
<style>
|
||||
.triple-grid-{slug_raw} {{
|
||||
display: grid;
|
||||
grid-template-columns: repeat(3, 1fr);
|
||||
gap: 1rem;
|
||||
max-width: 800px;
|
||||
margin: 0 auto;
|
||||
}}
|
||||
@media (max-width: 767px) {{
|
||||
.triple-grid-{slug_raw} {{
|
||||
grid-template-columns: 1fr;
|
||||
}}
|
||||
}}
|
||||
.calc-group {{
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}}
|
||||
.calc-label {{
|
||||
font-weight: 600;
|
||||
color: #333333;
|
||||
margin-bottom: 8px;
|
||||
}}
|
||||
.calc-input {{
|
||||
width: 100%;
|
||||
padding: 12px;
|
||||
border: 1px solid #ccc;
|
||||
border-radius: 4px;
|
||||
font-size: 1.2rem;
|
||||
}}
|
||||
</style>
|
||||
|
||||
<div class="triple-grid-{slug_raw}">
|
||||
<div class="calc-group">
|
||||
<label class="calc-label" for="input-1">{item['label1']}</label>
|
||||
<input type="number" id="input-1" class="calc-input" placeholder="0">
|
||||
</div>
|
||||
<div class="calc-group">
|
||||
<label class="calc-label" for="input-2">{item['label2']}</label>
|
||||
<input type="number" id="input-2" class="calc-input" placeholder="0">
|
||||
</div>
|
||||
<div class="calc-group">
|
||||
<label class="calc-label" for="input-3">{item['label3']}</label>
|
||||
<input type="number" id="input-3" class="calc-input" placeholder="0">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p style="text-align: center; margin-top: 1.5rem; color: #666; font-size: 0.9rem;">
|
||||
<em>Enter any two values to calculate the third.</em>
|
||||
</p>
|
||||
|
||||
</div></div>
|
||||
<!-- /wp:kadence/column -->
|
||||
</div></div>
|
||||
<!-- /wp:kadence/rowlayout -->
|
||||
|
||||
{item['seo_text']}
|
||||
"""
|
||||
|
||||
# --- COMPLEX CALCULATOR DATA ---
|
||||
|
||||
complex_items = [
|
||||
{
|
||||
"title": "Binary to ASCII",
|
||||
"slug": "binary-to-ascii",
|
||||
"type": "textual",
|
||||
"label1": "Binary",
|
||||
"label2": "Text (ASCII)",
|
||||
"js_logic": """
|
||||
function convert1() {
|
||||
let val = document.getElementById("input-1").value.replace(/\\s/g, "");
|
||||
let text = "";
|
||||
for (let i = 0; i < val.length; i += 8) {
|
||||
let byte = val.substr(i, 8);
|
||||
text += String.fromCharCode(parseInt(byte, 2));
|
||||
}
|
||||
document.getElementById("input-2").value = text;
|
||||
}
|
||||
function convert2() {
|
||||
let val = document.getElementById("input-2").value;
|
||||
let binary = "";
|
||||
for (let i = 0; i < val.length; i++) {
|
||||
let bin = val[i].charCodeAt(0).toString(2);
|
||||
binary += ("00000000" + bin).slice(-8) + " ";
|
||||
}
|
||||
document.getElementById("input-1").value = binary.trim();
|
||||
}
|
||||
document.getElementById("input-1").addEventListener("input", convert1);
|
||||
document.getElementById("input-2").addEventListener("input", convert2);
|
||||
""",
|
||||
"seo_text": """
|
||||
<!-- wp:paragraph -->
|
||||
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Binary to ASCII Conversion:</strong> Translate binary strings of 0s and 1s back into readable text. Each 8-bit sequence represents a specific character in the ASCII standard.</p>
|
||||
<!-- /wp:paragraph -->
|
||||
"""
|
||||
},
|
||||
{
|
||||
"title": "Binary to Decimal",
|
||||
"slug": "binary-to-decimal",
|
||||
"type": "textual",
|
||||
"label1": "Binary",
|
||||
"label2": "Decimal",
|
||||
"js_logic": """
|
||||
function convert1() {
|
||||
let val = document.getElementById("input-1").value.replace(/\\s/g, "");
|
||||
document.getElementById("input-2").value = parseInt(val, 2).toString(10);
|
||||
}
|
||||
function convert2() {
|
||||
let val = document.getElementById("input-2").value;
|
||||
document.getElementById("input-1").value = parseInt(val, 10).toString(2);
|
||||
}
|
||||
document.getElementById("input-1").addEventListener("input", convert1);
|
||||
document.getElementById("input-2").addEventListener("input", convert2);
|
||||
""",
|
||||
"seo_text": """
|
||||
<!-- wp:paragraph -->
|
||||
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Binary to Decimal:</strong> Quickly convert base-2 binary numbers into their base-10 decimal equivalents used in everyday mathematics.</p>
|
||||
<!-- /wp:paragraph -->
|
||||
"""
|
||||
},
|
||||
{
|
||||
"title": "Binary to Hex",
|
||||
"slug": "binary-to-hex",
|
||||
"type": "textual",
|
||||
"label1": "Binary",
|
||||
"label2": "Hexadecimal",
|
||||
"js_logic": """
|
||||
function convert1() {
|
||||
let val = document.getElementById("input-1").value.replace(/\\s/g, "");
|
||||
document.getElementById("input-2").value = parseInt(val, 2).toString(16).toUpperCase();
|
||||
}
|
||||
function convert2() {
|
||||
let val = document.getElementById("input-2").value;
|
||||
document.getElementById("input-1").value = parseInt(val, 16).toString(2);
|
||||
}
|
||||
document.getElementById("input-1").addEventListener("input", convert1);
|
||||
document.getElementById("input-2").addEventListener("input", convert2);
|
||||
""",
|
||||
"seo_text": """
|
||||
<!-- wp:paragraph -->
|
||||
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Binary to Hex:</strong> Conversion between binary (base-2) and hexadecimal (base-16) is a fundamental skill in computing and low-level programming.</p>
|
||||
<!-- /wp:paragraph -->
|
||||
"""
|
||||
},
|
||||
{
|
||||
"title": "Amps to Watts",
|
||||
"slug": "amps-to-watts",
|
||||
"type": "triple",
|
||||
"label1": "Current (Amps)",
|
||||
"label2": "Voltage (Volts)",
|
||||
"label3": "Power (Watts)",
|
||||
"js_logic": """
|
||||
function solve() {
|
||||
let a = document.getElementById("input-1").value;
|
||||
let v = document.getElementById("input-2").value;
|
||||
let w = document.getElementById("input-3").value;
|
||||
if (a && v && !w) document.getElementById("input-3").value = (a * v).toFixed(2);
|
||||
else if (a && w && !v) document.getElementById("input-2").value = (w / a).toFixed(2);
|
||||
else if (v && w && !a) document.getElementById("input-1").value = (w / v).toFixed(2);
|
||||
}
|
||||
document.getElementById("input-1").addEventListener("input", solve);
|
||||
document.getElementById("input-2").addEventListener("input", solve);
|
||||
document.getElementById("input-3").addEventListener("input", solve);
|
||||
""",
|
||||
"seo_text": """
|
||||
<!-- wp:paragraph -->
|
||||
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Amps to Watts:</strong> Use this calculator to find power (Watts) given current (Amps) and voltage (Volts). It works for DC and single-phase AC circuits.</p>
|
||||
<!-- /wp:paragraph -->
|
||||
"""
|
||||
},
|
||||
{
|
||||
"title": "Amps to Kilowatts",
|
||||
"slug": "amps-to-kilowatts",
|
||||
"type": "triple",
|
||||
"label1": "Current (Amps)",
|
||||
"label2": "Voltage (Volts)",
|
||||
"label3": "Power (Kilowatts)",
|
||||
"js_logic": """
|
||||
function solve() {
|
||||
let a = document.getElementById("input-1").value;
|
||||
let v = document.getElementById("input-2").value;
|
||||
let kw = document.getElementById("input-3").value;
|
||||
if (a && v && !kw) document.getElementById("input-3").value = (a * v / 1000).toFixed(3);
|
||||
else if (a && kw && !v) document.getElementById("input-2").value = (kw * 1000 / a).toFixed(2);
|
||||
else if (v && kw && !a) document.getElementById("input-1").value = (kw * 1000 / v).toFixed(2);
|
||||
}
|
||||
document.getElementById("input-1").addEventListener("input", solve);
|
||||
document.getElementById("input-2").addEventListener("input", solve);
|
||||
document.getElementById("input-3").addEventListener("input", solve);
|
||||
""",
|
||||
"seo_text": """
|
||||
<!-- wp:paragraph -->
|
||||
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Amps to Kilowatts:</strong> Convert current to power in kilowatts. This calculator assumes a power factor of 1.0 (purely resistive load).</p>
|
||||
<!-- /wp:paragraph -->
|
||||
"""
|
||||
},
|
||||
{
|
||||
"title": "Amps to kVA",
|
||||
"slug": "amps-to-kva",
|
||||
"type": "triple",
|
||||
"label1": "Current (Amps)",
|
||||
"label2": "Voltage (Volts)",
|
||||
"label3": "Apparent Power (kVA)",
|
||||
"js_logic": """
|
||||
function solve() {
|
||||
let a = document.getElementById("input-1").value;
|
||||
let v = document.getElementById("input-2").value;
|
||||
let kva = document.getElementById("input-3").value;
|
||||
if (a && v && !kva) document.getElementById("input-3").value = (a * v / 1000).toFixed(3);
|
||||
else if (a && kva && !v) document.getElementById("input-2").value = (kva * 1000 / a).toFixed(2);
|
||||
else if (v && kva && !a) document.getElementById("input-1").value = (kva * 1000 / v).toFixed(2);
|
||||
}
|
||||
document.getElementById("input-1").addEventListener("input", solve);
|
||||
document.getElementById("input-2").addEventListener("input", solve);
|
||||
document.getElementById("input-3").addEventListener("input", solve);
|
||||
""",
|
||||
"seo_text": """
|
||||
<!-- wp:paragraph -->
|
||||
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Amps to kVA:</strong> Calculate apparent power in kVA for single-phase electrical systems. Apparent power is the product of RMS voltage and current.</p>
|
||||
<!-- /wp:paragraph -->
|
||||
"""
|
||||
}
|
||||
]
|
||||
|
||||
# --- EXECUTION LOOP ---
|
||||
|
||||
for item in complex_items:
|
||||
print(f"\\n--- Processing {item['title']} ---")
|
||||
|
||||
if item['type'] == "textual":
|
||||
content_html = get_text_ui(item)
|
||||
elif item['type'] == "triple":
|
||||
content_html = get_triple_ui(item)
|
||||
|
||||
calc_data = {
|
||||
"title": item['title'],
|
||||
"status": "publish",
|
||||
"slug": item['slug'],
|
||||
"content": content_html
|
||||
}
|
||||
|
||||
req = urllib.request.Request(url_base_calc, data=json.dumps(calc_data).encode("utf-8"), headers=headers, method="POST")
|
||||
try:
|
||||
resp = urllib.request.urlopen(req, timeout=30)
|
||||
res = json.loads(resp.read().decode("utf-8"))
|
||||
post_id = res['id']
|
||||
print(f"--> Posted Calculator (ID: {post_id})")
|
||||
|
||||
js_wrapped = f"<script>\\n{item['js_logic']}\\n</script>"
|
||||
kadence_data = {
|
||||
"title": f"JS Logic: {item['title']}",
|
||||
"status": "publish",
|
||||
"content": js_wrapped,
|
||||
"meta": {
|
||||
"_kad_element_hook": "kadence_after_header",
|
||||
"_kad_element_show_conditionals": json.dumps([{"rule": "singular|calculator", "select": "ids", "ids": [post_id], "mustMatch": False}])
|
||||
}
|
||||
}
|
||||
|
||||
req_j = urllib.request.Request(url_base_kadence, data=json.dumps(kadence_data).encode("utf-8"), headers=headers, method="POST")
|
||||
resp_j = urllib.request.urlopen(req_j, timeout=30)
|
||||
print(f"--> Posted JS Hook")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
if hasattr(e, 'read'): print(e.read().decode('utf-8'))
|
||||
|
||||
time.sleep(1)
|
||||
Reference in New Issue
Block a user