Exemple #1
0
def test_get_last_scans():

    center_info1 = CenterInfo("01", "Centre 1", "https://example1.fr")
    center_info2 = CenterInfo("01", "Centre 2", "https://example2.fr")

    center_info2.prochain_rdv = "2021-06-06T00:00:00"

    centres_cherchés = [center_info1, center_info2]

    fake_now = dt.datetime(2021, 5, 5)
    with mock_datetime_now(fake_now):
        centres_cherchés = get_last_scans(centres_cherchés)

    assert centres_cherchés[0].last_scan_with_availabilities == None
    assert centres_cherchés[1].last_scan_with_availabilities == "2021-05-05T00:00:00"
Exemple #2
0
def test_doctolib_motive_categories():
    # Certains centres opèrent une distinction de motifs pour les professionnels de santé /
    # non professionnels de santé.
    # On doit gérer ces cas-là.

    start_date = "2021-04-03"
    base_url = "https://partners.doctolib.fr/centre-de-vaccinations-internationales/ville1/centre1?pid=practice-165752&enable_cookies_consent=1"  # noqa
    center_info = CenterInfo(departement="07",
                             nom="Mon Super Centre",
                             url=base_url)
    scrap_request = ScraperRequest(base_url, start_date, center_info)

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

        if request.url.path == "/booking/centre1.json":
            path = Path("tests", "fixtures", "doctolib",
                        "category-booking.json")
            return httpx.Response(200,
                                  json=json.loads(
                                      path.read_text(encoding="utf-8")))

        assert request.url.path == "/availabilities.json"
        path = Path("tests", "fixtures", "doctolib",
                    "category-availabilities.json")
        return httpx.Response(200,
                              json=json.loads(
                                  path.read_text(encoding="utf-8")))

    client = httpx.Client(transport=httpx.MockTransport(app))
    slots = DoctolibSlots(client=client, cooldown_interval=0)

    next_date = slots.fetch(scrap_request)
    assert next_date == "2021-04-10"
Exemple #3
0
def organization_to_center(organization) -> Optional[CenterInfo]:
    if organization is None:
        return None
    url = AVECMONDOC_CONF.get("patient_url", "").format(slug=organization.get("slug"))
    id = organization["id"]
    zip = organization["zipCode"]
    dept = departementUtils.to_departement_number(departementUtils.cp_to_insee(zip))
    reasons = organization["consultationReasons"]
    if reasons is None:
        logger.warning(f"no reasons found in organization")
        return None
    if get_valid_reasons(reasons) == []:
        return None
    center = CenterInfo(dept, organization["name"], url)
    location = CenterLocation(0, 0, organization["city"], organization["zipCode"])
    if organization.get("coordinates") is not None:
        location.longitude = organization["coordinates"].get("lng", 0.0)
        location.latitude = organization["coordinates"].get("lat", 0.0)
    center.metadata = {
        "address": organization["address"],
        "phone_number": organization["phone"],
    }
    center.location = location
    center.internal_id = f"amd{id}"
    if "schedules" not in organization:
        return center
    business_hours = {}
    for day, day_name in AVECMONDOC_SCRAPER.get("business_days", {}).items():
        value = ""
        if organization["schedules"][day]["enabled"]:
            value = " ".join(f'{sc["start"]}-{sc["end"]}' for sc in organization["schedules"][day]["schedules"])
        business_hours[day_name] = value
    center.metadata["business_hours"] = business_hours
    return center
Exemple #4
0
def test_doctolib_next_slot():
    # Cas de repli : c'est surprenant, mais parfois la liste des dispos
    # est vide, mais il y a un champ 'next_slot' qui contient la date de
    # la prochaine visite, que l'on utilise dans ce cas.

    start_date = "2021-04-03"
    base_url = "https://partners.doctolib.fr/centre-de-vaccinations-internationales/ville1/centre1?pid=practice-165752&enable_cookies_consent=1"  # noqa
    center_info = CenterInfo(departement="07",
                             nom="Mon Super Centre",
                             url=base_url)
    scrap_request = ScraperRequest(base_url, start_date, center_info)

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

        if request.url.path == "/booking/centre1.json":
            path = Path("tests", "fixtures", "doctolib",
                        "next-slot-booking.json")
            return httpx.Response(200,
                                  json=json.loads(
                                      path.read_text(encoding="utf-8")))

        assert request.url.path == "/availabilities.json"
        path = Path("tests", "fixtures", "doctolib",
                    "next-slot-availabilities.json")
        return httpx.Response(200,
                              json=json.loads(
                                  path.read_text(encoding="utf-8")))

    client = httpx.Client(transport=httpx.MockTransport(app))
    slots = DoctolibSlots(client=client, cooldown_interval=0)

    next_date = slots.fetch(scrap_request)
    # Next slot should not be used
    assert next_date is None
