def test_keldoc_timeout(): center1_url = "https://vaccination-covid.keldoc.com/centre-hospitalier-regional/foo/bar?specialty=no" def app(request: httpx.Request) -> httpx.Response: if request.url.path == "/centre-hospitalier-regional/foo/bar": raise TimeoutError if request.url.path == "/api/patients/v2/searches/resource": raise TimeoutError if request.url.path == "/api/patients/v2/clinics/1/specialties/1/cabinets": raise TimeoutError return httpx.Response(200, json={}) client = httpx.Client(transport=httpx.MockTransport(app)) request = ScraperRequest(center1_url, "2020-04-04") test_center_1 = KeldocCenter(request, client=client) # Test center info TA with pytest.raises(TimeoutError): test_center_1.parse_resource() with pytest.raises(TimeoutError): test_center_1.fetch_center_data() # Test cabinet TA test_center_1.id = 1 test_center_1.vaccine_specialties = [1] with pytest.raises(TimeoutError): test_center_1.fetch_vaccine_cabinets() assert not test_center_1.vaccine_cabinets
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")
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')
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}, ]