Skip to content
Snippets Groups Projects
Commit 14de8b4a authored by Millian Poquet's avatar Millian Poquet
Browse files

code: enable fetching data without input data file

parent 4b7c38de
No related branches found
No related tags found
No related merge requests found
......@@ -9,30 +9,34 @@ class CourseRequest:
self.df = pd.read_csv(filename, parse_dates=['begin_date', 'end_date'])
self.df['course_request_id'] = self.df.index
def generate_request_body(self):
def generate_request_input(self):
date_range_min = min(self.df['begin_date']).strftime("%Y-%m-%d")
date_range_max = (max(self.df['end_date']) + pd.Timedelta(days=1)).strftime("%Y-%m-%d")
apogee_codes = self.df['module_apogee'].unique()
fields = [
f'start={date_range_min}',
f'end={date_range_max}',
'resType=100',
'calView=agendaWeek',
] + ['federationIds%5B%5D={}'.format(apogee_code) for apogee_code in apogee_codes]
return '&'.join(fields)
return (date_range_min, date_range_max, apogee_codes)
def do_request(self, url='https://edt.univ-tlse3.fr/calendar2/Home/GetCalendarData'):
headers = {"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}
request_body = self.generate_request_body()
response = requests.post(url, request_body, headers=request_headers)
if not response.ok:
logging.error(f'POST HTTP request failed (status code {response.status_code}): {response.reason}')
logging.error(f'Request response text:\n---\n{response.text}\n---')
response.raise_for_status()
return response.text
(date_min, date_max, apogee_codes) = self.generate_request_input()
return do_celcat_calendar_request(date_min, date_max, apogee_codes, url)
def do_celcat_calendar_request(min_date, max_date, module_apogee_codes, url='https://edt.univ-tlse3.fr/calendar2/Home/GetCalendarData'):
headers = {"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}
fields = [
f'start={min_date}',
f'end={max_date}',
'resType=100',
'calView=agendaWeek',
] + ['federationIds%5B%5D={}'.format(apogee_code) for apogee_code in module_apogee_codes]
fields_str = '&'.join(fields)
logging.info(f'Sending a POST request with data={fields_str}')
response = requests.post(url, data=fields_str, headers=headers)
if not response.ok:
logging.error(f'POST HTTP request failed (status code {response.status_code}): {response.reason}')
logging.error(f'Request response text:\n---\n{response.text}\n---')
response.raise_for_status()
return response.text
class CelcatEvents:
def __init__(self, celcat_raw_response):
......@@ -41,7 +45,7 @@ class CelcatEvents:
self.df['end'] = self.df['end'].astype('datetime64[ns]')
self.df = self.df[["start", "end", "allDay", "description", "eventCategory", "modules"]]
self.df['timeslot_id'] = self.df.index
class FilteredCelcatEvents:
def __init__(self, course_request, celcat_events):
self._course_request = course_request
......@@ -143,6 +147,7 @@ def course_df_to_ics(df):
return c
logging.basicConfig(level=logging.INFO)
req = CourseRequest('input-data.csv')
celcat_raw_response = req.do_request()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment