from bs4 import BeautifulSoup
import re


def parse_course_detail(html):
    """
    Use beautifulsoup to get detailed information of one course, then put them
    in a dictionary.
    """
    soup = BeautifulSoup(html, 'lxml')
    table = soup.find_all('span', attrs = {'id': re.compile("^u2(45|54|63|72|81|90)_line\d.?$")})
    clean_pattern = re.compile(' \n|\n|\r')
    
    num_of_courses = len(table) // 6

    try:
        result = {
                "lecNum": ['-'.join(re.sub(clean_pattern, '', table[6 * i].getText()).upper().split()) for i in range(num_of_courses)],
                "size": [int(re.sub(clean_pattern, '', table[4 + 6 * i].getText())) for i in range(num_of_courses)],
                "currentEnrollment": [int(re.sub(clean_pattern, '', table[5 + 6 * i].getText())) for i in range(num_of_courses)]
                }
    except:
        result = {
                "lecNum": ['-'.join(re.sub(clean_pattern, '', table[6 * i].getText()).upper().split()) for i in range(num_of_courses)],
                "size": [int(re.sub(clean_pattern, '', table[4 + 6 * i].getText())) for i in range(num_of_courses)],
                "currentEnrollment": [0 for i in range(num_of_courses)]
                }
    return result