Exemple #5
0
def test_center_to_centerdict():
    center = CenterInfo(
        "28", "Delphine ROUSSEAU",
        "https://patient.avecmondoc.com/fiche/structure/delphine-rousseau-159")
    center.metadata = {
        "address": "21 Rue Nicole 28000 Chartres",
        "phone_number": "0033143987678",
        "business_hours": {
            "Lundi": "08:30-12:30 13:30-17:00",
            "Mardi": "08:30-12:30 13:30-17:00",
            "Mercredi": "08:30-12:30 13:30-17:00",
            "Jeudi": "08:30-12:30 13:30-17:00",
            "Vendredi": "08:30-12:30 13:30-17:00",
            "Samedi": "",
            "Dimanche": "",
        },
    }
    center.location = CenterLocation(1.481373, 48.447586, "Chartres", "28000")
    center.internal_id = "amd159"
    center.type = DRUG_STORE

    data_file = Path("tests/fixtures/avecmondoc/centerdict.json")
    data = json.loads(data_file.read_text(encoding="utf8"))

    assert center_to_centerdict(center) == data
Exemple #6
0
def test_blocked_by_doctolib_par_availabilities():
    # Cas de base.

    start_date = "2021-04-03"
    base_url = "https://partners.doctolib.fr/centre-de-vaccinations-internationales/ville1/centre1?pid=practice-165752&enable_cookies_consent=1"  # noqa
    center_info = CenterInfo(departement="07",
                             nom="Mon Super Centre",
                             url=base_url)
    scrap_request = ScraperRequest(base_url, start_date, center_info)

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

        if request.url.path == "/booking/centre1.json":
            path = Path("tests", "fixtures", "doctolib", "basic-booking.json")
            return httpx.Response(200,
                                  json=json.loads(
                                      path.read_text(encoding="utf-8")))

        return httpx.Response(403, text="Anti dDos")

    client = httpx.Client(transport=httpx.MockTransport(app))
    slots = DoctolibSlots(client=client, cooldown_interval=0)

    with pytest.raises(Blocked403):
        slots.fetch(scrap_request)
Exemple #7
0
def test_center_info_next_availability():
    center = CenterInfo("Paris", "Centre 1", "https://.../centre")
    center.prochain_rdv = "TEST"
    data = center.handle_next_availability()
    assert not data
    center.prochain_rdv = "2021-06-06"
    data = center.handle_next_availability()
    assert center.prochain_rdv == "2021-06-06"
    center.prochain_rdv = "2042-04-10T00:00:00"
    data = center.handle_next_availability()
    assert center.prochain_rdv is None
def test_fetch_slots():
    def app(request: httpx.Request) -> httpx.Response:
        try:
            with open(Path("tests", "fixtures", "mapharma", "slots.json"),
                      encoding="utf8") as f:
                return httpx.Response(200, content=f.read())
        except IOError:
            return httpx.Response(404, content="")

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

    center_info = CenterInfo(
        departement="49",
        nom="Pharmacie Grand Maine",
        url="https://mapharma.net/49100-3?c=257&l=0",
        location=CenterLocation(longitude=-0.595416,
                                latitude=47.468377,
                                city="ANGERS",
                                cp="49100"),
        metadata={
            "address":
            "CENTRE CIAL GRAND MAINE RUE DU GRAND LAUNAY, 49100 ANGERS",
            "business_hours": {
                "lundi": "09:30 – 20:00",
                "mardi": "09:30 – 20:00",
                "mercredi": "09:30 – 20:00",
                "jeudi": "09:30 – 20:00",
                "vendredi": "09:30 – 20:00",
                "samedi": "09:30 – 20:00",
                "dimanche": "Fermé",
            },
        },
    )

    request = ScraperRequest(url="https://mapharma.net/97200?c=60&l=1",
                             start_date="2021-04-14",
                             center_info=center_info)
    first_availability = fetch_slots(request,
                                     creneau_q=DummyQueue(),
                                     client=client,
                                     opendata_file=TEST_OPEN_DATA_FILE)

    assert first_availability == "2021-04-19T17:15:00"

    # test campagne["total_libres"]: 0
    request = ScraperRequest("https://mapharma.net/88400?c=92&l=1",
                             "2021-04-14",
                             center_info=center_info)
    first_availability = fetch_slots(request,
                                     client=client,
                                     opendata_file=TEST_OPEN_DATA_FILE)
    assert first_availability == None
