feat: Add new calculator batches (6-10) and associated deployment/verification scripts, and update the calculator list.
This commit is contained in:
146
batch_9.py
Normal file
146
batch_9.py
Normal file
@@ -0,0 +1,146 @@
|
||||
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"
|
||||
}
|
||||
|
||||
batch_9 = [
|
||||
{"title": "Meters per second to Feet per second", "slug": "meters-per-second-to-feet-per-second", "v1": "m/s", "v2": "ft/s", "factor": 3.28084, "desc": "Meters per second is the SI unit of speed. Feet per second is commonly used in aerospace and ballistics in the United States."},
|
||||
{"title": "Meters per second to Miles per hour", "slug": "meters-per-second-to-miles-per-hour", "v1": "m/s", "v2": "mph", "factor": 2.23694, "desc": "This conversion is vital for understanding scientific measurements in terms of everyday vehicle speeds used in the USA and UK."},
|
||||
{"title": "Meters per second to Yards per second", "slug": "meters-per-second-to-yards-per-second", "v1": "m/s", "v2": "yd/s", "factor": 1.09361, "desc": "Meters and yards are nearly equal in length, but the conversion is necessary for sports and construction where exact yardage is required."},
|
||||
{"title": "Micrograms to Milligrams", "slug": "micrograms-to-milligrams", "v1": "µg", "v2": "mg", "factor": 0.001, "desc": "Both units measure very small masses. One milligram contains one thousand micrograms, a critical distinction in pharmacy and biochemistry."},
|
||||
{"title": "Micrometers to Millimeters", "slug": "micrometers-to-millimeters", "v1": "µm", "v2": "mm", "factor": 0.001, "desc": "The micrometer, often called the micron, is 1/1000th of a millimeter. It is used to measure the thickness of human hair or paper."},
|
||||
{"title": "Milligrams to Micrograms", "slug": "milligrams-to-micrograms", "v1": "mg", "v2": "µg", "factor": 1000.0, "desc": "Standard conversion for high-potency supplements and medicines where doses are often specified in micrograms."},
|
||||
{"title": "Milliliters to Liters", "slug": "milliliters-to-liters", "v1": "ml", "v2": "l", "factor": 0.001, "desc": "The milliliter is equal to one cubic centimeter. One thousand milliliters make up one standard liter."},
|
||||
{"title": "Milliliters to Fluid Ounces", "slug": "milliliters-to-fluid-ounces", "v1": "ml", "v2": "fl oz", "factor": 0.033814, "desc": "A common conversion for beverage containers and nutrition labels, translating metric milliliters to US customary fluid ounces."},
|
||||
{"title": "Millimeters to Centimeters", "slug": "millimeters-to-centimeters", "v1": "mm", "v2": "cm", "factor": 0.1, "desc": "One centimeter is exactly ten millimeters. This simple metric shift is fundamental in engineering and design."},
|
||||
{"title": "Millimeters to Inches", "slug": "millimeters-to-inches", "v1": "mm", "v2": "in", "factor": 0.0393701, "desc": "One inch is exactly 25.4 millimeters. This conversion is the bridge between metric and imperial precision engineering."}
|
||||
]
|
||||
|
||||
def check_exists(slug):
|
||||
req = urllib.request.Request(f"{url_base_calc}?slug={slug}", headers=headers)
|
||||
try:
|
||||
resp = urllib.request.urlopen(req, timeout=30)
|
||||
data = json.loads(resp.read().decode("utf-8"))
|
||||
if data: return data[0]['id']
|
||||
except: pass
|
||||
return None
|
||||
|
||||
def get_element_id_for_post(post_id):
|
||||
req_e = urllib.request.Request(f"{url_base_kadence}?per_page=100", headers=headers)
|
||||
try:
|
||||
resp_e = urllib.request.urlopen(req_e, timeout=30)
|
||||
elements = json.loads(resp_e.read().decode("utf-8"))
|
||||
for e in elements:
|
||||
cond = e.get('meta', {}).get('_kad_element_show_conditionals', '')
|
||||
if str(post_id) in cond:
|
||||
return e['id']
|
||||
except: pass
|
||||
return None
|
||||
|
||||
for item in batch_9:
|
||||
print(f"\n--- Processing {item['title']} ---")
|
||||
|
||||
slug = item['slug']
|
||||
unique_id = slug.replace('-', '_')
|
||||
calc_html = f"""
|
||||
<!-- wp:kadence/rowlayout {{"uniqueID":"{unique_id}_row","columns":1,"colLayout":"equal","maxWidth":600,"bgColor":"#f5f7f9","borderRadius":8,"padding":[32,32,32,32],"marginUnit":"px"}} -->
|
||||
<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":"{unique_id}_col"}} -->
|
||||
<div class="wp-block-kadence-column"><div class="kt-inside-inner-col">
|
||||
|
||||
<!-- wp:kadence/rowlayout {{"uniqueID":"{unique_id}_inner_row","columns":2,"colLayout":"equal","maxWidth":600,"marginUnit":"px"}} -->
|
||||
<div class="kb-row-layout-wrap kb-row-layout-id_{unique_id}_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_{unique_id}_col1"><div class="kt-inside-inner-col">
|
||||
<label for="input-1" style="font-weight: 600; color: #333333; margin-bottom: 8px; display: block;">{item['v1']}</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_{unique_id}_col2"><div class="kt-inside-inner-col">
|
||||
<label for="input-2" style="font-weight: 600; color: #333333; margin-bottom: 8px; display: block;">{item['v2']}</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 -->
|
||||
|
||||
<p style="margin-top: 2rem; line-height: 1.6;">{item['desc']}</p>
|
||||
"""
|
||||
|
||||
existing_id = check_exists(slug)
|
||||
if existing_id:
|
||||
print(f"--> Updating existing calculator (ID: {existing_id})")
|
||||
calc_data = {"content": calc_html, "title": item['title']}
|
||||
req_c = urllib.request.Request(f"{url_base_calc}/{existing_id}", data=json.dumps(calc_data).encode("utf-8"), headers=headers, method="PUT")
|
||||
post_id = existing_id
|
||||
else:
|
||||
print(f"--> Creating new calculator")
|
||||
calc_data = {"title": item['title'], "slug": slug, "status": "publish", "content": calc_html}
|
||||
req_c = urllib.request.Request(url_base_calc, data=json.dumps(calc_data).encode("utf-8"), headers=headers, method="POST")
|
||||
resp_c = urllib.request.urlopen(req_c, timeout=30)
|
||||
post_id = json.loads(resp_c.read().decode("utf-8"))['id']
|
||||
|
||||
# Robust JS Logic
|
||||
js_logic = 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 * {item['factor']}).toFixed(8));
|
||||
}};
|
||||
i2.oninput = function() {{
|
||||
var v = parseFloat(i2.value);
|
||||
if (isNaN(v)) {{ i1.value = ""; return; }}
|
||||
i1.value = parseFloat((v / {item['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>"""
|
||||
|
||||
element_id = get_element_id_for_post(post_id)
|
||||
if element_id:
|
||||
print(f"--> Updating existing JS Logic element (ID: {element_id})")
|
||||
kadence_data = {"content": js_logic}
|
||||
req_j = urllib.request.Request(f"{url_base_kadence}/{element_id}", data=json.dumps(kadence_data).encode("utf-8"), headers=headers, method="PUT")
|
||||
else:
|
||||
print(f"--> Creating new JS Logic element")
|
||||
kadence_data = {
|
||||
"title": f"JS Logic: {item['title']}",
|
||||
"status": "publish",
|
||||
"content": js_logic,
|
||||
"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"--> SUCCESS: Post {post_id}")
|
||||
time.sleep(1)
|
||||
|
||||
print("\nBATCH 9 COMPLETE")
|
||||
Reference in New Issue
Block a user