moodleNotifer

login.py
Go to the documentation of this file.
1
6
7 import re
8 from bs4 import BeautifulSoup
9
10 # import pickle
11 # from .letsloginourself import RequestHelper
12 # with open('configdictionary', 'rb') as config_dictionary_file:
13 # config_dictionary = pickle.load(config_dictionary_file)
14 # #print(config_dictionary)
15
16 # moodle_domain = config_dictionary['domain']
17 # moodle_path = config_dictionary['path']
18 # moodle_token = config_dictionary['moodle_token']
19
20 # #print(moodle_domain,moodle_path,moodle_token)
21
22 # request_helper = RequestHelper(moodle_domain, moodle_path, moodle_token, False)
23
24
25 def prune_dict(list_of_data, keys):
26 files = []
27 for val in list_of_data:
28 valid_val = dict((j, val[j]) for j in val if j in keys)
29 files.append(valid_val)
30 return files
31
32 def string_from_html(data):
33 soup = BeautifulSoup(data,'lxml')
34 txt = ""
35 for ptag in soup.find_all('p'):
36 txt += ptag.text
37 return txt
38
39
42 def get_list_of_courses(request_helper):
43 '''
44 input : object of class RequestHelper
45 output : two dictionaries: 1. course_ids with key value pairs as 'SubjectCode' : 'Id used by moodle for it'
46 2. course_names with key value pairs as 'SubjectCode' : 'Name of the Subject'
47 '''
48 result = request_helper.post_REST('core_webservice_get_site_info')
49 if 'userid' not in result:
50 raise RuntimeError('Error could not receive your user ID!')
51 userid = result.get('userid', '')
52 data = {'userid': userid}
53 result = request_helper.post_REST('core_enrol_get_users_courses', data)
54 course_names = {}
55 course_ids = {}
56 for index in result:
57 course_ids[index['shortname'][3:6]] = index['id']
58 course_names[index['shortname'][3:6]] = index['fullname']
59 return course_ids,course_names
60
61
62
65 def get_assignments_info(request_helper,course_id,all = True):
66 '''
67 Returns information of all assignments , if the second aruguement is false it returns info of only the latest assignment
68 input : object of class RequestHelper
69 output : list of all assignments with following attributes {'id','name','course','duedate','intro','introattachments'}
70 '''
71 extra_data = {}
72 courseids = {"0":course_id}
73 extra_data.update({'courseids': courseids})
74 result = request_helper.post_REST('mod_assign_get_assignments', extra_data)
75 assignments = result['courses'][0]['assignments']
76 l = []
77
78 for assign in assignments:
79 data = dict((k, assign[k]) for k in assign if k in ['id','name','course','duedate','timemodified','intro','introattachments'])
80 soup = BeautifulSoup(data['intro'],'lxml')
81 #processing intro
82 if data['intro']:
83 data['intro'] = string_from_html(data['intro'])
84 #processing introattachments
85 if data['introattachments']:
86 data['introattachments'] = prune_dict(data['introattachments'],['filename','fileurl'])
87 l.append(data)
88
89 if not l:
90 #print("Empty assignments for",extra_data)
91 return l
92 if(all == True):
93 return l
94 else:
95 return l[-1]
96
97
100 def get_quizzes_info(request_helper,course_id,all = True):
101 '''
102 returns information about all quizes if all is true else about the latest quiz
103 input : object of class RequestHelper, couse_id, all flag
104 output : list of all quizzes with following attributes {''id','name','timeopen','timeclose','intro','grade'}
105 '''
106 extra_data = {}
107 courseids = {"0":course_id}
108 extra_data.update({'courseids': courseids})
109 result = request_helper.post_REST('mod_quiz_get_quizzes_by_courses', extra_data)
110 quizzes = result['quizzes']
111 l = []
112 if not quizzes:
113 return l
114 for quiz in quizzes:
115 data = dict((k, quiz[k]) for k in quiz if k in ['id','name','timeopen','timeclose','intro','grade'])
116 #intro processing
117 if data['intro']:
118 data['intro'] = string_from_html(data['intro'])
119 l.append(data)
120 if(all == True):
121 return l
122 else:
123 return l[-1]
124
125
126
129 def get_course_contents(request_helper,course_id):
130 '''
131 returns contents of course week wise as uploaded
132 input : object of class RequestHelper
133 output: course contents for all weeks
134 '''
135 data = {'courseid': course_id}
136 result = request_helper.post_REST('core_course_get_contents', data)
137 l = []
138 for week in result:
139 data = dict((k, week[k]) for k in week if k in ['id','name','modules'])
140 if data['modules']:
141 files = []
142 for mod in data['modules']:
143 fl = dict((j, mod[j]) for j in mod if j in ['url','name','contents'])
144 if 'contents' in fl.keys():
145 fl['contents'] = prune_dict(fl['contents'],['filename','fileurl'])
146 files.append(fl)
147 data['modules'] = files
148 l.append(data)
149 return l
150
151
152
155 def get_discussion_forum_id(request_helper,course_id):
156 '''
157 input : object of class RequestHelper and course id
158 output: dictionary containing id and name of the Announcements forum
159 '''
160 extra_data = {}
161 courseids = {"0":course_id}
162 extra_data.update({'courseids': courseids})
163 result = request_helper.post_REST('mod_forum_get_forums_by_courses', extra_data)
164 forums = {}
165 for forum in result:
166 if(forum['name'] in ['Announcements']):
167 forums = dict((k, forum[k]) for k in forum if k in ['id','name'])
168 return forums
169
170
171
174 def get_discussion_forum_discussions(request_helper,forum_id):
175 '''
176 input : object of class RequestHelper, forum_id
177 output: returns most recent 5 discussions of the discussion forum
178 '''
179 data = {
180 'forumid': forum_id,
181 }
182 result = request_helper.post_REST('mod_forum_get_forum_discussions', data)
183 discussions = result['discussions']
184 if(len(discussions) > 5):
185 discussions = discussions[0:5]
186 discussions = prune_dict(discussions,['name','created','message'])
187 for i in discussions:
188 i['message'] = string_from_html(i['message'])
189 return discussions
190
191
192 def get_discussion_forum_posts():
193 '''
194 input : object of class RequestHelper
195 '''
196 data = {
197 'discussionid': 16545,
198 'sortby': 'modified',
199 'sortdirection': 'ASC',
200 }
201 posts_result = request_helper.post_REST('mod_forum_get_forum_discussion_posts', data)
202 print(posts_result)
203
204
205
206
207
208 #function Call to get list of courses taken by a student
209 '''
210 course_ids,course_names = get_list_of_courses(request_helper)
211 print(course_ids,course_names)
212 '''
213
214 #function Call to get assignments corresponding to a subject
215 '''
216 course_id = 219 #eg for 699
217 assignments = get_assignments_info(request_helper,course_id,False)
218 print(assignments)
219 '''
220
221 #function Call to get quizzes corresponding to a subject
222 '''
223 course_id = 230 #eg for 771 course
224 quizzes = get_quizzes_info(request_helper,course_id,True)
225 print(quizzes)
226 '''
227
228 #function Call to get quizzes corresponding to a subject
229 '''
230 course_id = 230 #eg for 771 course
231 quizzes = get_quizzes_info(request_helper,course_id,True)
232 print(quizzes)
233 '''
234
235 #function Call to get announcement forum id
236 '''
237 ann_forum = get_discussion_forum_id(request_helper,230)
238 print(ann_forum)
239 '''
240
241 #function Call to get course contents
242 '''
243 cc = get_course_contents(request_helper,230)
244 print(cc[-1])
245 '''
246
247 #function Call to get 5 most recent posts from a discussion forum
248 '''
249 discs = get_discussion_forum_discussions(request_helper,229)
250 # for d in discs:
251 # print(d)
252 print(type(discs),discs)
253 '''