-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmaccabi_scraper.py
More file actions
157 lines (127 loc) · 5.63 KB
/
Copy pathmaccabi_scraper.py
File metadata and controls
157 lines (127 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import requests
import json
import simplejson
import time
import uuid
import multiprocessing
import argparse
# --- Configuration ---
CITIES_PATH = "city_codes.json"
FIELDS_PATH = "field_codes.json"
SEARCH_API_URL = "https://serguide.maccabi4u.co.il/webapi/api/SearchPage/GetSearchPageSearch/"
CALENDAR_API_URL = "https://serguide.maccabi4u.co.il/webapi/api/Appointments/GetDoctorCalendarRIV"
OUTPUT_FILENAME = "maccabi_full_data_with_appointments.json"
REQUEST_DELAY_SECONDS = 1 # Delay between requests to be respectful to the server.
parser = argparse.ArgumentParser()
parser.add_argument("--num_cities", type=int, default=-1, help="How many cities to query")
parser.add_argument("--num_fields", type=int, default=-1, help="How many fields to query")
parser.add_argument("--processes", "-j", type=int, default = 8, help="Number of processes to query http with")
def get_doctors_for_criteria(session, city_code, field_code):
"""
Fetches all doctors for a given city and field, handling pagination.
"""
doctors = []
page_number = 1
total_pages = 1
while page_number <= total_pages:
payload = {
"ChapterId": "001",
"City": city_code,
"Field": field_code,
"InitiatorCode": "001",
"IsMobileApplication": 0,
"ModuleName": "doctorssearchresults",
"PageNumber": str(page_number),
"RequestId": str(uuid.uuid4()),
"Source": "SearchPage",
"isKosher": 0
}
try:
response = session.post(SEARCH_API_URL, json=payload, timeout=30)
response.raise_for_status()
data = response.json()
if data.get("Items"):
doctors.extend(data["Items"])
if page_number == 1:
num_of_pages = data.get("NumOfPages", 1)
if num_of_pages > 0:
total_pages = num_of_pages
page_number += 1
time.sleep(REQUEST_DELAY_SECONDS)
except requests.exceptions.RequestException as e:
print(f" - Error fetching doctors page {page_number}: {e}")
break
return doctors
def get_doctor_calendar(session, doctor):
"""
Fetches the appointment calendar for a single doctor.
"""
if not doctor.get("EmployeeNumber") or not doctor.get("PositionId") or not doctor.get("PROFAREAS"):
return {"error": "Missing required information for calendar lookup"}
# We need to guess the correct 'cpt' code. Let's try the first one from PROFAREAS.
cpt_code = doctor["PROFAREAS"][0]
payload = {
"cpt": str(cpt_code),
"drId": str(doctor["EmployeeNumber"]),
"positionId": str(doctor["PositionId"]),
"requestId": str(uuid.uuid4())
}
try:
response = session.post(CALENDAR_API_URL, json=payload, timeout=20)
response.raise_for_status()
calendar_data = response.json()
time.sleep(REQUEST_DELAY_SECONDS)
return calendar_data
except requests.exceptions.RequestException as e:
print(f" - Error fetching calendar: {e}. {payload=}")
return {"error": str(e)}
def scrape_city(city_code, city_name, fields, session_headers, num_fields):
session = requests.Session()
session.headers.update(session_headers)
scraped_data = {}
try:
scraped_data[city_code] = {}
fields_list = list(fields.items())
if num_fields != -1:
fields_list = fields_list[:num_fields]
for j, (field_code, field_name) in enumerate(fields_list):
print(f" -> Processing Field: {field_name} ({field_code}) in city {city_name} ({city_code}) - {j+1} / {len(fields)}")
doctors_list = get_doctors_for_criteria(session, city_code, field_code)
# Store the results in the nested structure
scraped_data[city_code][field_code] = {
"doctors": doctors_list,
"count": len(doctors_list)
}
except Exception as e:
print(f"Error occured while scraping for city {city_name} ({city_code}): {e}")
return scraped_data
def main(num_cities, num_fields, processes):
"""
Main function to orchestrate the scraping process.
"""
scraped_data = {}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Content-Type': 'application/json',
'Accept-Language': 'en-US,en;q=0.9,he;q=0.8',
'Referer': 'https://serguide.maccabi4u.co.il/heb/doctors/doctorssearchresults/?'
}
with open(CITIES_PATH, 'r') as f:
CITIES = json.load(f)
with open(FIELDS_PATH, 'r') as f:
FIELDS = json.load(f)
print("Starting final scraper...")
scrape_args = [(city_code, city_name, FIELDS, headers, num_fields) for city_code, city_name in CITIES.items()]
if num_cities != -1:
scrape_args = scrape_args[:num_cities]
with multiprocessing.Pool(processes=processes) as pool:
city_results = pool.starmap(scrape_city, scrape_args)
for res in city_results:
scraped_data.update(res)
print(f"Scraping complete. Saving data to {OUTPUT_FILENAME}...")
with open(OUTPUT_FILENAME, 'w', encoding='utf-8') as f:
json.dump(scraped_data, f, ensure_ascii=False, indent=4)
print("Done.")
if __name__ == "__main__":
args = parser.parse_args()
main(args.num_cities, args.num_fields, args.processes)