Created
June 4, 2024 12:06
-
-
Save sermelipharo/ee0e4c89cad85fbef37b4bbf1ed77bd3 to your computer and use it in GitHub Desktop.
Simple Link Status Checker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| import requests | |
| import sys | |
| import signal | |
| from concurrent.futures import ThreadPoolExecutor, as_completed | |
| try: | |
| from colorama import init, Fore, Style | |
| colorama_installed = True | |
| init(autoreset=True) | |
| except ImportError: | |
| colorama_installed = False | |
| # Global flag to handle termination | |
| should_terminate = False | |
| def signal_handler(sig, frame): | |
| global should_terminate | |
| should_terminate = True | |
| print("\nTermination signal received. Exiting...") | |
| def check_link(link): | |
| """Check the status of a single link.""" | |
| try: | |
| if should_terminate: | |
| return "Terminated" | |
| response = requests.head(link, allow_redirects=True) | |
| if response.status_code == 200: | |
| return f"{link} HTTP Code {response.status_code}" | |
| else: | |
| if colorama_installed: | |
| return Fore.RED + f"{link} HTTP Code {response.status_code}" + Style.RESET_ALL | |
| else: | |
| return f"{link} HTTP Code {response.status_code}" | |
| except requests.RequestException: | |
| if colorama_installed: | |
| return Fore.RED + f"{link} HTTP Code Error" + Style.RESET_ALL | |
| else: | |
| return f"{link} HTTP Code Error" | |
| def check_links_in_file(file_path, max_workers=1): | |
| """Check all links in a text file, with optional parallel execution.""" | |
| with open(file_path, 'r', encoding='utf-8') as file: | |
| content = file.read() | |
| # Regular expression to extract links | |
| link_pattern = re.compile(r'\bhttps?://[^\s)]+\b') | |
| links = link_pattern.findall(content) | |
| # Using ThreadPoolExecutor for parallel execution | |
| with ThreadPoolExecutor(max_workers=max_workers) as executor: | |
| future_to_link = {executor.submit(check_link, link): link for link in links} | |
| try: | |
| for future in as_completed(future_to_link): | |
| if should_terminate: | |
| break | |
| print(future.result()) | |
| except KeyboardInterrupt: | |
| print("\nExecution interrupted. Terminating remaining tasks...") | |
| executor.shutdown(wait=False) | |
| sys.exit(1) | |
| if __name__ == "__main__": | |
| if not colorama_installed: | |
| print("colorama is not installed. Install it using 'pip install colorama' for colored output.") | |
| if len(sys.argv) < 2: | |
| print("Usage: python check_links.py <path_to_text_file> [--threads N]") | |
| sys.exit(1) | |
| file_path = sys.argv[1] | |
| max_workers = 1 | |
| # Parse command line arguments | |
| if len(sys.argv) == 4 and sys.argv[2] == '--threads': | |
| try: | |
| max_workers = int(sys.argv[3]) | |
| except ValueError: | |
| print("Invalid value for --threads. It must be an integer.") | |
| sys.exit(1) | |
| # Register the signal handler for SIGINT | |
| signal.signal(signal.SIGINT, signal_handler) | |
| check_links_in_file(file_path, max_workers) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment