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_8 = [ {"title": "Grams to Micrograms", "slug": "grams-to-micrograms", "v1": "Grams (g)", "v2": "Micrograms (µg)", "factor": 1000000.0, "desc": "A gram is a basic unit of mass in the metric system. A microgram is one-millionth of a gram, used primarily in medicine and micro-biology."}, {"title": "Grams to Milligrams", "slug": "grams-to-milligrams", "v1": "Grams (g)", "v2": "Milligrams (mg)", "factor": 1000.0, "desc": "One gram equals one thousand milligrams. This conversion is extremely common across science, medication dosage, and dietary tracking."}, {"title": "Hectopascals to Pascals", "slug": "hectopascals-to-pascals", "v1": "Hectopascals (hPa)", "v2": "Pascals (Pa)", "factor": 100.0, "desc": "The pascal (Pa) is the SI unit of pressure. A hectopascal is 100 pascals and is identical to the millibar, frequently used in meteorology."}, {"title": "Hectopascals to Millibars", "slug": "hectopascals-to-millibars", "v1": "Hectopascals (hPa)", "v2": "Millibars (mbar)", "factor": 1.0, "desc": "Hectopascals and millibars are equivalent units of pressure. While hectopascals are the SI standard, millibars are still widely used in weather reporting."}, {"title": "Joules to Kilojoules", "slug": "joules-to-kilojoules", "v1": "Joules (J)", "v2": "Kilojoules (kJ)", "factor": 0.001, "desc": "The joule is the SI unit of energy. One kilojoule is one thousand joules, often used for representing energy content in food or mechanical work."}, {"title": "Kilojoules to Joules", "slug": "kilojoules-to-joules", "v1": "Kilojoules (kJ)", "v2": "Joules (J)", "factor": 1000.0, "desc": "Kilojoules are larger units of energy. To convert them to standard joules, simply multiply by one thousand."}, {"title": "Micrograms to Grams", "slug": "micrograms-to-grams", "v1": "Micrograms (µg)", "v2": "Grams (g)", "factor": 0.000001, "desc": "Converting micrograms to grams is common in analytical chemistry where high-precision measurements of trace substances are required."}, {"title": "Milligrams to Grams", "slug": "milligrams-to-grams", "v1": "Milligrams (mg)", "v2": "Grams (g)", "factor": 0.001, "desc": "Milligrams are often used for small measurements of mass. One milligram is one-thousandth of a gram."}, {"title": "Millibars to Pascals", "slug": "millibars-to-pascals", "v1": "Millibars (mbar)", "v2": "Pascals (Pa)", "factor": 100.0, "desc": "One millibar is exactly 100 pascals. This relationship is a cornerstone of barometric pressure reporting in aviation and meteorology."}, {"title": "Millimeters of Mercury to Pascals", "slug": "millimeters-of-mercury-to-pascals", "v1": "mmHg", "v2": "Pascals (Pa)", "factor": 133.322, "desc": "Millimeters of mercury (mmHg) is a legacy unit of pressure, famously used for blood pressure readings. One mmHg is approximately 133.322 pascals."} ] 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_8: 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 8 COMPLETE")