260 lines
11 KiB
Python
260 lines
11 KiB
Python
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_5 = [
|
|
{
|
|
"title": "Fahrenheit to Celsius",
|
|
"slug": "fahrenheit-to-celsius",
|
|
"label1": "Fahrenheit (°F)",
|
|
"label2": "Celsius (°C)",
|
|
"factor": 5/9,
|
|
"offset": -32 * (5/9),
|
|
"seo_text": """
|
|
<!-- wp:paragraph -->
|
|
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Temperature Conversion - Fahrenheit to Celsius:</strong> Converting between Fahrenheit and Celsius is a common necessity for travel, science, and weather reporting. This calculator uses the standard formula $C = (F - 32) \times 5/9$.</p>
|
|
<!-- /wp:paragraph -->
|
|
"""
|
|
},
|
|
{
|
|
"title": "Feet per Second to Meters per Second",
|
|
"slug": "feet-per-second-to-meters-per-second",
|
|
"label1": "Feet per Second (ft/s)",
|
|
"label2": "Meters per Second (m/s)",
|
|
"factor": 0.3048,
|
|
"offset": 0,
|
|
"seo_text": """
|
|
<!-- wp:paragraph -->
|
|
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Speed Conversion:</strong> Feet per second and meters per second are both standard units of speed. This converter is essential for engineering, ballistics, and aviation calculations where different measurement systems are used.</p>
|
|
<!-- /wp:paragraph -->
|
|
"""
|
|
},
|
|
{
|
|
"title": "Fluid Ounces to Milliliters",
|
|
"slug": "fluid-ounces-to-milliliters",
|
|
"label1": "Fluid Ounces (US fl oz)",
|
|
"label2": "Milliliters (ml)",
|
|
"factor": 29.5735296,
|
|
"offset": 0,
|
|
"seo_text": """
|
|
<!-- wp:paragraph -->
|
|
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Volume Conversion:</strong> US fluid ounces are a standard unit of liquid volume in the United States. This tool allows for precise conversion to milliliters, commonly used in food labels and medicine.</p>
|
|
<!-- /wp:paragraph -->
|
|
"""
|
|
},
|
|
{
|
|
"title": "Gallons per Minute to Liters per Second",
|
|
"slug": "gallons-per-minute-to-liters-per-second",
|
|
"label1": "Gallons per Minute (GPM)",
|
|
"label2": "Liters per Second (L/s)",
|
|
"factor": 0.0630901964,
|
|
"offset": 0,
|
|
"seo_text": """
|
|
<!-- wp:paragraph -->
|
|
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Flow Rate Conversion:</strong> Gallons per minute (GPM) is a common unit for water flow in plumbing and irrigation, while liters per second (L/s) is the preferred SI unit for volumetric flow.</p>
|
|
<!-- /wp:paragraph -->
|
|
"""
|
|
},
|
|
{
|
|
"title": "Grains to Grams",
|
|
"slug": "grains-to-grams",
|
|
"label1": "Grains (gr)",
|
|
"label2": "Grams (g)",
|
|
"factor": 0.06479891,
|
|
"offset": 0,
|
|
"seo_text": """
|
|
<!-- wp:paragraph -->
|
|
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Weight Conversion:</strong> The grain is a unit of measurement for mass, based on the average mass of a single seed of a cereal. It is still used today in ballistics and the measurement of medication.</p>
|
|
<!-- /wp:paragraph -->
|
|
"""
|
|
},
|
|
{
|
|
"title": "Grams to Milligrams",
|
|
"slug": "grams-to-milligrams",
|
|
"label1": "Grams (g)",
|
|
"label2": "Milligrams (mg)",
|
|
"factor": 1000.0,
|
|
"offset": 0,
|
|
"seo_text": """
|
|
<!-- wp:paragraph -->
|
|
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Metric Mass Conversion:</strong> Milligrams are equal to one-thousandth of a gram. This conversion is extremely common in chemistry, biology, and healthcare for measuring small quantities of substances.</p>
|
|
<!-- /wp:paragraph -->
|
|
"""
|
|
},
|
|
{
|
|
"title": "Grams to Ounces",
|
|
"slug": "grams-to-ounces",
|
|
"label1": "Grams (g)",
|
|
"label2": "Ounces (oz)",
|
|
"factor": 0.0352739619,
|
|
"offset": 0,
|
|
"seo_text": """
|
|
<!-- wp:paragraph -->
|
|
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Mass Conversion:</strong> Converting metric grams to imperial ounces is a frequent task in international commerce and cooking. One ounce is approximately 28.35 grams.</p>
|
|
<!-- /wp:paragraph -->
|
|
"""
|
|
},
|
|
{
|
|
"title": "Grams to Pennyweights",
|
|
"slug": "grams-to-pennyweights",
|
|
"label1": "Grams (g)",
|
|
"label2": "Pennyweights (dwt)",
|
|
"factor": 0.643014931,
|
|
"offset": 0,
|
|
"seo_text": """
|
|
<!-- wp:paragraph -->
|
|
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Jewelry Measurement:</strong> Pennyweights are a traditional unit of mass used in the jewelry industry for precious metals. This calculator provides a precise bridge to the metric system.</p>
|
|
<!-- /wp:paragraph -->
|
|
"""
|
|
},
|
|
{
|
|
"title": "Grams to Troy Ounces",
|
|
"slug": "grams-to-troy-ounces",
|
|
"label1": "Grams (g)",
|
|
"label2": "Troy Ounces (oz t)",
|
|
"factor": 0.0321507466,
|
|
"offset": 0,
|
|
"seo_text": """
|
|
<!-- wp:paragraph -->
|
|
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Precious Metal Calculation:</strong> Unlike standard ounces, troy ounces are used specifically for the pricing and measurement of gold, silver, and platinum. One troy ounce is equivalent to 31.103 grams.</p>
|
|
<!-- /wp:paragraph -->
|
|
"""
|
|
},
|
|
{
|
|
"title": "Gray to Rad",
|
|
"slug": "gray-to-rad",
|
|
"label1": "Grays (Gy)",
|
|
"label2": "Rads",
|
|
"factor": 100.0,
|
|
"offset": 0,
|
|
"seo_text": """
|
|
<!-- wp:paragraph -->
|
|
<p style="margin-top: 2rem; line-height: 1.6;"><strong>Absorbed Dose Conversion:</strong> The gray is the SI unit of absorbed ionizing radiation dose, while the rad is the deprecated cgs unit. One gray is defined as exactly 100 rads.</p>
|
|
<!-- /wp:paragraph -->
|
|
"""
|
|
}
|
|
]
|
|
|
|
for item in batch_5:
|
|
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);
|
|
if (urlParams.has('v1')) {{
|
|
let v1 = parseFloat(urlParams.get('v1'));
|
|
if (!isNaN(v1)) {{
|
|
inp1.value = v1;
|
|
calculate1();
|
|
}}
|
|
}} else if (urlParams.has('v2')) {{
|
|
let v2 = parseFloat(urlParams.get('v2'));
|
|
if (!isNaN(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 5 COMPLETE ---")
|