Exemple #9
0
def test_doctolib_sends_creneau():
    # Given
    start_date = "2021-04-03"
    base_url = "https://partners.doctolib.fr/centre-de-vaccinations-internationales/ville1/centre1?pid=practice-165752&enable_cookies_consent=1"  # noqa
    center_info = CenterInfo(departement="07",
                             nom="Mon Super Centre",
                             url=base_url)
    scrap_request = ScraperRequest(base_url, start_date, center_info)

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

        if request.url.path == "/booking/centre1.json":
            path = Path("tests", "fixtures", "doctolib", "basic-booking.json")
            return httpx.Response(200,
                                  json=json.loads(
                                      path.read_text(encoding="utf-8")))

        assert request.url.path == "/availabilities.json"
        path = Path("tests", "fixtures", "doctolib",
                    "basic-availabilities.json")
        return httpx.Response(200,
                              json=json.loads(
                                  path.read_text(encoding="utf-8")))

    client = httpx.Client(transport=httpx.MockTransport(app))
    q = SimpleQueue()
    slots = DoctolibSlots(client=client, cooldown_interval=0, creneau_q=q)

    # When
    slots.fetch(scrap_request)
    actual = []
    while not q.empty():
        actual.append(q.get())
    # Then
    assert len(actual) == 1
    assert actual[0] == Creneau(
        reservation_url=base_url,
        horaire=dateutil.parser.parse("2021-04-10T21:45:00.000+02:00"),
        type_vaccin=[Vaccine.PFIZER],
        lieu=Lieu(
            departement="07",
            plateforme=Plateforme.DOCTOLIB,
            url=base_url,
            nom="Mon Super Centre",
            internal_id="doctolib123456789pid165752",
            lieu_type="vaccination-center",
        ),
        dose=["1"],
    )
 def centre(self, lieu: Lieu):
     return CenterInfo(
         departement=lieu.departement,
         nom=lieu.nom,
         url=lieu.url,
         location=self.location_to_dict(lieu.location),
         metadata=lieu.metadata,
         prochain_rdv=None,
         plateforme=lieu.plateforme.value,
         type=lieu.lieu_type,
         appointment_count=0,
         internal_id=lieu.internal_id,
         vaccine_type=[],
         erreur=None,
         atlas_gid=lieu.atlas_gid,
     )
Exemple #11
0
def test_get_first_availability():

    request = ScraperRequest(
        url=
        "https://www.maiia.com/centre-de-vaccination/42400-saint-chamond/centre-de-vaccination-covid---hopital-du-gier-?centerid=5ffc744c68dedf073a5b87a2",
        start_date="2021-07-17",
        center_info=CenterInfo(
            departement="42",
            nom="Centre de vaccination COVID - Hôpital du Gier ",
            url=
            "https://www.maiia.com/centre-de-vaccination/42400-saint-chamond/centre-de-vaccination-covid---hopital-du-gier-?centerid=5ffc744c68dedf073a5b87a2",
            location=CenterLocation(longitude=4.50184989506,
                                    latitude=45.4698319517,
                                    city="Saint-Chamond",
                                    cp="42400"),
            metadata={
                "address": "19 rue victor hugo 42400 Saint-Chamond",
                "business_hours": {
                    "Lundi": "08:15-17:15",
                    "Mardi": "08:15-17:15",
                    "Mercredi": "08:15-17:15",
                    "Jeudi": "08:15-17:15",
                    "Vendredi": "08:15-17:15",
                    "Samedi": "08:15-17:15",
                    "Dimanche": "08:15-17:15",
                },
            },
            type="vaccination-center",
            internal_id="5ffc744c",
        ),
    )
    reasons = get_reasons("5ffc744c68dedf073a5b87a2",
                          limit=MAIIA_LIMIT,
                          client=client)
    instance = MaiiaSlots(creneau_q=DummyQueue, client=None)
    fake_now = dt.datetime(2021, 4, 29, 18, 20)
    with mock_datetime_now(fake_now):
        first_availability, slots_count = instance.get_first_availability(
            "5ffc744c68dedf073a5b87a2",
            "2021-04-29",
            reasons,
            client=client,
            request=request)

    assert slots_count == 7182
    assert first_availability.isoformat() == "2021-05-13T13:40:00+00:00"
