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"""

{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 10 COMPLETE")