Last active
January 27, 2023 14:50
-
-
Save thevickypedia/476f106676663601f7521ac9eb8aa025 to your computer and use it in GitHub Desktop.
Check domain name availability across all top-level domains in AWS using whois service
This file contains 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 logging | |
import subprocess | |
from typing import Iterable, NoReturn | |
import requests | |
from bs4 import BeautifulSoup | |
class CustomFormatter(logging.Formatter): | |
"""Override logging.Formatter using custom colors.""" | |
grey = "\x1b[38;20m" | |
green = "\033[32m" | |
yellow = "\x1b[33;20m" | |
red = "\x1b[31;20m" | |
bold_red = "\x1b[31;1m" | |
reset = "\x1b[0m" | |
format = "%(asctime)s - %(levelname)s - [%(funcName)s:%(lineno)d] - %(message)s" | |
FORMATS = { | |
logging.DEBUG: grey + format + reset, | |
logging.INFO: green + format + reset, | |
logging.WARNING: yellow + format + reset, | |
logging.ERROR: red + format + reset, | |
logging.CRITICAL: bold_red + format + reset | |
} | |
def format(self, record): | |
log_fmt = self.FORMATS.get(record.levelno) | |
formatter = logging.Formatter(log_fmt) | |
return formatter.format(record) | |
logger = logging.getLogger(__name__) | |
logger.setLevel(logging.DEBUG) | |
handler = logging.StreamHandler() | |
handler.setFormatter(CustomFormatter()) | |
logger.addHandler(handler) | |
def get_top_level() -> Iterable[str]: | |
"""Read domain file downloaded from AWS and yield each domain.""" | |
try: | |
response = requests.get(url="https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/registrar-tld-list.html") | |
except requests.exceptions.RequestException as error: | |
logger.error(error.__str__()) | |
return | |
if not response.ok: | |
logger.error("[%s]: %s" % (response.status_code, response.content)) | |
return | |
soup = BeautifulSoup(response.content, "html.parser") | |
dl_data = soup.find_all("dd") | |
for dl_item in dl_data: | |
for tag in dl_item.find_all("a"): | |
if tag.text.startswith("."): | |
yield tag.text.split()[0] | |
def check_availability(name: str) -> NoReturn: | |
"""Check availability of a domain name with all top-level domains.""" | |
for top in get_top_level(): | |
domain = name + top | |
try: | |
response = subprocess.check_output("whois %s" % domain, shell=True) | |
except subprocess.SubprocessError as error: | |
if isinstance(error, subprocess.CalledProcessError): | |
logger.error("[%s]: %s" % (error.returncode, error.output.decode(encoding='UTF-8').strip())) | |
break | |
logger.warning(error.__str__()) | |
continue | |
init = response.decode('utf-8').splitlines()[0].strip() | |
if init.upper() == ("Domain Name: %s" % domain).upper(): | |
pass | |
elif init == "Domain not found.": | |
logger.info("'%s' is free!" % domain) | |
elif init == "No Data Found": | |
logger.warning("'%s' - no data found" % domain) | |
else: | |
logger.warning("%s :: %s" % (domain, init)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment