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_7 = [ {"title": "Centigrams to Grams", "slug": "centigrams-to-grams", "v1": "Centigrams (cg)", "v2": "Grams (g)", "factor": 0.01, "desc": "Centigrams are a metric unit of mass equal to 1/100th of a gram. They are used in chemistry and pharmacy for small measurements."}, {"title": "Centiliters to Liters", "slug": "centiliters-to-liters", "v1": "Centiliters (cl)", "v2": "Liters (l)", "factor": 0.01, "desc": "Centiliters are commonly used in Europe for measuring liquid volumes in beverages. One centiliter is 10 milliliters."}, {"title": "Centimeters to Feet", "slug": "centimeters-to-feet", "v1": "Centimeters (cm)", "v2": "Feet (ft)", "factor": 0.032808399, "desc": "Centimeters are the standard metric unit for length in everyday use, while feet remain prevalent in the US and UK for height and construction."}, {"title": "Centimeters to Meters", "slug": "centimeters-to-meters", "v1": "Centimeters (cm)", "v2": "Meters (m)", "factor": 0.01, "desc": "The meter is the base unit of length in the International System of Units (SI). There are exactly 100 centimeters in one meter."}, {"title": "Centimeters to Millimeters", "slug": "centimeters-to-millimeters", "v1": "Centimeters (cm)", "v2": "Millimeters (mm)", "factor": 10.0, "desc": "Millimeters provide higher precision for small-scale measurements. One centimeter consists of ten millimeters."}, {"title": "Chains to Feet", "slug": "chains-to-feet", "v1": "Chains (ch)", "v2": "Feet (ft)", "factor": 66.0, "desc": "A chain is a unit of length equal to 66 feet, historically used in land surveying and railway engineering (Gunter's chain)."}, {"title": "Chains to Meters", "slug": "chains-to-meters", "v1": "Chains (ch)", "v2": "Meters (m)", "factor": 20.1168, "desc": "In modern surveying, the traditional chain (66 feet) is defined as exactly 20.1168 meters."}, {"title": "Cubic Centimeters to Cubic Inches", "slug": "cubic-centimeters-to-cubic-inches", "v1": "Cubic Centimeters (cc)", "v2": "Cubic Inches (cu in)", "factor": 0.0610237441, "desc": "Cubic centimeters (cc) are equal to milliliters and are often used to measure engine displacement."}, {"title": "Cubic Feet to Cubic Meters", "slug": "cubic-feet-to-cubic-meters", "v1": "Cubic Feet (cu ft)", "v2": "Cubic Meters (m³)", "factor": 0.0283168466, "desc": "Cubic feet are used for shipping volumes and HVAC capacity, while cubic meters are the metric standard for volume."}, {"title": "Cubic Meters to Liters", "slug": "cubic-meters-to-liters", "v1": "Cubic Meters (m³)", "v2": "Liters (l)", "factor": 1000.0, "desc": "A cubic meter is a large unit of volume equal to one thousand liters, often used for water consumption or industrial reservoirs."} ] 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 for item in batch_7: print(f"\n--- Processing {item['title']} ---") calc_html = f"""
{item['desc']}
""" existing_id = check_exists(item['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": item['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"""""" # Check for existing Kadence element targeted at this post_id req_e = urllib.request.Request(f"{url_base_kadence}?per_page=100", headers=headers) resp_e = urllib.request.urlopen(req_e, timeout=30) elements = json.loads(resp_e.read().decode("utf-8")) element_id = None for e in elements: cond = e.get('meta', {}).get('_kad_element_show_conditionals', '') if str(post_id) in cond: element_id = e['id'] break 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 7 COMPLETE")