import logging
import requests


def _is_valid_link(link, check_src=False):
    """ Checks if the URL provided to the PDF file is valid

    :param link         : link to the PDF file, e.g. `https://arxiv.org/pdf/1812.11928.pdf`
    :type link          : str
    :param check_src    : make sure the link isn't a redirection to a pdf, but leads to an archiv file
    :type check_src     : bool
    :return             : True if the link is valid, False otherwise
    :rtype              : bool
    """

    logging.info("Checking link validity...")
    request = requests.get(link)
    if request.status_code == 200:
        if check_src:
            headers = request.headers
            # Seems to indicate an attempt to download non-existent sources redirected to the PDF
            if headers['Content-Type'] == 'application/pdf' and headers['Content-Encoding'] == 'gzip':
                return False
        return True
    return False