from urlparse import urlsplit

from django.core.exceptions import ValidationError
from django.core.validators import URLValidator


class InvalidNetworkUrl(Exception):
    """
    Thrown when the network url is invalid.
    """
    pass


class DataNotFound(Exception):
    """
    Thrown when requested data is not found.
    """
    pass


class NetworkNotSupported(Exception):
    """
    Thrown when network is not found or supported.
    """
    pass


url_validator = URLValidator(schemes=["http", "https"])


def validate_url(domain, url):
    try:
        url_validator(url)
    except ValidationError:
        raise InvalidNetworkUrl("Invalid url %s" % url)

    netloc = urlsplit(url).netloc
    base_domain = netloc.split("www.")[-1]
    if base_domain != domain:
        raise InvalidNetworkUrl("Wrong url %s" % url)
