· 8 years ago · Nov 24, 2017, 11:00 PM
1from bs4 import BeautifulSoup as bs
2import session
3import os
4
5BASE_URL = "https://magshimim.edu20.org"
6
7# the directory where all the courses will be saved
8BASE_DIR = "Magshimim_courses"
9
10# the directory where the answers of the lessons will be saved
11ANSWER_SECTION = "answers"
12
13# the directory where the learning stuff will be saved, such as presentation, example, etc.
14LEARNING_SECTION = "learning"
15
16class lesson(object):
17 def __init__(self, name):
18 # the name of the lesson
19 self.name = name
20
21 def create_lesson(self, base_dir):
22 """creates a directory for the lesson, and directories for answers, and learning stuff
23
24 @@param base_dir - the course name that the lesson belongs to
25 """
26
27 # creates a directory for the lesson if not exists
28 if not os.path.isdir(base_dir + "/" + self.name):
29 os.mkdir(base_dir + "/" + self.name)
30
31 # creates a directory for the answers section if not exists
32 if not os.path.isdir(base_dir + "/" + self.name + "/" + ANSWER_SECTION):
33 os.mkdir(base_dir + "/" + self.name + "/" + ANSWER_SECTION)
34
35 # creates a directory for the learning section if not exists
36 if not os.path.isdir(base_dir + "/" + self.name + "/" + LEARNING_SECTION):
37 os.mkdir(base_dir + "/" + self.name + "/" + LEARNING_SECTION)
38
39
40class course(object):
41 def __init__(self, name, url):
42 # the name of the course
43 self.name = name
44 # a list that contains all the lessons that the course has
45 self.lessons = []
46 # the page containing all the lessons of the course
47 self.url = url
48
49 def get_lessons(self, s):
50 """fills the lessons list
51
52 @@param s - the session between the user and edu
53 """
54 print self.url
55 # gets course page in normal view
56 soup = bs(s.get(self.url).text.encode('utf-8'))
57
58 # gets the dir containing all the lessons
59 div = soup.find('div', class_='studentModuleView new_studentModuleView new_background')
60 # loops over all the courses
61 for table in div.find_all('table', class_='false'):
62 # gets the name of the lesson
63 lesson_name = table.find(lambda tag: tag.name == 'a' and not tag.has_attr('class')).text
64 self.lessons.append(lesson(lesson_name))
65
66 def create_course(self):
67 """creates all the course's lessons inside a directory named after the course name.
68 """
69 # creates a directory for the course if not exists
70 if not os.path.isdir(BASE_DIR + "/" + self.name):
71 os.mkdir(BASE_DIR + "/" + self.name)
72
73 # applies the create lesson function for each lesson the course contains
74 map(lambda lesson: lesson.create_lesson(BASE_DIR + "/" + self.name), self.lessons)
75
76def get_courses(s):
77 """finds all the courses that the user has, and creates objects containing them
78
79 @@return - a list containing all the courses objects
80 """
81 # the page containing the information about all the completed courses
82 soup = bs(s.get('https://magshimim.edu20.org/user/completed/3047789').text.encode('utf-8'))
83 # creates all the courses that the user has on edu
84 courses = [course(a.text, BASE_URL + a['href']) for a in soup.find('table', class_="mobileOptimized").find_all('a') if a.has_attr('href')]
85
86 return courses
87
88
89def main():
90 # craetes the base directory if not exists
91 if not os.path.isdir(BASE_DIR):
92 os.mkdir(BASE_DIR)
93
94 # creates a statefull connection with edu, and logs into my account
95 s = session.connect_to_edu()
96
97 courses = get_courses(s)
98 # fills the courses' lessons
99 map(lambda lesson: lesson.get_lessons(s), courses)
100 # creates the courses' directories, filled all the courses data.
101 map(lambda lesson: lesson.create_course(), courses)
102
103if __name__ == "__main__":
104 main()