Proxy function to be used with `enable_requests_cache()`
    to have the requests cached

    >>> import timeit
    >>> clean_cache()
    >>> enable_requests_cache()
    >>> url = "https://regardscitoyens.org/"
    >>> timeit.timeit(lambda: download(url), number=1) < 0.01
    False
    >>> timeit.timeit(lambda: download(url), number=1) < 0.01
    True

def download(url, retry=5):
    """
    Proxy function to be used with `enable_requests_cache()`
    to have the requests cached

    >>> import timeit
    >>> clean_cache()
    >>> enable_requests_cache()
    >>> url = "https://regardscitoyens.org/"
    >>> timeit.timeit(lambda: download(url), number=1) < 0.01
    False
    >>> timeit.timeit(lambda: download(url), number=1) < 0.01
    True
    """
    try:
        if CACHE_ENABLED:
            file = os.path.join(cache_directory(), hashlib.sha224(url.encode('utf-8')).hexdigest())
            if os.path.exists(file):
                try:
                    with open(file, 'rb') as f:
                        resp = pickle.load(f)
                        if '--debug' in sys.argv:
                            print('[download]', url, '[#cached]', file=sys.stderr)
                        return resp
                except Exception:
                    if '--debug' in sys.argv:
                        print('[download]', url, '[#failed-to-retrieve]', file=sys.stderr)

        if '--debug' in sys.argv:
            print('[download]', url, file=sys.stderr)

        resp = requests.get(url, headers={
            'User-Agent': 'https://github.com/regardscitoyens/the-law-factory-parser (Compat: Mozilla)'
        })

        if 500 <= resp.status_code < 600:
            raise HTTPError('%s Server Error for url: %s' % (resp.status_code, url), response=resp)

        if CACHE_ENABLED:
            if not os.path.exists(cache_directory()):
                os.makedirs(cache_directory())
            with open(file, 'wb') as f:
                pickle.dump(resp, f)
        return resp
    except (ConnectionError, HTTPError) as e:
        if retry:
            time.sleep(1)
            return download(url, retry=retry-1)
        raise e