import urllib.request import re import json import time # Batch 2 Registry Mapping for Verification batch_2_registry = [ {"url": "https://howdoyouconvert.com/calculator/attograms-to-femtograms/?ao_noptimize=1", "fwd": 0.001, "v1": 1000, "expected_v2": 1}, {"url": "https://howdoyouconvert.com/calculator/bar-to-pascal/?ao_noptimize=1", "fwd": 100000, "v1": 1, "expected_v2": 100000}, {"url": "https://howdoyouconvert.com/calculator/bar-to-psi/?ao_noptimize=1", "fwd": 14.5037738, "v1": 1, "expected_v2": 14.5037738}, {"url": "https://howdoyouconvert.com/calculator/becquerel-to-curie/?ao_noptimize=1", "fwd": 2.7027027e-11, "v1": 37000000000, "expected_v2": 1}, {"url": "https://howdoyouconvert.com/calculator/becquerel-to-rutherford/?ao_noptimize=1", "fwd": 0.000001, "v1": 1000000, "expected_v2": 1}, {"url": "https://howdoyouconvert.com/calculator/bits-to-bytes/?ao_noptimize=1", "fwd": 0.125, "v1": 8, "expected_v2": 1}, {"url": "https://howdoyouconvert.com/calculator/btu-to-kilojoules/?ao_noptimize=1", "fwd": 1.05505585, "v1": 1, "expected_v2": 1.05505585}, {"url": "https://howdoyouconvert.com/calculator/btuhour-to-watts/?ao_noptimize=1", "fwd": 0.293071, "v1": 100, "expected_v2": 29.3071}, {"url": "https://howdoyouconvert.com/calculator/calories-to-joules/?ao_noptimize=1", "fwd": 4.184, "v1": 1, "expected_v2": 4.184}, {"url": "https://howdoyouconvert.com/calculator/calories-to-kilojoules/?ao_noptimize=1", "fwd": 4.184, "v1": 1, "expected_v2": 4.184}, ] def verify_math(calc): print(f"\n--- Verifying {calc['url']} ---") headers = {"User-Agent": "Mozilla/5.0"} # Test Forward try: url_v1 = f"{calc['url']}&v1={calc['v1']}" req_f = urllib.request.Request(url_v1, headers=headers) resp_f = urllib.request.urlopen(req_f).read().decode('utf-8') # We need to check if the JS logic is actually executing. # Since we use URL params and DOMContentLoaded, we can't 'see' the changed input value in raw HTML. # Instead, we will extract the JS from the page body and evaluate it locally to verify the logic we injected is correct. scripts = re.findall(r'\s*(.*?)\s*', resp_f, re.DOTALL) logic_script = "" for s in scripts: if "calculate1" in s and "document.getElementById" in s: logic_script = s break if not logic_script: print("FAILED: Logic script not found in page source.") return False print("Found JS logic. Verifying formula constants...") # Extract the logic lines fwd_logic = re.search(r'function calculate1.*?let val2 = (.*?);', logic_script, re.DOTALL) rev_logic = re.search(r'function calculate2.*?let val1 = (.*?);', logic_script, re.DOTALL) if not fwd_logic or not rev_logic: print("FAILED: Could not parse math logic lines from JS.") return False f_expr = fwd_logic.group(1).replace("val1", str(calc['v1'])) r_expr = rev_logic.group(1).replace("val2", str(calc['expected_v2'])) # Evaluate local math actual_v2 = eval(f_expr) actual_v1 = eval(r_expr) print(f"Forward: {calc['v1']} -> Expected {calc['expected_v2']}, Got {actual_v2}") print(f"Reverse: {calc['expected_v2']} -> Expected {calc['v1']}, Got {actual_v1}") if abs(actual_v2 - calc['expected_v2']) < 1e-7 and abs(actual_v1 - calc['v1']) < 1e-7: print("SUCCESS: Math Verification Passed.") return True else: print("FAILED: Math discrepancy detected.") return False except Exception as e: print(f"FAILED: Verification error: {e}") return False all_passed = True for c in batch_2_registry: if not verify_math(c): all_passed = False time.sleep(1) if all_passed: print("\nALL BATCH 2 CALCULATORS VERIFIED SUCCESSFULLY.") else: print("\nSOME BATCH 2 VERIFICATIONS FAILED.")