""" Program that grabs the specific lecture URLs from the CS51A course home page """ from urllib.request import urlopen COURSE_PAGE = "http://cs.pomona.edu/classes/cs51a/" def get_lectures_urls(page): """ Get the lecture URLs available from the course web page and return them in a list :param page: (str) the URL to read :return: (list) """ web_page = urlopen(page) urls = [] # all of the urls for lectures, start with "lectures/" search_line = "lectures/" for line in web_page: line = line.decode('ISO-8859-1').strip() if search_line in line: begin_index = line.find(search_line) end_index = line.find('"', begin_index) urls.append(page + line[begin_index:end_index]) return urls def write_list_to_file(some_list, output_file): """ Writes out all of the contents of some_list to the output_file :param some_list: (list) list with data to be written to output_file :param output_file: (str) name of file to write some_list data to :return: """ out = open(output_file, "w") out.write("Lecture notes for CS51A\n\n") for item in some_list: out.write(item + "\n") out.close() def write_lectures(outfile): """ Gets the lecture links from the COURSE_PAGE and writes :param outfile: (str) the output file to write the lecture links to :return: (None) """ # get the lecture urls lecture_urls = get_lectures_urls(COURSE_PAGE) # print them out to the file write_list_to_file(lecture_urls, outfile)