feat: Add batch processing scripts for complex calculator creation and verification, and update the calculator list.

This commit is contained in:
Ben
2026-02-21 00:29:54 -08:00
parent ad0660adba
commit 569b11d2ea
12 changed files with 1988 additions and 48 deletions

258
batch_3.py Normal file
View File

@@ -0,0 +1,258 @@
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)"
}
batch_3 = [
{
"title": "Carats to Grams",
"slug": "carats-to-grams",
"label1": "Carats (ct)",
"label2": "Grams (g)",
"factor": 0.2,
"offset": 0,
"seo_text": """
<!-- wp:paragraph -->
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Understanding Carats and Grams:</strong> The carat is a unit of mass used for measuring gemstones and pearls. One carat is defined as exactly 200 milligrams (0.2 grams). The gram is the base unit of mass in the metric system.</p>
<!-- /wp:paragraph -->
"""
},
{
"title": "Celsius to Fahrenheit",
"slug": "celsius-to-fahrenheit",
"label1": "Celsius (°C)",
"label2": "Fahrenheit (°F)",
"factor": 1.8,
"offset": 32,
"seo_text": """
<!-- wp:paragraph -->
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Celsius and Fahrenheit Scales:</strong> These two temperature scales are the most commonly used worldwide. Celsius is part of the metric system, while Fahrenheit is primarily used in the United States and some Caribbean territories.</p>
<!-- /wp:paragraph -->
"""
},
{
"title": "Centimeters to Inches",
"slug": "centimeters-to-inches",
"label1": "Centimeters (cm)",
"label2": "Inches (in)",
"factor": 0.393700787,
"offset": 0,
"seo_text": """
<!-- wp:paragraph -->
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Length Conversion:</strong> Centimeters are a metric unit of length, while inches are part of the imperial and US customary systems. One inch is defined exactly as 2.54 centimeters.</p>
<!-- /wp:paragraph -->
"""
},
{
"title": "CFS to CMS",
"slug": "cfs-to-cms",
"label1": "Cubic Feet per Second (CFS)",
"label2": "Cubic Meters per Second (CMS)",
"factor": 0.0283168466,
"offset": 0,
"seo_text": """
<!-- wp:paragraph -->
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Volumetric Flow Rate:</strong> CFS and CMS are used to measure the volume of fluid (usually water) passing through a point per unit of time. This is critical in hydrology and civil engineering.</p>
<!-- /wp:paragraph -->
"""
},
{
"title": "CMS to CFS",
"slug": "cms-to-cfs",
"label1": "Cubic Meters per Second (CMS)",
"label2": "Cubic Feet per Second (CFS)",
"factor": 35.3146667,
"offset": 0,
"seo_text": """
<!-- wp:paragraph -->
<p style="margin-top: 2rem; line-height: 1.6;"><strong>CMS to CFS Conversion:</strong> Converting from metric volumetric flow (CMS) to imperial (CFS) is essential for international engineering projects and environmental monitoring.</p>
<!-- /wp:paragraph -->
"""
},
{
"title": "Coulomb per Kilogram to Roentgen",
"slug": "coulomb-per-kilogram-to-roentgen",
"label1": "Coulombs per Kilogram (C/kg)",
"label2": "Roentgen (R)",
"factor": 3875.96899,
"offset": 0,
"seo_text": """
<!-- wp:paragraph -->
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Radiation Exposure:</strong> The coulomb per kilogram and the roentgen are units used to measure ionizing radiation exposure in air. The C/kg is the SI unit, while the roentgen is a legacy unit.</p>
<!-- /wp:paragraph -->
"""
},
{
"title": "Cups to Milliliters",
"slug": "cups-to-milliliters",
"label1": "Cups (US)",
"label2": "Milliliters (ml)",
"factor": 236.588237,
"offset": 0,
"seo_text": """
<!-- wp:paragraph -->
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Kitchen Conversion:</strong> Converting US cups to milliliters is a common task in cooking and baking. A standard US cup is approximately 236.6 ml.</p>
<!-- /wp:paragraph -->
"""
},
{
"title": "Curie to Becquerel",
"slug": "curie-to-becquerel",
"label1": "Curies (Ci)",
"label2": "Becquerels (Bq)",
"factor": 3.7e10,
"offset": 0,
"seo_text": """
<!-- wp:paragraph -->
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Radioactivity Units:</strong> The curie is an older, non-SI unit, while the becquerel is the modern SI unit. One curie is exactly 37 billion becquerels.</p>
<!-- /wp:paragraph -->
"""
},
{
"title": "Daltons to AMU",
"slug": "daltons-to-amu",
"label1": "Daltons (Da)",
"label2": "Atomic Mass Units (u)",
"factor": 1.0,
"offset": 0,
"seo_text": """
<!-- wp:paragraph -->
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Atomic Mass Conversion:</strong> The dalton and the unified atomic mass unit (u) are effectively identical units used to express the mass of atoms and molecules.</p>
<!-- /wp:paragraph -->
"""
},
{
"title": "Days to Hours",
"slug": "days-to-hours",
"label1": "Days",
"label2": "Hours",
"factor": 24.0,
"offset": 0,
"seo_text": """
<!-- wp:paragraph -->
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Time Computation:</strong> In our standard 24-hour day system, converting days to hours is done by multiplying the day count by 24.</p>
<!-- /wp:paragraph -->
"""
}
]
for item in batch_3:
print(f"\n--- Constructing UI Block for {item['title']} ---")
slug_raw = item['slug'].replace("-", "")
content_html = 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>.kb-row-layout-id_{slug_raw}_row > .kt-row-column-wrap{{align-content:start;}}:where(.kb-row-layout-id_{slug_raw}_row > .kt-row-column-wrap) > .wp-block-kadence-column{{justify-content:start;}}.kb-row-layout-id_{slug_raw}_row > .kt-row-column-wrap{{column-gap:var(--global-kb-gap-md, 2rem);row-gap:var(--global-kb-gap-md, 2rem);max-width:600px;margin-left:auto;margin-right:auto;grid-template-columns:repeat(2, minmax(0, 1fr));}}.kb-row-layout-id_{slug_raw}_row > .kt-row-layout-overlay{{opacity:0.30;}}@media all and (max-width: 1024px){{.kb-row-layout-id_{slug_raw}_row > .kt-row-column-wrap{{grid-template-columns:repeat(2, minmax(0, 1fr));}}}}@media all and (max-width: 767px){{.kb-row-layout-id_{slug_raw}_row > .kt-row-column-wrap{{grid-template-columns:minmax(0, 1fr);}}}}</style><div class="kb-row-layout-wrap kb-row-layout-id_{slug_raw}_row aligncenter wp-block-kadence-rowlayout"><div class="kt-row-column-wrap kt-has-2-columns kt-row-layout-equal kt-tab-layout-inherit kt-mobile-layout-row kt-row-valign-top">
<div class="wp-block-kadence-column kadence-column_{slug_raw}_col1"><div class="kt-inside-inner-col">
<label for="input-1" style="font-weight: 600; color: #333333; margin-bottom: 8px; display: block;">{item['label1']}</label>
<input type="number" id="input-1" class="calc-input" placeholder="0" style="width:100%; padding: 12px; border: 1px solid #ccc; border-radius: 4px; font-size: 1.2rem;">
</div></div>
<div class="wp-block-kadence-column kadence-column_{slug_raw}_col2"><div class="kt-inside-inner-col">
<label for="input-2" style="font-weight: 600; color: #333333; margin-bottom: 8px; display: block;">{item['label2']}</label>
<input type="number" id="input-2" class="calc-input" placeholder="0" style="width:100%; padding: 12px; border: 1px solid #ccc; border-radius: 4px; font-size: 1.2rem;">
</div></div></div></div>
</div></div>
<!-- /wp:kadence/column -->
</div></div>
<!-- /wp:kadence/rowlayout -->
{item['seo_text']}
"""
calc_data = {
"title": item['title'],
"status": "publish",
"slug": item['slug'],
"content": content_html,
"format": "standard"
}
# Post the calculator
req_c = urllib.request.Request(url_base_calc, data=json.dumps(calc_data).encode("utf-8"), headers=headers, method="POST")
post_id = None
try:
resp_c = urllib.request.urlopen(req_c, timeout=30)
r_json = json.loads(resp_c.read().decode("utf-8"))
post_id = r_json['id']
print(f"--> Posted {item['title']} (ID: {post_id})")
except Exception as e:
print("Error creating calculator:", e)
if hasattr(e, 'read'): print(e.read().decode('utf-8'))
continue
# JS Logic with Offset Support: v2 = v1 * factor + offset
offset = item.get('offset', 0)
js_code = f"""
<script>
function calculate1() {{
let val1 = parseFloat(document.getElementById("input-1").value);
if(isNaN(val1)) {{
document.getElementById("input-2").value = "";
return;
}}
let val2 = val1 * {item['factor']} + {offset};
document.getElementById("input-2").value = parseFloat(val2.toFixed(8));
}}
function calculate2() {{
let val2 = parseFloat(document.getElementById("input-2").value);
if(isNaN(val2)) {{
document.getElementById("input-1").value = "";
return;
}}
let val1 = (val2 - {offset}) / {item['factor']};
document.getElementById("input-1").value = parseFloat(val1.toFixed(8));
}}
document.getElementById("input-1").addEventListener("input", calculate1);
document.getElementById("input-2").addEventListener("input", calculate2);
window.addEventListener('DOMContentLoaded', (event) => {{
const urlParams = new URLSearchParams(window.location.search);
const v1 = urlParams.get('v1');
const v2 = urlParams.get('v2');
if (v1 !== null && !isNaN(v1)) {{
document.getElementById("input-1").value = v1;
calculate1();
}} else if (v2 !== null && !isNaN(v2)) {{
document.getElementById("input-2").value = v2;
calculate2();
}}
}});
</script>
"""
kadence_data = {
"title": f"JS Logic: {item['title']}",
"status": "publish",
"content": js_code,
"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")
try:
urllib.request.urlopen(req_j, timeout=30)
print(f"--> Posted JS hook")
except Exception as e:
print("Error creating JS Element:", e)
time.sleep(1)
print("\n--- BATCH 3 COMPLETE ---")