Exemplo n.º 1
0
def test_no_center_data():
    path_centerinfo = Path("tests", "fixtures", "keldoc",
                           "cabinet-16913-centerinfo.json")
    centerinfo_1 = json.loads(path_centerinfo.read_text(encoding="utf-8"))

    center_info = CenterInfo.from_csv_data(centerinfo_1)

    center1_url = "https://www.keldoc.com/centre-hospitalier-regional/lorient-56100/groupe-hospitalier-bretagne-sud-lorient-hopital-du-scorff?specialty=144"
    request = ScraperRequest(center1_url,
                             "2020-04-04",
                             center_info=center_info)

    def app(request: httpx.Request) -> httpx.Response:
        if (request.url.path ==
                "/centre-hospitalier-regional/lorient-56100/groupe-hospitalier-bretagne-sud-lorient-hopital-du-scorff"
            ):
            return httpx.Response(
                302,
                headers={
                    "Location":
                    "https://vaccination-covid.keldoc.com/redirect/?dom=centre-hospitalier-regional&inst=lorient-56100&user=groupe-hospitalier-bretagne-sud-lorient-hopital-du-scorff&specialty=144 "
                },
            )
        if request.url.path == "/redirect/":
            return httpx.Response(200, json={})
        return httpx.Response(403, json={})

    keldoc.session = httpx.Client(transport=httpx.MockTransport(app))
    date = fetch_slots(request)
    assert not date
Exemplo n.º 2
0
def test_fetch():
    mesoigner.PLATFORM_ENABLED = True

    center_data = dict()
    center_data = json.load(
        io.open(TEST_CENTRE_INFO, "r", encoding="utf-8-sig"))

    center_info = CenterInfo.from_csv_data(center_data)

    # This center has availabilities and should return a date, non null appointment_count and vaccines
    request = ScraperRequest(
        "https://pharmacie-des-pyrenees.pharmaxv.fr/rendez-vous/vaccination/269-vaccination-covid-19/pre-inscription",
        "2021-06-16",
        center_info,
    )
    slots = json.load(
        io.open(Path("tests", "fixtures", "mesoigner", "slots_available.json"),
                "r",
                encoding="utf-8-sig"))

    def app(requested: httpx.Request) -> httpx.Response:
        assert "User-Agent" in requested.headers

        return httpx.Response(200, json=slots)

    client = httpx.Client(transport=httpx.MockTransport(app))

    center_with_availability = mesoigner.MesoignerSlots(client=client)

    response = center_with_availability.fetch(request)
    assert response == "2021-06-16T14:50:00+02:00"
Exemplo n.º 3
0
def test_fetch():
    def app(requested: httpx.Request) -> httpx.Response:
        assert "User-Agent" in requested.headers
        return httpx.Response(responsecode, json=slots)

    valwin.PLATFORM_ENABLED = True

    center_data = dict()
    center_data = json.load(io.open(TEST_CENTRE_INFO, "r", encoding="utf-8-sig"))

    center_info = CenterInfo.from_csv_data(center_data)

    # This center has availabilities and should return a date, non null appointment_count and vaccines
    request = ScraperRequest(
        "https://pharma-api.epharmacie.pro/global/api/meetings/v2/pharmabest75-plateau-lyon/slots",
        "2021-08-10",
        center_info,
    )
    slots = json.load(io.open(Path("tests", "fixtures", "valwin", "slots_available.json"), "r", encoding="utf-8-sig"))

    # Response 200
    responsecode = 200
    client = httpx.Client(transport=httpx.MockTransport(app))
    center_with_availability = valwin.Slots(client=client)
    response = center_with_availability.fetch(request)
    assert response == "2021-09-17T10:00:00"

    # Response 403

    responsecode = 403
    client = httpx.Client(transport=httpx.MockTransport(app))
    center_with_availability = valwin.Slots(client=client)
    with pytest.raises(Exception):
        response = center_with_availability.fetch(request)
        assert response == None
