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"""
{item['desc']}
""" 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"""""" 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")