Last active
August 29, 2015 13:56
-
-
Save esparta/8981415 to your computer and use it in GitHub Desktop.
Checks a list of URLs ...
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
""" | |
URL Checker. | |
A basic tester for the URL | |
""" | |
from __future__ import print_function | |
import re | |
import requests | |
from colors import Colors | |
def test_url(url): | |
""" | |
Cheks if the URL is "alive" | |
Returns: Tuple ( boolean , Reason ) | |
""" | |
req = requests.get(url, stream=True) | |
return (req.ok, req.reason) | |
def test_list(markdownfile): | |
""" Test a given Markdown file """ | |
with open(markdownfile) as markdown: | |
for line in markdown: | |
title_regex = re.compile(r"(\[.*?\])\((http://.*?)\)") | |
for match in title_regex.findall(line): | |
title, urlx = match | |
print("Testing {t} {u}".format(t = title, u=urlx), end=" ") | |
result, reason = test_url(urlx) | |
color = Colors.get("green") if result \ | |
else Colors.get("fail") | |
print("{0}{1}{2}".format(color, reason, Colors.endc())) | |
if __name__ == "__main__": | |
test_list("free-programming-books-es.md") |
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
""" | |
Static class to access basic colors for TTY scripts | |
""" | |
class Colors(object): | |
""" | |
We want colors on TTY! | |
""" | |
_colors = { | |
"header": '\033[95m', | |
"blue" : '\033[94m', | |
"green" :'\033[92m', | |
"warning" : '\033[93m', | |
"fail" : '\033[91m', | |
"endc" : '\033[0m' | |
} | |
def __init__(self): | |
"""The init """ | |
pass | |
@classmethod | |
def get(cls, thecolor=None): | |
""" Get the colors """ | |
return cls._colors.get(thecolor, cls.endc()) | |
@classmethod | |
def endc(cls): | |
"""End the colors on TTY""" | |
return cls._colors["endc"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment