Esempio n. 1
0
def test_keldoc_parse_center():
    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"))
    request = ScraperRequest(center1_url, "2020-04-04", input_data=input_data)
    client = httpx.Client(transport=httpx.MockTransport(app_center1))
    test_center_1 = KeldocCenter(request, client=client)
    # Fetch vaccine cabinets
    cabinets = filter_vaccine_motives(test_center_1.appointment_motives)
    agendas = []
    for match in cabinets:
        agendas.extend(match.get("agendas"))
    agendas = list(set(agendas))
    assert agendas == [51414, 49335]

    # Fetch motives
    motives = filter_vaccine_motives(test_center_1.appointment_motives)

    assert motives == json.loads(
        Path("tests", "fixtures", "keldoc",
             "center1-motives.json").read_text())

    # Find first availability date
    fake_now = dt.datetime(2020, 4, 4, 8, 15)
    with mock_datetime_now(fake_now):
        date, count = test_center_1.find_first_availability("2020-04-04")
    assert not date
    test_center_1.vaccine_motives = motives
    with mock_datetime_now(fake_now):
        date, count = test_center_1.find_first_availability("2020-04-04")
    tz = datetime.timezone(datetime.timedelta(seconds=7200))
    assert date == datetime.datetime(2021, 4, 20, 16, 55, tzinfo=tz)
Esempio n. 2
0
def fetch_slots(request: ScraperRequest):
    if "www.keldoc.com" in request.url:
        logger.debug(f"Fixing wrong hostname in request: {request.url}")
        request.url = request.url.replace("www.keldoc.com", "vaccination-covid.keldoc.com")
    if KELDOC_KILL_SWITCH:
        return None
    center = KeldocCenter(request, client=session)
    # Unable to parse center resources (id, location)?
    if not center.parse_resource():
        return None
    # Try to fetch center data
    if not center.fetch_center_data():
        return None

    # Filter specialties, cabinets & motives
    center.vaccine_specialties = get_relevant_vaccine_specialties_id(center.specialties)
    center.fetch_vaccine_cabinets()
    center.vaccine_motives = filter_vaccine_motives(
        session, center.selected_cabinet, center.id, center.vaccine_specialties, center.vaccine_cabinets
    )
    # Find the first availability
    date, count, appointment_schedules = center.find_first_availability(request.get_start_date())
    if not date:
        request.update_appointment_count(0)
        return None
    request.update_appointment_count(count)
    if appointment_schedules:
        request.update_appointment_schedules(appointment_schedules)
    return date.strftime("%Y-%m-%dT%H:%M:%S.%f%z")
Esempio n. 3
0
def test_keldoc_parse_complex():
    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"))
    request = ScraperRequest(center1_url, "2020-04-04", input_data=input_data)
    client = httpx.Client(transport=httpx.MockTransport(app_center1))
    test_center_1 = KeldocCenter(request, client=client)
    # Fetch vaccine cabinets
    cabinets = filter_vaccine_motives(test_center_1.appointment_motives)

    appointments = []
    data = {
        "availabilities": {
            "2021-04-20": [
                {
                    "start_time": "2021-04-20T16:53:00.000000+0200"
                },
                {
                    "start_time": "2021-04-20T16:50:00.000000+0200"
                },
                {
                    "start_time": "2021-04-20T18:59:59.000000+0200"
                },
            ],
            "2021-04-21": [{
                "start_time": "2021-04-21T08:12:12.000000+0200"
            }],
        }
    }
    availability, new_count = parse_keldoc_availability(
        test_center_1, data, appointments)
    assert availability.isoformat() == "2021-04-20T16:50:00+02:00"
