import re from pathlib import Path from urllib.parse import urlparse # Define paths input_file = Path("orig-inp.txt") current_dir = Path(".") if not input_file.exists(): print(f"Error: {input_file} not found.") exit(1) # Regex to find a URL at the end of a line # Looks for http:// or https:// followed by non-whitespace characters until the end of the line url_regex = re.compile(r"(https?://\S+)$") print(f"{'Filename / URL Source':<40} | {'Status':<15} | {'Size (KB)':<10}") print("-" * 72) with open(input_file, "r", encoding="utf-8") as f: for line in f: line = line.strip() # Ignore blank lines if not line: continue # Extract the URL from the end of the line match = url_regex.search(line) if not match: print(f"{line[:40]:<40} | No URL Found | -") continue url = match.group(1) try: # Extract the final segment (filename) from the URL parsed_url = urlparse(url) filename = Path(parsed_url.path).name # Sanity Check 1: Does the filename make sense? if not filename or not Path(filename).suffix or filename.startswith("."): print(f"{filename or url[:40]:<40} | Invalid Name | -") continue local_file = current_dir / filename # Sanity Check 2: Does it exist? if not local_file.is_file(): print(f"{filename:<40} | Missing | -") continue # Sanity Check 3: Is it over 1 KB? file_size_kb = local_file.stat().st_size / 1024 if file_size_kb <= 1: print( f"{filename:<40} | Too Small | {file_size_kb:.2f} KB" ) continue # Passed all checks print(f"{filename:<40} | OK | {file_size_kb:.2f} KB") except Exception as e: print(f"{url[:40]:<40} | Error: {str(e)[:10]} | -")