feat: Add new calculator batches (6-10) and associated deployment/verification scripts, and update the calculator list.
This commit is contained in:
146
batch_10.py
Normal file
146
batch_10.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_10 = [
|
||||
{"title": "Megabytes to Gigabytes", "slug": "megabytes-to-gigabytes", "v1": "Megabytes (MB)", "v2": "Gigabytes (GB)", "factor": 0.001, "desc": "One gigabyte contains 1,000 megabytes (decimal definition). This conversion is standard in consumer electronics and storage capacity reporting."},
|
||||
{"title": "Megajoules to Kilowatt-hours", "slug": "megajoules-to-kilowatt-hours", "v1": "Megajoules (MJ)", "v2": "Kilowatt-hours (kWh)", "factor": 0.277778, "desc": "One kilowatt-hour is exactly 3.6 megajoules. Megajoules are often used in scientific energy calculations, while kWh is the standard for utility billing."},
|
||||
{"title": "Meters to Feet", "slug": "meters-to-feet", "v1": "Meters (m)", "v2": "Feet (ft)", "factor": 3.28084, "desc": "The meter is the SI base unit of length. One meter is approximately 3.28 feet, a common conversion for height and room dimensions."},
|
||||
{"title": "Meters to Yards", "slug": "meters-to-yards", "v1": "Meters (m)", "v2": "Yards (yd)", "factor": 1.09361, "desc": "Meters and yards are similar in scale, but the meter is slightly longer (approx. 1.09 yards). This is common in sports like swimming and athletics."},
|
||||
{"title": "Metric tons to Short tons", "slug": "metric-tons-to-short-tons", "v1": "Metric Tons (t)", "v2": "Short Tons (US)", "factor": 1.10231, "desc": "A metric ton (tonne) is 1,000 kg, slightly heavier than the US short ton (2,000 lbs)."},
|
||||
{"title": "Minutes to Hours", "slug": "minutes-to-hours", "v1": "Minutes (min)", "v2": "Hours (hr)", "factor": 0.0166667, "desc": "Sixty minutes make one hour. This conversion is used for tracking labor hours and travel duration."},
|
||||
{"title": "Minutes to Seconds", "slug": "minutes-to-seconds", "v1": "Minutes (min)", "v2": "Seconds (s)", "factor": 60.0, "desc": "One minute contains sixty seconds. This conversion is essential for high-precision time tracking and performance measurement."},
|
||||
{"title": "Nautical miles to Kilometers", "slug": "nautical-miles-to-kilometers", "v1": "Nautical Miles (nmi)", "v2": "Kilometers (km)", "factor": 1.852, "desc": "A nautical mile is defined based on the Earth's circumference and is exactly 1.852 kilometers, the standard for maritime and aviation navigation."},
|
||||
{"title": "Newtons to Dynes", "slug": "newtons-to-dynes", "v1": "Newtons (N)", "v2": "Dynes (dyn)", "factor": 100000.0, "desc": "A newton is the SI unit of force. One newton is equal to 100,000 dynes (the CGS unit of force)."},
|
||||
{"title": "Ounces to Grams", "slug": "ounces-to-grams", "v1": "Ounces (oz)", "v2": "Grams (g)", "factor": 28.3495, "desc": "One avoirdupois ounce is approximately 28.35 grams. This is the global standard for kitchen measurements and postal weights."}
|
||||
]
|
||||
|
||||
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_10:
|
||||
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 10 COMPLETE")
|
||||
Reference in New Issue
Block a user