Exemplo n.º 4
0
def test_fetch():
    def app(requested: httpx.Request) -> httpx.Response:
        assert "User-Agent" in requested.headers
        return httpx.Response(responsecode, json=slots)

    bimedoc.PLATFORM_ENABLED = True

    center_data = dict()
    center_data = json.load(io.open(TEST_CENTRE_INFO, "r", encoding="utf-8-sig"))

    center_info = CenterInfo.from_csv_data(center_data)

    # This center has availabilities and should return a date, non null appointment_count and vaccines
    request = ScraperRequest(
        "https://server.bimedoc.com/vmd/pharmacy-with-slots/9cf46288-0080-4a8d-8856-8e9998ced9f7?start_date=2021-08-10&end_date=2021-08-17",
        "2021-08-10",
        center_info,
    )
    slots = json.load(io.open(Path("tests", "fixtures", "bimedoc", "slots_available.json"), "r", encoding="utf-8-sig"))

    # Response 200
    responsecode = 200
    client = httpx.Client(transport=httpx.MockTransport(app))
    center_with_availability = bimedoc.BimedocSlots(client=client)
    response = center_with_availability.fetch(request)
    assert response == "2021-08-11T08:15:00Z"

    # Response 403

    responsecode = 403
    client = httpx.Client(transport=httpx.MockTransport(app))
    center_with_availability = bimedoc.BimedocSlots(client=client)
    with pytest.raises(Exception):
        response = center_with_availability.fetch(request)
        assert response == None
Exemplo n.º 5
0
def test_get_appointments():

    """get_appointments should return first available appointment date"""

    center_data = dict()
    center_data = json.load(io.open(TEST_CENTRE_INFO, "r", encoding="utf-8-sig"))
    center_info = CenterInfo.from_csv_data(center_data)

    # This center has availabilities and should return a date, non null appointment_count and vaccines
    request = ScraperRequest(
        "https://server.bimedoc.com/vmd/pharmacy-with-slots/9cf46288-0080-4a8d-8856-8e9998ced9f7?start_date=2021-08-10&end_date=2021-08-17",
        "2021-08-10",
        center_info,
        "Bimedoc9cf46288-0080-4a8d-8856-8e9998ced9f",
    )
    center_with_availability = bimedoc.BimedocSlots()
    slots = json.load(io.open(Path("tests", "fixtures", "bimedoc", "slots_available.json"), "r", encoding="utf-8-sig"))

    assert center_with_availability.get_appointments(request, slots_api=slots) == "2021-08-11T08:15:00Z"
    assert request.appointment_count == 133
    assert request.vaccine_type == [Vaccine.PFIZER]

    # This one should return no date, neither appointment_count nor vaccine.
    request = ScraperRequest(
        "https://server.bimedoc.com/vmd/pharmacy-with-slots/9cf46288-0080-4a8d-8856-8e9998ced9f7?start_date=2021-08-10&end_date=2021-08-17",
        "2021-08-10",
        center_info,
    )
    center_without_availability = bimedoc.BimedocSlots()
    slots = json.load(
        io.open(Path("tests", "fixtures", "bimedoc", "slots_unavailable.json"), "r", encoding="utf-8-sig")
    )
    assert center_without_availability.get_appointments(request, slots_api=slots) == None
    assert request.appointment_count == 0
    assert request.vaccine_type == None
Exemplo n.º 6
0
def test_fetch_slots():
    bimedoc.PLATFORM_ENABLED = False
    center_data = dict()
    center_data = json.load(io.open(TEST_CENTRE_INFO, "r", encoding="utf-8-sig"))
    center_info = CenterInfo.from_csv_data(center_data)

    request = ScraperRequest(
        "https://server.bimedoc.com/vmd/pharmacy-with-slots/9cf46288-0080-4a8d-8856-8e9998ced9f7?start_date=2021-08-10&end_date=2021-08-17",
        "2021-08-10",
        center_info,
    )
    response = bimedoc.fetch_slots(request)
    # On devrait trouver None puisque la plateforme est désactivée
    assert response == None
Exemplo n.º 7
0
def test_fetch_slots():
    valwin.PLATFORM_ENABLED = False
    center_data = dict()
    center_data = json.load(io.open(TEST_CENTRE_INFO, "r", encoding="utf-8-sig"))
    center_info = CenterInfo.from_csv_data(center_data)

    request = ScraperRequest(
        "https://pharma-api.epharmacie.pro/global/api/meetings/v2/pharmabest75-plateau-lyon/slots",
        "2021-08-10",
        center_info,
    )
    response = valwin.fetch_slots(request)
    # On devrait trouver None puisque la plateforme est désactivée
    assert response == None