Exemple #12
0
def test_fetch_slots():
    def app(request: httpx.Request) -> httpx.Response:
        if request.url.path == "/api/BusinessHours/availabilitiesPerDay":
            path = Path("tests/fixtures/avecmondoc/get_availabilities.json")
            return httpx.Response(200,
                                  json=json.loads(
                                      path.read_text(encoding="utf8")))
        if request.url.path == "/api/Organizations/slug/delphine-rousseau-159":
            path = Path("tests/fixtures/avecmondoc/get_organization_slug.json")
            return httpx.Response(200,
                                  json=json.loads(
                                      path.read_text(encoding="utf8")))
        return httpx.Response(404)

    center_info = CenterInfo(
        departement="69",
        nom="Pharmacie AVENUE (Picard)",
        url=
        "https://patient.avecmondoc.com/fiche/structure/pharmacie-avenue-picard-583",
        location=CenterLocation(longitude=4.77231,
                                latitude=45.742216,
                                city="Francheville",
                                cp="69340"),
        metadata={
            "address": "29 avenue du Chater, 69340 Francheville",
            "business_hours": {
                "Lundi": "08:45-12:15 15:00-19:15",
                "Mardi": "08:45-12:15 15:00-19:15",
                "Mercredi": "08:45-12:15 15:00-19:15",
                "Jeudi": "08:45-12:15 15:00-19:15",
                "Vendredi": "08:45-12:15 15:00-19:15",
                "Samedi": "08:45-12:15",
                "Dimanche": "",
            },
            "phone_number": "+33478342931",
        },
        plateforme="Avecmondoc",
        type="drugstore",
        internal_id="amd583",
    )
    client = httpx.Client(transport=httpx.MockTransport(app))
    url = "https://patient.avecmondoc.com/fiche/structure/delphine-rousseau-159"
    request = ScraperRequest(url, "2021-05-20", center_info=center_info)
    first_availability = fetch_slots(request, client=client)
    assert first_availability == "2021-05-20T09:00:00+00:00"
    assert request.vaccine_type == ["Pfizer-BioNTech", "Janssen"]
Exemple #13
0
def test_center_info_fill():
    center = CenterInfo("Paris", "Centre 1", "https://.../centre")
    newloc = CenterLocation(1.122, 2.391, "Ok", "Cp")
    request = ScraperRequest(center.url, "2021-05-04")
    result = ScraperResult(request, "Doctolib", "2021-05-06")
    center.fill_localization(newloc)
    request.update_appointment_count(42)
    request.add_vaccine_type(Vaccine.PFIZER)
    request.add_vaccine_type(Vaccine.ASTRAZENECA)
    request.add_vaccine_type(Vaccine.MODERNA)
    request.update_internal_id("doctolibcentre1")
    request.update_practitioner_type(DRUG_STORE)
    request.set_appointments_only_by_phone(False)
    center.fill_result(result)

    assert center.location == newloc
    assert center.prochain_rdv == "2021-05-06"
    assert center.plateforme == "Doctolib"
    assert center.type == "drugstore"
    assert center.appointment_count == 42
    assert center.internal_id == "doctolibcentre1"
    assert center.vaccine_type == ["Pfizer-BioNTech", "AstraZeneca", "Moderna"]
    assert not center.appointment_by_phone_only
    assert center.default() == {
        "departement": "Paris",
        "nom": "Centre 1",
        "url": "https://.../centre",
        "location": {
            "longitude": 1.122,
            "latitude": 2.391,
            "city": "Ok",
            "cp": "Cp"
        },
        "metadata": None,
        "prochain_rdv": "2021-05-06",
        "plateforme": "Doctolib",
        "type": "drugstore",
        "appointment_count": 42,
        "internal_id": "doctolibcentre1",
        "vaccine_type": ["Pfizer-BioNTech", "AstraZeneca", "Moderna"],
        "appointment_by_phone_only": False,
        "erreur": None,
        "last_scan_with_availabilities": None,
        "appointment_schedules": None,
        "request_counts": None,
    }
