from bs4 import BeautifulSoup
import re


def getCoursesDict(file):
    """
    This function is used to scrape the courses' urls from the CMU course website
    :param file: the local saved cmu course file
    :return: a course dictionary which save {course_id:course_url} pairs
    """
    print("Begin getCoursesDict()")
    htmlfile = open(file, 'r', encoding='latin-1')   # open a local html file, read only
    htmlpage = htmlfile.read()
    soup = BeautifulSoup(htmlpage, "html.parser")   # get a soup instance by the html file

    course_dict = {}   # save the courses id and the url
    find_url = re.compile(r'\'(.*?)\'')
    # find all courses in each department
    for department in soup.find_all('table', class_="table table-condensed table-striped"):
        # find each course in the department
        for courses in department.find('tbody').find_all('tr'):
            # find the course id column
            course_col = courses.find('td').find('a')
            # if the course id column has content
            if course_col is not None:
                href = course_col.get('onclick')
                full_url = "https://enr-apps.as.cmu.edu{}".format(find_url.findall(href)[2])
                course_id = course_col.get_text()
                course_dict[course_id] = full_url
    print("End getCoursesDict()")
    return course_dict