Exemplo n.º 8
0
def online_keldoc_test():

    path_centerinfo = Path("tests", "fixtures", "keldoc",
                           "cabinet-16913-centerinfo.json")
    centerinfo_1 = json.loads(path_centerinfo.read_text(encoding="utf-8"))

    center_info = CenterInfo.from_csv_data(centerinfo_1)

    request = ScraperRequest(
        "https://www.keldoc.com/cabinet-medical/grenoble-38000/centre-de-vaccination-universite-inter-age-du-dauphine-uiad",
        "2021-04-13",
        center_info=center_info,
    )

    fetch_slots(request)
Exemplo n.º 9
0
def cherche_prochain_rdv_dans_centre(
        centre: dict) -> CenterInfo:  # pragma: no cover
    center_data = CenterInfo.from_csv_data(centre)
    start_date = get_start_date()
    has_error = None
    result = None
    try:
        result = fetch_centre_slots(centre["rdv_site_web"],
                                    start_date,
                                    input_data=centre.get("booking"))
        center_data.fill_result(result)
    except ScrapeError as scrape_error:
        logger.error(
            f"erreur lors du traitement de la ligne avec le gid {centre['gid']} {str(scrape_error)}"
        )
        has_error = scrape_error
    except CircuitBreakerOffException as error:
        logger.error(
            f"circuit '{error.name}' désactivé lors du traîtement de la ligne avec le gid {centre['gid']}: {str(error)}"
        )
        has_error = error
    except Exception:
        logger.error(
            f"erreur lors du traitement de la ligne avec le gid {centre['gid']}"
        )
        traceback.print_exc()

    if has_error is None:
        logger.info(
            f'{centre.get("gid", "")!s:>8} {center_data.plateforme!s:16} {center_data.prochain_rdv or ""!s:32} {center_data.departement!s:6}'
        )
    else:
        logger.info(
            f'{centre.get("gid", "")!s:>8} {center_data.plateforme!s:16} {"Erreur" or ""!s:32} {center_data.departement!s:6}'
        )

    if result is not None and result.request.url is not None:
        center_data.url = result.request.url.lower()
        if result.request.internal_id is None:
            center_data.internal_id = f'{result.platform.lower()}{centre.get("gid", "")}'

    if "type" in centre:
        center_data.type = centre["type"]
    if not center_data.type:
        center_data.type = VACCINATION_CENTER
    center_data.gid = centre.get("gid", "")
    logger.debug(center_data.default())
    return center_data
Exemplo n.º 10
0
def test_keldoc_scrape_nodate():
    center1_url = (
        "https://www.keldoc.com/centre-hospitalier-regional/lorient-56100/groupe-hospitalier"
        "-bretagne-sud-lorient-hopital-du-scorff?specialty=144 ")

    keldoc.KELDOC_ENABLED = True

    def app_center2(request: httpx.Request) -> httpx.Response:
        if "timetables/" in request.url.path:
            return httpx.Response(200, json={})
        if (request.url.path ==
                "/centre-hospitalier-regional/lorient-56100/groupe-hospitalier-bretagne-sud-lorient-hopital-du-scorff"
            ):
            return httpx.Response(
                302,
                headers={
                    "Location":
                    "https://vaccination-covid.keldoc.com/redirect/?dom=centre-hospitalier-regional&inst=lorient-56100&user=groupe-hospitalier-bretagne-sud-lorient-hopital-du-scorff&specialty=144 "
                },
            )
        for path in CENTER1_KELDOC:
            if request.url.path == path:
                return httpx.Response(200,
                                      json=get_test_data(CENTER1_KELDOC[path]))
        return httpx.Response(200, json={})

    path_centerinfo = Path("tests", "fixtures", "keldoc",
                           "cabinet-16913-centerinfo.json")
    centerinfo_1 = json.loads(path_centerinfo.read_text(encoding="utf-8"))

    center_info = CenterInfo.from_csv_data(centerinfo_1)

    request = ScraperRequest(center1_url,
                             "2099-12-12",
                             center_info=center_info)
    keldoc.session = httpx.Client(transport=httpx.MockTransport(app_center2))

    date = fetch_slots(request)
    assert not date
