예제 #1
0
    def process_ccc_or_credit_request(lookup_type, lookup_value):
        """
		Fetches course data table in HTML, extracts sections, and generates list of courses.

		:param lookup_type: Type of data being requested (CCC requirement or credit level)
		:param lookup_value: Value of data requested (W1, EGSS, half credit, etc.)
		:return: List of parsed course data
		"""

        # return cached data, if it exists
        cache_data = course_data_get(lookup_value)

        if cache_data is not None:
            return cache_data["set_time"], cache_data["data"]

        # fetch the HTML
        lookup_type_enum = LookupType[lookup_type.upper()]
        html = fetch_course_html(lookup_value, lookup_type_enum)

        # extract section data from html
        sections = extract_sections(html)

        # ignore elements that don't contain course data
        filtered_sections = filter(lambda x: len(x) > 5, sections)

        # generate a list of dictionaries to organize data for each section
        all_courses = map(CreditOrCCC.handle_ccc_or_credit_section,
                          filtered_sections)

        # store in cache and return data
        cache_time = course_data_set(lookup_value, all_courses)

        return cache_time, all_courses
예제 #2
0
def fetch_section_details(crn, department):
    """
	Fetches description and other data for an individual section.

	:param crn: CRN of the section
	:param department: Department the section belongs to
	:return: Section details
	"""

    # return cached data, if it exists
    key = crn + "details"
    details = course_data_get(key)

    if details is not None:
        return details["set_time"], details["data"]

    # Create and execute server request
    payload = {
        'formopt': 'VIEWSECT',
        'term': TERM,
        'updsubj': department,
        'crn': crn,
        'viewterm': TERM,
        'viewlookopt': 'DPT',
        'viewparam1': department,
        'viewopenopt': 'ALL',
    }

    r = requests.get(BUCKNELL_SECTION_DETAILS_URL, params=payload)

    # Create object that will allow parsing of HTML
    soup = BeautifulSoup(r.text)

    # Extract section details
    details = str(soup.find_all(class_='datadisplaytable')[0])

    # store details in cache
    cache_time = course_data_set(key, details, timeout=259200)

    return cache_time, details
예제 #3
0
    def process_department_request(department_name):
        """
		Fetches course data table in HTML, extracts sections, and generates list of courses.

		:param department_name: The department being requested (CSCI, ECON, etc)
		:return: List of parsed course data
		"""

        # return cached data, if it exists
        cache_data = course_data_get(department_name)

        if cache_data is not None:
            return cache_data["set_time"], cache_data["data"]

        # fetch the HTML
        html = fetch_course_html(department_name, LookupType.DEPARTMENT)

        # extract sections from HTML
        sections = extract_sections(html)

        # ignore first row of section data if it is a department message
        sections = sections[1:] if len(
            sections[0].find_all("td")) == 1 else sections

        # divide a row of section data into its individual columns
        section_split = map(
            lambda current_section: current_section.find_all("td"), sections)

        # group sections by course
        grouped_sections = Department.group_sections_by_course(section_split)

        # parse courses to group sections with their labs, recitations, etc.
        parsed_courses = map(Course, grouped_sections)

        # create format that can be stored in cache or returned to user
        formatted_results = [course.export() for course in parsed_courses]

        cache_time = course_data_set(department_name, formatted_results)

        return cache_time, formatted_results