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

255
batch_4.py Normal file
View File

@@ -0,0 +1,255 @@
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_4 = [
{
"title": "Days to Weeks",
"slug": "days-to-weeks",
"label1": "Days",
"label2": "Weeks",
"factor": 1/7,
"offset": 0,
"seo_text": """
<!-- wp:paragraph -->
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Time Conversion - Days to Weeks:</strong> Weeks are a standard unit of time used across most calendars globally. One week consists of seven days. This calculator helps you convert daily counts into standard weeks.</p>
<!-- /wp:paragraph -->
"""
},
{
"title": "Days to Years",
"slug": "days-to-years",
"label1": "Days",
"label2": "Years",
"factor": 1/365.25,
"offset": 0,
"seo_text": """
<!-- wp:paragraph -->
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Time Conversion - Days to Years:</strong> Converting days into years is important for astronomical and logistical calculations. This calculator uses the standard Gregorian year length of 365.25 days to account for leap years.</p>
<!-- /wp:paragraph -->
"""
},
{
"title": "Degrees to Mils",
"slug": "degrees-to-mils",
"label1": "Degrees (°)",
"label2": "Mils",
"factor": 17.777777778,
"offset": 0,
"seo_text": """
<!-- wp:paragraph -->
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Angle Measurement - Degrees to Mils:</strong> While degrees are common in everyday use, mils (milliradians) are frequently used in military and engineering contexts for precise targeting and measurement.</p>
<!-- /wp:paragraph -->
"""
},
{
"title": "Degrees to Radians",
"slug": "degrees-to-radians",
"label1": "Degrees (°)",
"label2": "Radians (rad)",
"factor": 0.0174532925,
"offset": 0,
"seo_text": """
<!-- wp:paragraph -->
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Mathematical Precision - Degrees to Radians:</strong> Radians are the standard unit of angular measure used in mathematics, physics, and computer science. This calculator provides a quick conversion from common degrees.</p>
<!-- /wp:paragraph -->
"""
},
{
"title": "Dynes to Newtons",
"slug": "dynes-to-newtons",
"label1": "Dynes",
"label2": "Newtons (N)",
"factor": 1e-5,
"offset": 0,
"seo_text": """
<!-- wp:paragraph -->
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Force Measurement - Dynes to Newtons:</strong> The dyne is a unit of force in the cgs (centimeter-gram-second) system, while the newton is the standard SI unit. One newton is exactly 100,000 dynes.</p>
<!-- /wp:paragraph -->
"""
},
{
"title": "Ergs to Joules",
"slug": "ergs-to-joules",
"label1": "Ergs",
"label2": "Joules (J)",
"factor": 1e-7,
"offset": 0,
"seo_text": """
<!-- wp:paragraph -->
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Energy Conversion - Ergs to Joules:</strong> The erg is a legacy unit of energy and mechanical work in the cgs system. In modern science, the joule is the preferred SI unit for energy.</p>
<!-- /wp:paragraph -->
"""
},
{
"title": "Feet to Meters",
"slug": "feet-to-meters",
"label1": "Feet (ft)",
"label2": "Meters (m)",
"factor": 0.3048,
"offset": 0,
"seo_text": """
<!-- wp:paragraph -->
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Length Conversion - Feet to Meters:</strong> Feet are part of the imperial and US customary systems, while meters are the core metric unit of length. One foot is defined internationally as exactly 0.3048 meters.</p>
<!-- /wp:paragraph -->
"""
},
{
"title": "Foot-Pounds to Newton-Meters",
"slug": "foot-pounds-to-newton-meters",
"label1": "Foot-Pounds (ft-lb)",
"label2": "Newton-Meters (N-m)",
"factor": 1.35581795,
"offset": 0,
"seo_text": """
<!-- wp:paragraph -->
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Torque Conversion:</strong> Foot-pounds and newton-meters are units used to measure torque or work. This calculator is particularly useful for mechanics and engineers.</p>
<!-- /wp:paragraph -->
"""
},
{
"title": "Gallons to Liters",
"slug": "gallons-to-liters",
"label1": "Gallons (US)",
"label2": "Liters (L)",
"factor": 3.78541178,
"offset": 0,
"seo_text": """
<!-- wp:paragraph -->
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Volume Conversion - Gallons to Liters:</strong> Converting between US gallons and metric liters is a common requirement in automotive, culinary, and scientific fields.</p>
<!-- /wp:paragraph -->
"""
},
{
"title": "Gigabytes to Megabytes",
"slug": "gigabytes-to-megabytes",
"label1": "Gigabytes (GB)",
"label2": "Megabytes (MB)",
"factor": 1000.0,
"offset": 0,
"seo_text": """
<!-- wp:paragraph -->
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Digital Storage - GB to MB:</strong> This calculator uses the standard decimal (base-10) convention used by hard drive manufacturers and memory card providers, where 1 GB = 1,000 MB.</p>
<!-- /wp:paragraph -->
"""
}
]
for item in batch_4:
print(f"\\n--- Processing {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 Calculator
req_c = urllib.request.Request(url_base_calc, data=json.dumps(calc_data).encode("utf-8"), headers=headers, method="POST")
try:
resp_c = urllib.request.urlopen(req_c, timeout=30)
res_c = json.loads(resp_c.read().decode("utf-8"))
post_id = res_c['id']
print(f"--> Posted {item['title']} (ID: {post_id})")
# JS Logic with robust event handling
offset = item.get('offset', 0)
js_wrapped = f"""<script>
window.addEventListener('DOMContentLoaded', (event) => {{
const inp1 = document.getElementById("input-1");
const inp2 = document.getElementById("input-2");
function calculate1() {{
let val1 = parseFloat(inp1.value);
if(isNaN(val1)) {{
inp2.value = "";
return;
}}
let val2 = val1 * {item['factor']} + {offset};
inp2.value = parseFloat(val2.toFixed(8));
}}
function calculate2() {{
let val2 = parseFloat(inp2.value);
if(isNaN(val2)) {{
inp1.value = "";
return;
}}
let val1 = (val2 - {offset}) / {item['factor']};
inp1.value = parseFloat(val1.toFixed(8));
}}
inp1.addEventListener("input", calculate1);
inp2.addEventListener("input", calculate2);
const urlParams = new URLSearchParams(window.location.search);
const v1 = urlParams.get('v1');
const v2 = urlParams.get('v2');
if (v1 !== null && !isNaN(parseFloat(v1))) {{
inp1.value = v1;
calculate1();
}} else if (v2 !== null && !isNaN(parseFloat(v2))) {{
inp2.value = v2;
calculate2();
}}
}});
</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")
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)
print("\\n--- BATCH 4 COMPLETE ---")