Exemplo n.º 11
0
def test_convert_ordoclic_second():
    data = {
        "nom": "Centre 2",
        "com_insee": "35238",
        "rdv_site_web": "https://site.fr/",
        "iterator": "ordoclic",
        "location": {
            "coordinates": {
                "lon": 1.1281,
                "lat": 93.182,
            },
            "city": "Foobar",
            "address": "12 Avenue de la ville",
            "zip": "22000",
        },
        "phone_number": "06 06 06 06 06",
    }
    center = CenterInfo.from_csv_data(data)
    assert center.nom == "Centre 2"
    assert center.metadata["address"] == "12 Avenue de la ville, 22000 Foobar"
    assert center.metadata["phone_number"] == "+33606060606"
    assert center.metadata["business_hours"] is None
Exemplo n.º 12
0
def test_convert_centerinfo_invalid():
    data = {
        "nom": "Centre 1",
        "gid": "d001",
        "rdv_site_web": "https://site.fr",
        "com_insee": "0095238",
        "rdv_tel": "06 06 06 06 06",
        "phone_number": "06 06 06 06 07",
        "adr_num": "1",
        "adr_voie": "Rue de la Fraise",
        "com_cp": "75016",
        "com_nom": "Paris",
        "business_hours": {
            "lundi": "09:50-10:10",
            "mardi": "09:10-10:10",
            "mercredi": "10:00-10:10",
            "jeudi": "10:20-10:40",
            "vendredi": "09:50-10:10",
            "samedi": "09:00-10:20",
            "dimanche": "Fermé",
        },
    }

    center = CenterInfo.from_csv_data(data)
    assert center.departement == ""
    assert center.url == "https://site.fr"
    assert center.metadata["address"] == "1 Rue de la Fraise, 75016 Paris"
    assert center.metadata["phone_number"] == "+33606060607"
    assert center.metadata["business_hours"] == {
        "lundi": "09:50-10:10",
        "mardi": "09:10-10:10",
        "mercredi": "10:00-10:10",
        "jeudi": "10:20-10:40",
        "vendredi": "09:50-10:10",
        "samedi": "09:00-10:20",
        "dimanche": "Fermé",
    }
Exemplo n.º 13
0
def test_keldoc_scrape():
    center1_url = "https://vaccination-covid.keldoc.com/centre-hospitalier-regional/lorient-56100/groupe-hospitalier-bretagne-sud-lorient-hopital-du-scorff?cabinet=16913&specialty=144"
    path = Path("tests", "fixtures", "keldoc", "center1-cabinet-16913.json")
    input_data = json.loads(path.read_text(encoding="utf-8"))

    path = Path("tests", "fixtures", "keldoc", "cabinet-16913-centerinfo.json")
    centerinfo_1 = json.loads(path.read_text(encoding="utf-8"))

    center_info = CenterInfo.from_csv_data(centerinfo_1)
    request = ScraperRequest(center1_url,
                             "2020-04-04",
                             input_data=input_data,
                             center_info=center_info)
    keldoc.session = httpx.Client(transport=httpx.MockTransport(app_center1))

    date = fetch_slots(request)
    # When it's already killed
    if not keldoc.PLATFORM_ENABLED:
        assert date is None
    else:
        assert date == "2021-04-20T16:55:00.000000+0200"
    keldoc.PLATFORM_ENABLED = False
    test_killswitch = fetch_slots(request)
    assert not test_killswitch
Exemplo n.º 14
0
def test_get_appointments():

    """get_appointments should return first available appointment date"""

    center_data = dict()
    center_data = json.load(io.open(TEST_CENTRE_INFO, "r", encoding="utf-8-sig"))
    center_info = CenterInfo.from_csv_data(center_data)

    # This center has availabilities and should return a date, non null appointment_count and vaccines
    request = ScraperRequest(
        "https://pharma-api.epharmacie.pro/global/api/meetings/v2/aptiphar18-priker-magny-hameaux/slots",
        "2021-09-11",
        center_info,
        "Valwinaptiphar18-priker-magny-hameau",
    )
    center_with_availability = valwin.Slots()
    slots = json.load(io.open(Path("tests", "fixtures", "valwin", "slots_available.json"), "r", encoding="utf-8-sig"))

    assert center_with_availability.get_appointments(request, slots_api=slots) == "2021-09-17T10:00:00"
    assert request.appointment_count == 12
    assert request.vaccine_type == [Vaccine.ASTRAZENECA]

    # This one should return no date, neither appointment_count nor vaccine.
    request = ScraperRequest(
        "https://pharma-api.epharmacie.pro/global/api/meetings/v2/pharmabest75-plateau-lyon/slots",
        "2021-08-10",
        center_info,
        "Valwinpharmabest75-plateau-lyon",
    )
    center_without_availability = valwin.Slots()
    slots = json.load(
        io.open(Path("tests", "fixtures", "bimedoc", "slots_unavailable.json"), "r", encoding="utf-8-sig")
    )
    assert center_without_availability.get_appointments(request, slots_api=slots) == None
    assert request.appointment_count == 0
    assert request.vaccine_type == None
