#!/usr/bin/env python3
"""
check_urls.py — Tool index: kiem tra HTTP status va the noindex
Doc urls.txt, in ket qua theo URL.
"""
import urllib.request
import urllib.error
import re
import concurrent.futures
TIMEOUT = 15
USER_AGENT = "Mozilla/5.0 (compatible; UrlChecker/1.0)"
MAX_WORKERS = 10
def load_urls(path="urls.txt"):
with open(path, "r", encoding="utf-8") as f:
return [line.strip() for line in f if line.strip() and not line.startswith("#")]
def check_one(url):
if not url.startswith(("http://", "https://")):
url = "https://" + url
req = urllib.request.Request(url, headers={"User-Agent": USER_AGENT})
try:
with urllib.request.urlopen(req, timeout=TIMEOUT) as resp:
status = resp.status
x_robots = resp.headers.get("X-Robots-Tag", "")
html = resp.read(409600).decode("utf-8", errors="ignore")
except urllib.error.HTTPError as e:
return url, e.code, False, "HTTP ERROR"
except Exception as e:
return url, None, False, f"ERROR: {type(e).__name__}"
head = html[:12000]
noindex = "noindex" in x_robots.lower() or bool(re.search(
r'<meta[^>]+name=["\']robots["\'][^>]+content=["\'][^"\']*noindex',
head,
re.IGNORECASE,
)) or bool(re.search(
r'<meta[^>]+content=["\'][^"\']*noindex[^"\']*["\'][^>]+name=["\']robots["\']',
head,
re.IGNORECASE,
))
return url, status, noindex, "OK"
def main():
urls = load_urls()
if not urls:
print("urls.txt rỗng hoặc không tồn tại.")
return
with concurrent.futures.ThreadPoolExecutor(max_workers=MAX_WORKERS) as ex:
results = list(ex.map(check_one, urls))
for url, status, noindex, note in results:
if status == 200 and not noindex:
print(f"✅ {url} — HTTP 200 — READY")
elif status == 200 and noindex:
print(f"⛔ {url} — HTTP 200 — NOINDEX FOUND — SKIP")
else:
print(f"⛔ {url} — HTTP {status if status else note} — SKIP")
if __name__ == "__main__":
main()