def test_convert_ordoclic():
    center = CenterInfo("Paris", "Centre 1", "https://.../centre")
    data = {
        '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 = convert_ordoclic_to_center_info(data, center)
    assert center.metadata['address'] == '12 Avenue de la ville, 22000 Foobar'
    assert center.metadata['phone_number'] == '+33606060606'
    assert center.metadata['business_hours'] is None
Exemple #15
0
def test_convert_ordoclic():
    center = CenterInfo("Paris", "Centre 1", "https://.../centre")
    data = {
        "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 = convert_ordoclic_to_center_info(data, center)
    assert center.metadata["address"] == "12 Avenue de la ville, 22000 Foobar"
    assert center.metadata["phone_number"] == "+33606060606"
    assert center.metadata["business_hours"] is None
def test_center_info_fill():
    center = CenterInfo("Paris", "Centre 1", "https://.../centre")
    newloc = CenterLocation(1.122, 2.391, "Ok")
    request = ScraperRequest(center.url, "2021-05-04")
    result = ScraperResult(request, "Doctolib", "2021-05-06")
    center.fill_localization(newloc)
    request.update_appointment_count(42)
    request.add_vaccine_type(Vaccine.PFIZER)
    request.add_vaccine_type(Vaccine.ASTRAZENECA)
    request.add_vaccine_type(Vaccine.MODERNA)
    request.update_internal_id("doctolibcentre1")
    request.update_practitioner_type(DRUG_STORE)
    request.set_appointments_only_by_phone(False)
    center.fill_result(result)

    assert center.location == newloc
    assert center.prochain_rdv == "2021-05-06"
    assert center.plateforme == "Doctolib"
    assert center.type == "drugstore"
    assert center.appointment_count == 42
    assert center.internal_id == "doctolibcentre1"
    assert center.vaccine_type == ["Pfizer-BioNTech", "AstraZeneca", "Moderna"]
    assert not center.appointment_by_phone_only
    assert center.default() == {
        'departement': 'Paris',
        'nom': 'Centre 1',
        'url': 'https://.../centre',
        'location': {
            'longitude': 1.122,
            'latitude': 2.391,
            'city': 'Ok'
        },
        'metadata': None,
        'prochain_rdv': '2021-05-06',
        'plateforme': 'Doctolib',
        'type': 'drugstore',
        'appointment_count': 42,
        'internal_id': 'doctolibcentre1',
        'vaccine_type': ['Pfizer-BioNTech', 'AstraZeneca', 'Moderna'],
        'appointment_by_phone_only': False,
        'erreur': None,
        'last_scan_with_availabilities': None,
        'appointment_schedules': None
    }
Exemple #17
0
def test_blocked_by_doctolib_par_centre():
    # Cas de base.

    start_date = "2021-04-03"
    base_url = "https://partners.doctolib.fr/centre-de-vaccinations-internationales/ville1/centre1?pid=practice-165752&enable_cookies_consent=1"  # noqa
    center_info = CenterInfo(departement="07",
                             nom="Mon Super Centre",
                             url=base_url)
    scrap_request = ScraperRequest(base_url, start_date, center_info)

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

        if request.url.path == "/booking/centre1.json":
            path = Path("tests", "fixtures", "doctolib", "basic-booking.json")
            return httpx.Response(403, text="Anti dDos")

        assert request.url.path == "/availabilities.json"
        params = dict(httpx.QueryParams(request.url.query))
        assert params == {
            "start_date": start_date,
            "visit_motives_ids": "2",
            "agenda_ids": "3",
            "insurance_sector": "public",
            "practice_ids": "4",
            "destroy_temporary": "true",
            "limit": str(DOCTOLIB_CONF.pagination["pages"]),
        }
        path = Path("tests", "fixtures", "doctolib",
                    "basic-availabilities.json")
        return httpx.Response(200,
                              json=json.loads(
                                  path.read_text(encoding="utf-8")))

    client = httpx.Client(transport=httpx.MockTransport(app))
    slots = DoctolibSlots(client=client, cooldown_interval=0)

    with pytest.raises(Blocked403):
        slots.fetch(scrap_request)
Exemple #18
0
def test_organization_to_center():
    data_file = Path("tests/fixtures/avecmondoc/get_organization_slug.json")
    data = json.loads(data_file.read_text(encoding='utf8'))
    center = CenterInfo(
        "28", 
        "Delphine ROUSSEAU", 
        "https://patient.avecmondoc.com/fiche/structure/delphine-rousseau-159"
    )
    center.metadata = {
        "address": "21 Rue Nicole 28000 Chartres",
        "phone_number": "0033143987678",
        "business_hours": {
            "Lundi": "08:30-12:30 13:30-17:00",
            "Mardi": "08:30-12:30 13:30-17:00",
            "Mercredi": "08:30-12:30 13:30-17:00",
            "Jeudi": "08:30-12:30 13:30-17:00",
            "Vendredi": "08:30-12:30 13:30-17:00",
            "Samedi": "",
            "Dimanche": "",
            }
    }
    center.location = CenterLocation(1.481373, 48.447586, "Chartres", "28000")
    center.internal_id = "amd159"
    assert avecmondoc.organization_to_center(data).default() == center.default()
Exemple #19
0
def test_fetch_slots():
    # Basic full working test
    def app(request: httpx.Request) -> httpx.Response:
        if request.url.path == "/v1/public/entities/profile/pharmacie-oceane-paris":
            return httpx.Response(
                200, json=json.loads(Path("tests/fixtures/ordoclic/fetchslot-profile.json").read_text())
            )
        if request.url.path == "/v1/solar/entities/03674d71-b200-4682-8e0a-3ab9687b2b59/reasons":
            return httpx.Response(
                200, json=json.loads(Path("tests/fixtures/ordoclic/fetchslot-reasons.json").read_text())
            )
        if request.url.path == "/v1/solar/slots/availableSlots":
            return httpx.Response(
                200, json=json.loads(Path("tests/fixtures/ordoclic/fetchslot-slots.json").read_text())
            )
        return httpx.Response(403, json={})

    client = httpx.Client(transport=httpx.MockTransport(app))
    center_info = CenterInfo(
        departement="51",
        nom="Pharmacie Croix Dampierre",
        url="https://app.ordoclic.fr/app/pharmacie/pharmacie-croix-dampierre-chalons-en-champagne",
        location=CenterLocation(longitude=4.3858888, latitude=48.9422828, city="chalons-en-champagne", cp="51000"),
        metadata={
            "address": "AV DU PRESIDENT ROOSEVELT CC CROIX DAMPIERRE, 51000 CHALONS EN CHAMPAGNE",
            "phone_number": "+33326219000",
            "business_hours": None,
        },
    )

    request = ScraperRequest("https://app.ordoclic.fr/app/pharmacie/pharmacie-oceane-paris", "2021-05-08", center_info)
    res = fetch_slots(request, client=client)
    assert res == "2021-05-12T16:00:00+00:00"

    # Timeout test
    def app2(request: httpx.Request) -> httpx.Response:
        if request.url.path == "/v1/public/entities/profile/pharmacie-oceane-paris":
            return httpx.Response(
                200, json=json.loads(Path("tests/fixtures/ordoclic/fetchslot-profile.json").read_text())
            )
        if request.url.path == "/v1/solar/entities/03674d71-b200-4682-8e0a-3ab9687b2b59/reasons":
            return httpx.Response(
                200, json=json.loads(Path("tests/fixtures/ordoclic/fetchslot-reasons.json").read_text())
            )
        if request.url.path == "/v1/solar/slots/availableSlots":
            raise httpx.TimeoutException(message="Timeout", request=request)
        return httpx.Response(403, json={})

    client = httpx.Client(transport=httpx.MockTransport(app2))
    request = ScraperRequest("https://app.ordoclic.fr/app/pharmacie/pharmacie-oceane-paris", "2021-05-08", center_info)
    res = fetch_slots(request, client=client)
    assert res is None

    # HTTP error test (available slots)
    def app3(request: httpx.Request) -> httpx.Response:
        if request.url.path == "/v1/public/entities/profile/pharmacie-oceane-paris":
            return httpx.Response(
                200, json=json.loads(Path("tests/fixtures/ordoclic/fetchslot-profile.json").read_text())
            )
        if request.url.path == "/v1/solar/entities/03674d71-b200-4682-8e0a-3ab9687b2b59/reasons":
            return httpx.Response(
                200, json=json.loads(Path("tests/fixtures/ordoclic/fetchslot-reasons.json").read_text())
            )
        return httpx.Response(403, json={})

    client = httpx.Client(transport=httpx.MockTransport(app3))
    request = ScraperRequest("https://app.ordoclic.fr/app/pharmacie/pharmacie-oceane-paris", "2021-05-08", center_info)
    res = fetch_slots(request, client=client)
    assert res is None

    # HTTP error test (profile)
    def app4(request: httpx.Request) -> httpx.Response:
        return httpx.Response(403, json={})

    client = httpx.Client(transport=httpx.MockTransport(app4))
    request = ScraperRequest("https://app.ordoclic.fr/app/pharmacie/pharmacie-oceane-paris", "2021-05-08", center_info)
    res = fetch_slots(request, client=client)
    assert res is None

    # Only appointments by phone test
    def app5(request: httpx.Request) -> httpx.Response:
        if request.url.path == "/v1/public/entities/profile/pharmacie-oceane-paris":
            return httpx.Response(
                200, json=json.loads(Path("tests/fixtures/ordoclic/fetchslot-profile2.json").read_text())
            )
        if request.url.path == "/v1/solar/entities/03674d71-b200-4682-8e0a-3ab9687b2b59/reasons":
            return httpx.Response(
                200, json=json.loads(Path("tests/fixtures/ordoclic/fetchslot-reasons.json").read_text())
            )
        if request.url.path == "/v1/solar/slots/availableSlots":
            return httpx.Response(
                200, json=json.loads(Path("tests/fixtures/ordoclic/fetchslot-slots.json").read_text())
            )
        return httpx.Response(403, json={})

    client = httpx.Client(transport=httpx.MockTransport(app5))
    request = ScraperRequest("https://app.ordoclic.fr/app/pharmacie/pharmacie-oceane-paris", "2021-05-08", center_info)
    res = fetch_slots(request, client=client)
    assert res is None
def test_fetch_centre_slots():
    """
    We detect which implementation to use based on the visit URL.
    """
    def fake_doctolib_fetch_slots(request: ScraperRequest, sdate, **kwargs):
        return "2021-04-04"

    def fake_keldoc_fetch_slots(request: ScraperRequest, sdate, **kwargs):
        return "2021-04-05"

    def fake_maiia_fetch_slots(request: ScraperRequest, sdate, **kwargs):
        return "2021-04-06"

    fetch_map = {
        "Doctolib": {
            "urls":
            ["https://partners.doctolib.fr", "https://www.doctolib.fr"],
            "scraper_ptr": fake_doctolib_fetch_slots,
        },
        "Keldoc": {
            "urls":
            ["https://vaccination-covid.keldoc.com", "https://keldoc.com"],
            "scraper_ptr": fake_keldoc_fetch_slots,
        },
        "Maiia": {
            "urls": ["https://www.maiia.com"],
            "scraper_ptr": fake_maiia_fetch_slots
        },
    }

    start_date = "2021-04-03"
    center_info = CenterInfo(departement="08",
                             nom="Mon Centre",
                             url="https://some.url/")

    # Doctolib
    url = "https://partners.doctolib.fr/blabla"
    res = fetch_centre_slots(url,
                             None,
                             start_date,
                             fetch_map=fetch_map,
                             creneau_q=DummyQueue(),
                             center_info=center_info)
    assert res.platform == "Doctolib"
    assert res.next_availability == "2021-04-04"

    # Doctolib (old)
    url = "https://www.doctolib.fr/blabla"
    res = fetch_centre_slots(url,
                             None,
                             start_date,
                             fetch_map=fetch_map,
                             creneau_q=DummyQueue(),
                             center_info=center_info)
    assert res.platform == "Doctolib"
    assert res.next_availability == "2021-04-04"

    # Keldoc
    url = "https://vaccination-covid.keldoc.com/blabla"
    res = fetch_centre_slots(url,
                             None,
                             start_date,
                             fetch_map=fetch_map,
                             creneau_q=DummyQueue(),
                             center_info=center_info)
    assert res.platform == "Keldoc"
    assert res.next_availability == "2021-04-05"

    # Maiia
    url = "https://www.maiia.com/blabla"
    res = fetch_centre_slots(url,
                             None,
                             start_date,
                             fetch_map=fetch_map,
                             creneau_q=DummyQueue(),
                             center_info=center_info)
    assert res.platform == "Maiia"
    assert res.next_availability == "2021-04-06"

    # Default / unknown
    url = "https://www.example.com"
    res = fetch_centre_slots(url,
                             None,
                             start_date,
                             fetch_map=fetch_map,
                             creneau_q=DummyQueue(),
                             center_info=center_info)
    assert res.platform == "Autre"
    assert res.next_availability is None
Exemple #21
0
def merge_centers(centers: list, center_dicts):
    for center_dict in center_dicts:
        centers.append(CenterInfo(**center_dict))
    return centers
def test_export_data_when_blocked(tmp_path):
    center_info1 = CenterInfo("59", "Clinique du Cambresis",
                              "https://example.com/clinique-du-cambresis")
    center_info1.plateforme = "Maiia"
    center_info1.prochain_rdv = "2021-04-12:00:00"
    center_info1.erreur = None
    center_info1.appointment_count = 1

    center_info2 = CenterInfo("14", "Hôpital magique",
                              "https://example.com/hopital-magique")
    center_info2.plateforme = "Doctolib"
    center_info2.prochain_rdv = None
    center_info2.erreur = BlockedByDoctolibError(
        "https://example.com/hopital-magique")
    centres_cherchés = [center_info1, center_info2]

    out_dir = tmp_path / "out"
    out_dir.mkdir()
    outpath_format = str(out_dir / "{}.json")

    fake_now = dt.datetime(2021, 4, 4)
    with mock_datetime_now(fake_now):
        total, actifs, bloqués = export_data(centres_cherchés,
                                             outpath_format=outpath_format)

    # les totaux doivent être bons
    assert total == 2
    assert actifs == 1
    assert bloqués == 1

    # Departements 14 and 59 should contain expected data.
    content = json.loads((out_dir / "14.json").read_text())
    assert content == {
        "version":
        1,
        "last_updated":
        "2021-04-04T00:00:00",
        "centres_disponibles": [],
        "centres_indisponibles": [{
            "departement":
            "14",
            "nom":
            "Hôpital magique",
            "url":
            "https://example.com/hopital-magique",
            "location":
            None,
            "metadata":
            None,
            "prochain_rdv":
            None,
            "type":
            None,
            "plateforme":
            "Doctolib",
            "appointment_count":
            0,
            "internal_id":
            None,
            "vaccine_type":
            None,
            "appointment_by_phone_only":
            False,
            "erreur":
            "ERREUR DE SCRAPPING (Doctolib): Doctolib bloque nos appels: 403 https://example.com/hopital-magique"
        }],
        "doctolib_bloqué":
        True
    }

    content = json.loads((out_dir / "59.json").read_text())
    assert content == {
        "version":
        1,
        "centres_disponibles": [
            {
                "departement": "59",
                "nom": "Clinique du Cambresis",
                "url": "https://example.com/clinique-du-cambresis",
                "plateforme": "Maiia",
                "prochain_rdv": "2021-04-12:00:00",
                "location": None,
                "metadata": None,
                "type": None,
                "appointment_count": 1,
                "internal_id": None,
                "appointment_by_phone_only": False,
                "vaccine_type": None,
                "erreur": None
            },
        ],
        "centres_indisponibles": [],
        "last_updated":
        "2021-04-04T00:00:00",
    }
Exemple #23
0
def test_fetch_slots():

    # Oops I forgot centerid
    request = ScraperRequest(
        url=
        "https://www.maiia.com/centre-de-vaccination/42400-saint-chamond/centre-de-vaccination-covid---hopital-du-gier-",
        start_date="2021-04-16",
        center_info=CenterInfo(
            departement="42",
            nom="Centre de vaccination COVID - Hôpital du Gier ",
            url=
            "https://www.maiia.com/centre-de-vaccination/42400-saint-chamond/centre-de-vaccination-covid---hopital-du-gier-?centerid=5ffc744c68dedf073a5b87a2",
            location=CenterLocation(longitude=4.50184989506,
                                    latitude=45.4698319517,
                                    city="Saint-Chamond",
                                    cp="42400"),
            metadata={
                "address": "19 rue victor hugo 42400 Saint-Chamond",
                "business_hours": {
                    "Lundi": "08:15-17:15",
                    "Mardi": "08:15-17:15",
                    "Mercredi": "08:15-17:15",
                    "Jeudi": "08:15-17:15",
                    "Vendredi": "08:15-17:15",
                    "Samedi": "08:15-17:15",
                    "Dimanche": "08:15-17:15",
                },
            },
            type="vaccination-center",
            internal_id="5ffc744c",
        ),
    )
    first_availability = fetch_slots(request, client=client)
    assert first_availability == None

    request = ScraperRequest(
        url=
        "https://www.maiia.com/centre-de-vaccination/42400-saint-chamond/centre-de-vaccination-covid---hopital-du-gier-?centerid=5ffc744c68dedf073a5b87a2",
        start_date="2021-04-16",
        center_info=CenterInfo(
            departement="42",
            nom="Centre de vaccination COVID - Hôpital du Gier ",
            url=
            "https://www.maiia.com/centre-de-vaccination/42400-saint-chamond/centre-de-vaccination-covid---hopital-du-gier-?centerid=5ffc744c68dedf073a5b87a2",
            location=CenterLocation(longitude=4.50184989506,
                                    latitude=45.4698319517,
                                    city="Saint-Chamond",
                                    cp="42400"),
            metadata={
                "address": "19 rue victor hugo 42400 Saint-Chamond",
                "business_hours": {
                    "Lundi": "08:15-17:15",
                    "Mardi": "08:15-17:15",
                    "Mercredi": "08:15-17:15",
                    "Jeudi": "08:15-17:15",
                    "Vendredi": "08:15-17:15",
                    "Samedi": "08:15-17:15",
                    "Dimanche": "08:15-17:15",
                },
            },
            type="vaccination-center",
            internal_id="5ffc744c",
        ),
    )
    print(request.center_info)

    first_availability = fetch_slots(request, client=client)
    assert first_availability == "2021-05-13T13:40:00+00:00"