wikimedica-disease-search/data/fetch/http.py

43 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import logging
import requests
import urllib3
# Session de requêtage HTTP
session = requests.Session()
# Objet pour afficher le journal dexécution
logger = logging.getLogger('fetch.http')
class RetryAdapter(requests.adapters.HTTPAdapter):
"""
Adapteur de requêtes réessayant les requêtes échouées avec
un délai adaptatif.
"""
def __init__(self):
# Réessaie jusquà dix fois la récupération de chaque page, en
# augmentant progressivement le délai entre chaque essai
retry = urllib3.util.Retry(total=10, backoff_factor=0.3)
super(RetryAdapter, self).__init__(max_retries=retry)
def send(
self, request, stream=False, timeout=None, verify=True,
cert=None, proxies=None
):
logger.debug('{} {}'.format(request.method, request.url))
return super(RetryAdapter, self).send(
request, stream=stream, timeout=timeout,
verify=verify, cert=cert, proxies=proxies
)
adapter = RetryAdapter()
session.mount('http://', adapter)
session.mount('https://', adapter)
# Identification du robot
session.headers['User-Agent'] = (
'WikimedicaDiseaseSearch/0.1 '
'(https://gitlab.com/matteodelabre/wikimedica-disease-search)'
)