diff --git a/scripts/test/api.py b/scripts/test/api.py index ca2d41e..8208cad 100644 --- a/scripts/test/api.py +++ b/scripts/test/api.py @@ -2,6 +2,7 @@ import requests import logging from dotenv import load_dotenv +from helpers import extract_subject_area_and_number, construct_url # Load environment variables load_dotenv() @@ -11,7 +12,15 @@ # Configure logging logging.basicConfig(level=logging.INFO) -def fetch_course_info(full_url): + +# Fetch course information from the Berkeley API, trying both with and without 'C' in the catalog number if needed. +def fetch_course_info(term_id, class_name): + """ + Args: + term_id (int): The term ID. + class_name (str): The class name. + Returns: dict: The extracted course information or an error message. + """ app_id = os.getenv('APP_ID') app_key = os.getenv('APP_KEY') @@ -20,6 +29,12 @@ def fetch_course_info(full_url): "app_key": app_key, } + # Extract the subject area code and number part from the class name + subject_area_code, number = extract_subject_area_and_number(class_name) + + # Construct the full URL for the API request + full_url = construct_url(term_id, subject_area_code, number, BASE_URL) + try: response = requests.get(full_url, headers=headers) response.raise_for_status() # Raise an exception for HTTP errors @@ -34,6 +49,26 @@ def fetch_course_info(full_url): } return extracted_info - except requests.exceptions.RequestException as e: - logging.error(f"Failed to retrieve data. Error: {str(e)}") - return None + except requests.exceptions.RequestException: + # If the initial fetch fails, try adding 'C' to the number + number_with_c = 'C' + number + full_url_with_c = construct_url(term_id, subject_area_code, number_with_c, BASE_URL) + + try: + response_with_c = requests.get(full_url_with_c, headers=headers) + response_with_c.raise_for_status() # Raise an exception for HTTP errors + + if response_with_c.status_code == 200: + data = response_with_c.json()['apiResponse']['response'] + classes = data['classes'] + extracted_info = { + "display_name": classes[0]['displayName'], + "department": classes[0]['course']['subjectArea']['description'], + "enrollment_count": sum(section['aggregateEnrollmentStatus']['enrolledCount'] for section in classes) + } + return extracted_info + + except requests.exceptions.RequestException as e: + logging.error(f"Failed to retrieve data. Error: {str(e)}") + return {"error": f"Failed to retrieve data for the given class name: {class_name}"} + diff --git a/scripts/test/helpers.py b/scripts/test/helpers.py index 2de5917..490529e 100644 --- a/scripts/test/helpers.py +++ b/scripts/test/helpers.py @@ -10,21 +10,6 @@ def has_digits(input_string): return any(char.isdigit() for char in input_string) -# Modify the class name by splitting it into subject area code and number, then add 'C' to the number. -def modify_class_name_with_c(class_name): - """ - Args: class_name (str): The class name. - Returns: tuple: A tuple containing the subject area code and catalog number. - """ - # Extract the subject area code from the class name - subject_area_code = re.search(r'([a-zA-Z]+)', class_name).group(1).upper() - # Extract the number part from the class name - number = ''.join(filter(str.isdigit, class_name)) - # Add 'C' to the number part - number = 'C' + number - - return subject_area_code, number - #Extract the subject area code and catalog number from the class name. def extract_subject_area_and_number(class_name): @@ -38,3 +23,18 @@ def extract_subject_area_and_number(class_name): number = ''.join(filter(str.isdigit, class_name)) return subject_area_code, number + +#Construct URL parameters for the API request. +def construct_url(term_id, subject_area_code, catalog_number, base_url, page_number=1, page_size=100): + """ + Args: + term_id (int): The term ID. + subject_area_code (str): The subject area code. + catalog_number (str): The catalog number. + base_url (str): The base URL for the API. + page_number (int, optional): The page number. Defaults to 1. + page_size (int, optional): The page size. Defaults to 100. + Returns: str: The constructed full URL for the API request. + """ + url_params = f"term-id={term_id}&subject-area-code={subject_area_code}&catalog-number={catalog_number}&page-number={page_number}&page-size={page_size}" + return base_url + url_params diff --git a/scripts/test/main.py b/scripts/test/main.py index 9b4eeab..f8d4efb 100644 --- a/scripts/test/main.py +++ b/scripts/test/main.py @@ -6,8 +6,7 @@ import re import logging from api import fetch_course_info, BASE_URL -from helpers import has_digits -from validation import validate_class_name +#from validation import validate_class_name from dotenv import load_dotenv # Load environment variables @@ -17,39 +16,33 @@ logging.basicConfig(level=logging.INFO) -# Fetch course information from the Berkeley API. -def get_course_information(term_id, class_name, page_number=1, page_size=100): +#Fetch course information from the Berkeley API. +def get_course_information(term_id, class_name): """ + Fetch course information from the Berkeley API. Args: term_id (int): The term ID. class_name (str): The class name. - page_number (int, optional): The page number. Defaults to 1. - page_size (int, optional): The page size. Defaults to 100. Returns: str: JSON string with extracted information or error message. """ - # Validate the class name and extract the subject area code and catalog number + + """#Validate the class name and extract the subject area code and catalog number + Maybe won't need validaiton since we implemented try in api.py validation_result, error = validate_class_name(class_name) if error: - # If there's an error in validation, return the error message as a formatted JSON string + # If there's an error in validation, return the error message as JSON return json.dumps(error, indent=4) - # Unpack the validation result into subject area code and catalog number - subject_area_code, number = validation_result - - # Construct the URL parameters for the API request - url_params = f"term-id={term_id}&subject-area-code={subject_area_code}&catalog-number={number}&page-number={page_number}&page-size={page_size}" - - # Construct the full URL for the API request - full_url = BASE_URL + url_params - + subject_area_code, catalog_number = validation_result + # Fetch the course information from the API + result = fetch_course_info(term_id, f"{subject_area_code}{catalog_number}")""" + + # Fetch the course information from the API - result = fetch_course_info(full_url) - if result: - # If the fetch is successful, return the result as a formatted JSON string - return json.dumps(result, indent=4) + result = fetch_course_info(term_id, class_name) - # If all attempts fail, return an error message as a formatted JSON string - return json.dumps("Failed to retrieve data for the given class name.", indent=4) + # Return the result as a JSON string + return json.dumps(result, indent=4) def main(): diff --git a/scripts/test/validation.py b/scripts/test/validation.py index 22f3718..d7e0cb0 100644 --- a/scripts/test/validation.py +++ b/scripts/test/validation.py @@ -1,5 +1,5 @@ import re -from helpers import has_digits, modify_class_name_with_c, extract_subject_area_and_number +from helpers import has_digits, extract_subject_area_and_number #Validate and extract subject area code and catalog number from class name. def validate_class_name(class_name): @@ -14,11 +14,6 @@ def validate_class_name(class_name): # Check if the class name contains digits if not has_digits(class_name): return None, f"Class name {class_name} is invalid. Please enter a valid class name." - - # Check if the class name starts with 'stat' or 'data' - if class_name.lower().startswith(('stat', 'data')): - # Use the helper function to modify the class name and extract components - subject_area_code, number = modify_class_name_with_c(class_name) else: # Use the helper function to extract the subject area code and number