Esempio n. 4
0
def fetch_slots(request: ScraperRequest, creneau_q=DummyQueue()):
    if "keldoc.com" in request.url:
        logger.debug(f"Fixing wrong hostname in request: {request.url}")
        request.url = request.url.replace("keldoc.com",
                                          "vaccination-covid.keldoc.com")
    if not PLATFORM_ENABLED:
        return None
    center = KeldocCenter(request, client=session, creneau_q=creneau_q)
    center.vaccine_motives = filter_vaccine_motives(center.appointment_motives)

    center.lieu = Lieu(
        plateforme=Plateforme[PLATFORM.upper()],
        url=request.url,
        location=request.center_info.location,
        nom=request.center_info.nom,
        internal_id=f"keldoc{request.internal_id}",
        departement=request.center_info.departement,
        lieu_type=request.practitioner_type,
        metadata=request.center_info.metadata,
        atlas_gid=request.atlas_gid,
    )

    # Find the first availability
    date, count = center.find_first_availability(request.get_start_date())
    if not date and center.lieu:
        if center.lieu:
            center.found_creneau(
                PasDeCreneau(lieu=center.lieu,
                             phone_only=request.appointment_by_phone_only))
        request.update_appointment_count(0)
        return None

    request.update_appointment_count(count)
    return date.strftime("%Y-%m-%dT%H:%M:%S.%f%z")
Esempio n. 5
0
def fetch_slots(request: ScraperRequest):
    # Keldoc needs an end date, but if no appointment are found,
    # it still returns the next available appointment. Bigger end date
    # makes Keldoc responses slower.
    date_obj = datetime.strptime(request.get_start_date(), '%Y-%m-%d')
    end_date = (date_obj +
                timedelta(days=KELDOC_SLOT_LIMIT)).strftime('%Y-%m-%d')

    center = KeldocCenter(request, client=session)
    # Unable to parse center resources (id, location)?
    if not center.parse_resource():
        return None
    # Try to fetch center data
    if not center.fetch_center_data():
        return None

    # Filter specialties, cabinets & motives
    center.vaccine_specialties = filter_vaccine_specialties(center.specialties)
    center.fetch_vaccine_cabinets()
    center.vaccine_motives = filter_vaccine_motives(session,
                                                    center.selected_cabinet,
                                                    center.id,
                                                    center.vaccine_specialties,
                                                    center.vaccine_cabinets)
    # Find the first availability
    date, count = center.find_first_availability(request.get_start_date(),
                                                 end_date)
    if not date:
        request.update_appointment_count(0)
        return None
    request.update_appointment_count(count)
    return date.strftime('%Y-%m-%dT%H:%M:%S.%f%z')
Esempio n. 6
0
def test_null_motives():
    client = DEFAULT_CLIENT
    motives = filter_vaccine_motives(
        client,
        4233,
        1,
        None,
        None
    )
    assert not motives
Esempio n. 7
0
def test_keldoc_parse_center():
    center1_url = "https://vaccination-covid.keldoc.com/centre-hospitalier-regional/lorient-56100/groupe-hospitalier" \
                  "-bretagne-sud-lorient-hopital-du-scorff?specialty=144 "

    center1_data = json.loads(Path("tests", "fixtures", "keldoc", "center1-info.json").read_text())

    request = ScraperRequest(center1_url, "2020-04-04")
    client = httpx.Client(transport=httpx.MockTransport(app_center1))
    test_center_1 = KeldocCenter(request, client=client)
    assert test_center_1.parse_resource()

    # Check if parameters are parsed correctly
    assert test_center_1.resource_params
    res = test_center_1.resource_params
    assert res['type'] == 'centre-hospitalier-regional'
    assert res['location'] == 'lorient-56100'
    assert res['slug'] == 'groupe-hospitalier-bretagne-sud-lorient-hopital-du-scorff'

    # Fetch center data (id/center specialties)
    assert test_center_1.fetch_center_data()
    assert test_center_1.id == center1_data['id']
    assert test_center_1.specialties == center1_data['specialties']

    # Filter center specialties
    filtered_specialties = filter_vaccine_specialties(test_center_1.specialties)
    assert filtered_specialties == [144]

    # Fetch vaccine cabinets
    assert not test_center_1.fetch_vaccine_cabinets()
    test_center_1.vaccine_specialties = filtered_specialties
    cabinets = test_center_1.fetch_vaccine_cabinets()
    assert cabinets == [18780, 16913, 16910, 16571, 16579]

    # Fetch motives
    motives = filter_vaccine_motives(client, test_center_1.selected_cabinet, test_center_1.id,
                                     test_center_1.vaccine_specialties, test_center_1.vaccine_cabinets)
    assert motives == json.loads(Path("tests", "fixtures", "keldoc", "center1-motives.json").read_text())

    # Find first availability date
    date, count = test_center_1.find_first_availability("2020-04-04", "2020-04-05")
    assert not date
    test_center_1.vaccine_motives = motives
    date, count = test_center_1.find_first_availability("2020-04-04", "2020-04-05")
    tz = datetime.timezone(datetime.timedelta(seconds=7200))
    assert date == datetime.datetime(2021, 4, 20, 16, 55, tzinfo=tz)