Exemplo n.º 15
0
def cherche_prochain_rdv_dans_centre(
        data: Tuple[dict, Queue]) -> CenterInfo:  # pragma: no cover
    timestamp_before_request = datetime.datetime.now()

    centre, creneau_q = data
    center_data = CenterInfo.from_csv_data(centre)
    start_date = get_start_date()
    has_error = None
    result = None
    try:
        result = fetch_centre_slots(
            centre["rdv_site_web"],
            centre["platform_is"] if "platform_is" in centre.keys() else None,
            start_date,
            creneau_q=creneau_q,
            center_info=center_data,
            input_data=centre.get("booking"),
            atlas_gid=centre["atlas_gid"]
            if "atlas_gid" in centre.keys() else None,
        )
        center_data.fill_result(result)
    except Blocked403 as blocked_doctolib__error:
        logger.error(
            f"erreur lors du traitement de la ligne avec le gid {centre['gid']} {str(blocked_doctolib__error)}"
        )
        has_error = blocked_doctolib__error

    except DoublonDoctolib as doublon_doctolib:
        has_error = doublon_doctolib
        doublon = Color("{autored}DOUBLON{/autored}")
        gid = centre.get(
            "gid", "") if len(centre.get("gid", "")) < 50 else centre.get(
                "gid", "")[0:50]
        platform = "Doctolib"
        departement = center_data.departement if center_data.departement else ""
        table_data = [[
            platform + str(" " * (15 - len(platform))),
            gid + str(" " * (50 - len(gid))),
            doublon + str(" " * (35 - len(doublon))),
            departement + str(" " * (3 - len(departement))),
        ]]

        table_instance = DoubleTable(table_data)
        table_instance.outer_border = False
        print(table_instance.table)

    except CircuitBreakerOffException as error:
        logger.error(
            f"circuit '{error.name}' désactivé lors du traîtement de la ligne avec le gid {centre['gid']}: {str(error)}"
        )
        has_error = error
    except Exception:

        logger.error(
            f"erreur lors du traitement de la ligne avec le gid {centre['gid']}"
        )
        traceback.print_exc()

    else:
        next_appointment = (Color("{autogreen}" + center_data.prochain_rdv +
                                  "{/autogreen}")
                            if center_data.prochain_rdv else "")
        gid = centre.get(
            "gid", "") if len(centre.get("gid", "")) < 50 else centre.get(
                "gid", "")[0:50]
        platform = center_data.plateforme if center_data.plateforme else "Autre"
        departement = center_data.departement if center_data.departement else ""
        table_data = [[
            platform + str(" " * (15 - len(platform))),
            gid + str(" " * (50 - len(gid))),
            next_appointment + str(" " * (35 - len(next_appointment))),
            departement + str(" " * (3 - len(departement))),
        ]]

        table_instance = DoubleTable(table_data)
        table_instance.outer_border = False
        print(table_instance.table)

    time_for_request = (datetime.datetime.now() -
                        timestamp_before_request).total_seconds()

    if result is not None and result.request.url is not None:
        center_data.url = result.request.url.lower()
        if result.request.internal_id is None:
            center_data.internal_id = f'{result.platform.lower()}{centre.get("gid", "")}'

    if "type" in centre:
        center_data.type = centre["type"]
    if not center_data.type:
        center_data.type = VACCINATION_CENTER
    center_data.gid = centre.get("gid", "")
    center_data.time_for_request = time_for_request
    return center_data