From ebabf77a263c0ad214be9cd13b15e1b786ff2be6 Mon Sep 17 00:00:00 2001 From: spark Date: Wed, 28 Oct 2026 18:27:47 +0000 Subject: [PATCH] spark: add URL extraction and validation utility --- wizard-work/spark/url_extractor.py | 51 ++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 wizard-work/spark/url_extractor.py diff --git a/wizard-work/spark/url_extractor.py b/wizard-work/spark/url_extractor.py new file mode 100644 index 0000000..05801c7 --- /dev/null +++ b/wizard-work/spark/url_extractor.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 +"""URL extraction and validation utility.""" + +import re +from urllib.parse import urlparse +from typing import List + + +def extract_and_validate_urls(text: str) -> List[str]: + """ + Extract all valid HTTP/HTTPS URLs from a text string. + Returns a list of URLs with valid http/https schemes and proper structure. + + Args: + text: Input text to search for URLs + + Returns: + List of validated URL strings + """ + # Regex pattern to match URLs + url_pattern = r'https?://[^\s<>"{}|\\^`\[\]]+' + + matches = re.findall(url_pattern, text) + + valid_urls = [] + for match in matches: + try: + parsed = urlparse(match) + # Require http/https scheme AND a netloc (hostname) + if parsed.scheme in ('http', 'https') and parsed.netloc: + valid_urls.append(match.rstrip('.,;:!?)')) + except Exception: + continue + + return valid_urls + + +if __name__ == "__main__": + test_text = ''' + Check out https://example.com and http://test.org for info. + Invalid: ftp://bad.com and malformed https:// and no-scheme://test.com + Also visit https://docs.python.org/3/library/re.html#module-re + ''' + + urls = extract_and_validate_urls(test_text) + print("Extracted URLs:", urls) + + # Verify expected results - only valid http/https URLs + expected = ['https://example.com', 'http://test.org', 'https://docs.python.org/3/library/re.html#module-re'] + assert urls == expected, f"Mismatch: got {urls}, expected {expected}" + print("VERIFICATION PASSED") \ No newline at end of file