def test_keldoc_parse_center():
    center1_url = (
        "https://vaccination-covid.keldoc.com/centre-hospitalier-regional/lorient-56100/groupe-hospitalier"
        "-bretagne-sud-lorient-hopital-du-scorff?specialty=144 "
    )

    center1_data = json.loads(Path("tests", "fixtures", "keldoc", "center1-info.json").read_text())

    request = ScraperRequest(center1_url, "2020-04-04")
    client = httpx.Client(transport=httpx.MockTransport(app_center1))
    test_center_1 = KeldocCenter(request, client=client)
    assert test_center_1.parse_resource()

    # Check if parameters are parsed correctly
    assert test_center_1.resource_params
    res = test_center_1.resource_params
    assert res["type"] == "centre-hospitalier-regional"
    assert res["location"] == "lorient-56100"
    assert res["slug"] == "groupe-hospitalier-bretagne-sud-lorient-hopital-du-scorff"

    # Fetch center data (id/center specialties)
    assert test_center_1.fetch_center_data()
    assert test_center_1.id == center1_data["id"]
    assert test_center_1.specialties == center1_data["specialties"]

    # Filter center specialties
    filtered_specialties = get_relevant_vaccine_specialties_id(test_center_1.specialties)
    assert filtered_specialties == [144]

    # Fetch vaccine cabinets
    assert not test_center_1.fetch_vaccine_cabinets()
    test_center_1.vaccine_specialties = filtered_specialties
    cabinets = test_center_1.fetch_vaccine_cabinets()
    assert cabinets == [18780, 16913, 16910, 16571, 16579]

    # Fetch motives
    motives = filter_vaccine_motives(
        client,
        test_center_1.selected_cabinet,
        test_center_1.id,
        test_center_1.vaccine_specialties,
        test_center_1.vaccine_cabinets,
    )
    assert motives == json.loads(Path("tests", "fixtures", "keldoc", "center1-motives.json").read_text())

    # Find first availability date
    fake_now = dt.datetime(2020, 4, 4, 8, 15)
    with mock_datetime_now(fake_now):
        date, count, appointment_schedules = test_center_1.find_first_availability("2020-04-04")
    assert not date
    test_center_1.vaccine_motives = motives
    with mock_datetime_now(fake_now):
        date, count, appointment_schedules = test_center_1.find_first_availability("2020-04-04")
    tz = datetime.timezone(datetime.timedelta(seconds=7200))
    assert date == datetime.datetime(2021, 4, 20, 16, 55, tzinfo=tz)
    assert appointment_schedules == [
        {"name": "chronodose", "from": "2020-04-04T08:15:00+02:00", "to": "2020-04-05T08:14:59+02:00", "total": 0},
        {"name": "1_days", "from": "2020-04-04T00:00:00+02:00", "to": "2020-04-04T23:59:59+02:00", "total": 0},
        {"name": "2_days", "from": "2020-04-04T00:00:00+02:00", "to": "2020-04-05T23:59:59+02:00", "total": 0},
        {"name": "7_days", "from": "2020-04-04T00:00:00+02:00", "to": "2020-04-10T23:59:59+02:00", "total": 0},
        {"name": "28_days", "from": "2020-04-04T00:00:00+02:00", "to": "2020-05-01T23:59:59+02:00", "total": 0},
        {"name": "49_days", "from": "2020-04-04T00:00:00+02:00", "to": "2020-05-22T23:59:59+02:00", "total": 0},
    ]