-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathns.py
454 lines (361 loc) · 19.7 KB
/
ns.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
from netschoolapi import NetSchoolAPI
import datetime
import re
import html2markdown
from settings import lessons_and_their_smiles
async def get_student(url, login, password, school, studentId):
api = NetSchoolAPI(url)
student = await api.login(login,password,school, studentId)
await api.logout()
return student
async def get_countries(url):
api = NetSchoolAPI(url)
result = await api.countries()
await api.close()
return result
async def get_provinces(url, countryId):
api = NetSchoolAPI(url)
result = await api.provinces(countryId)
await api.close()
return result
async def get_cities(url, countryId, provincesId):
api = NetSchoolAPI(url)
result = await api.cities(countryId, provincesId)
await api.close()
return result
async def get_school(url, countryId = None, provincesId = None, cityId = None):
api = NetSchoolAPI(url)
result = await api.schools(countryId, provincesId, cityId)
await api.close()
return result
# Вход в сго
async def login(login, password, school, link, studentId = None):
api = NetSchoolAPI(link)
await api.login(
login,
password,
school,
studentId)
await api.logout()
# Получить текущую неделю
def get_week():
today = datetime.date.today()
monday = today - datetime.timedelta(days=today.weekday())
saturday = monday + datetime.timedelta(days=5)
week = [monday, saturday]
return week
# Получить следующую неделю
def get_next_week():
today = datetime.date.today()
monday = today - datetime.timedelta(days=today.weekday())
next_monday = monday + datetime.timedelta(days=7)
next_saturday = next_monday + datetime.timedelta(days=5)
week = [next_monday, next_saturday]
return week
# Получить предыдущую неделю
def get_back_week():
today = datetime.date.today()
monday = today - datetime.timedelta(days=today.weekday())
last_monday = monday - datetime.timedelta(days=7)
last_saturday = last_monday + datetime.timedelta(days=5)
week = [last_monday, last_saturday]
return week
# Получить дневник
async def get_diary(login, password, period, school, link, studentId):
api = NetSchoolAPI(link)
await api.login(
login,
password,
school,
studentId)
diary = await api.diary(period[0], period[1])
await api.logout()
return diary
# Получить объявления
async def get_announcements(login, password, amount, school, link, studentId):
api = NetSchoolAPI(link) # Логинимся в СГО
await api.login(
login,
password,
school,
studentId)
announcements = await api.announcements() # Получаем объявления
await api.logout() # Выходим из СГО
announcements = announcements[:int(amount)] # Обрезаем только нужные объявления
# Если есть объявления:
if announcements:
# Приводим объявления в нужный вид
announcement = ''
result = []
for i in announcements:
announcement = f"Дата: {i.post_date.date()}\n{i.name}:\n{i.content}\n"
announcement = re.sub(r'\<[^>]*\>', '', announcement)
result.append(announcement)
return result
# Если нет объявлений:
else:
return ['❌Нет объявлений!']
async def get_marks(login, password, school, url, studentId, subject = None, onlySubjects: bool = None):
api = NetSchoolAPI(url) # Логинимся в СГО
await api.login(login,password,school,studentId)
period = await api.get_period() # Получаем текущий триместр/четверть
period = period['filterSources'][2]['defaultValue'].split(' - ')
start = datetime.datetime.strptime(period[0], '%Y-%m-%dT%H:%M:%S.0000000')
end = datetime.datetime.strptime(period[1], '%Y-%m-%dT%H:%M:%S.0000000')
diary = await api.diary(start=start, end=end) # Получаем дневник
await api.logout() # Выходим из СГО
# Перебираем оценки
marks = {}
for days in diary['weekDays']:
for lesson in days['lessons']:
if lesson['subjectName'] not in marks.keys():
marks[lesson['subjectName']] = []
if 'assignments' in lesson.keys():
for assignment in lesson['assignments']:
if 'mark' in assignment.keys() and 'mark' in assignment['mark']:
if assignment['mark']['mark'] != None:
marks[lesson['subjectName']].append(assignment['mark']['mark'])
if onlySubjects:
return list(marks.keys())
if subject == None:
result = ''
for lesson in marks.keys():
if marks[lesson]:
marks[lesson] = [mark for mark in marks[lesson] if mark]
general_sum = round(sum(marks[lesson]) / len(marks[lesson]), 2)
marks[lesson] = ' '.join(str(e) for e in marks[lesson])
result += f"\n{lesson}: {marks[lesson]} | {general_sum}"
if not result:
result = '❌Нет оценок'
return result
elif subject == 'all':
return marks
else:
return marks[subject]
async def getMarkNotify(login, password, school, url, oldmarks):
api = NetSchoolAPI(url) # Логинимся в СГО
await api.login(login, password, school)
students = await api.getStudents() # Получаем детей, привязанных к аккаунту СГО
await api.logout() # Выходим из СГО
marks = []
for student in students: # Перебираем детей
studentId = student['studentId']
studentNick = student['nickName']
api = NetSchoolAPI(url) # Логинимся в СГО
await api.login(login, password, school, studentId)
period = await api.get_period() # Получаем текущий триместр/четверть
period = period['filterSources'][2]['defaultValue'].split(' - ')
start = datetime.datetime.strptime(period[0], '%Y-%m-%dT%H:%M:%S.0000000')
end = datetime.datetime.strptime(period[1], '%Y-%m-%dT%H:%M:%S.0000000')
diary = await api.diary(start=start, end=end) # Получаем весь дневник за период/триместр
await api.logout() # Выходим из СГО
# Перебираем все оценки
for days in diary['weekDays']:
for lesson in days['lessons']:
if 'assignments' in lesson.keys():
for assignment in lesson['assignments']:
if 'mark' in assignment.keys() and 'mark' in assignment['mark']:
if assignment['mark']['mark']:
date = datetime.datetime.strptime(assignment['dueDate'], '%Y-%m-%dT%H:%M:%S')
all_marks = await get_marks(login, password, school, url, studentId, lesson['subjectName'])
average_mark = float(round(sum(all_marks) / len(all_marks), 2)) # Считаем ср. балл
result = html2markdown.convert(f"❗У ученика {studentNick} новая оценка по предмету {lesson['subjectName']}: {assignment['mark']['mark']} за {assignment['assignmentName']}. Дата: {date.day}.{date.month}.{date.year} \n📊Текущий ср. балл: {average_mark}")
clean = re.compile(r'([a-z0-9]+|#[0-9]{1,6}|#x[0-9a-f]{1,6});')
result = re.sub(clean, '', result)
marks.append(result)
# Смотрим какие оценки новые
difference = []
for item in marks:
try:
if item not in eval(oldmarks):
difference.append(item)
except TypeError:
if item not in oldmarks:
difference.append(item)
return marks, difference
async def getAnnouncementsNotify(login, password, school, url, studentId, old_announcements):
api = NetSchoolAPI(url) # Логинимся в СГО
await api.login(login, password, school, studentId)
announcements = await api.announcements() # Получаем все объявления
# Если есть объявления:
if announcements:
# Приводим объявления в нужный вид
needed_announcements = {}
for i in announcements:
announcementId = i['id']
needed_announcements[announcementId] = {}
date = datetime.datetime.strptime(i['postDate'].split(".")[0], '%Y-%m-%dT%H:%M:%S')
date = f'{date.hour}:{date.minute} {date.day}.{date.month}.{date.year}'
announcement_text = f"❗Новое объявление \n📅Дата: {date} \n👩💼Автор: {i['author']['fio']} \n🔎Тема: {i['name']} \n💬Текст: {i['description']}"
announcement_text = re.sub(r'\<[^>]*\>', '', announcement_text)
needed_announcements[announcementId]['text'] = announcement_text
needed_announcements[announcementId]['attachments'] = {}
for attachment in i['attachments']:
needed_announcements[announcementId]['attachments'][attachment['id']] = {}
needed_announcements[announcementId]['attachments'][attachment['id']]['file_source'] = await api.download_attachment_as_bytes(attachment)
needed_announcements[announcementId]['attachments'][attachment['id']]['title'] = attachment['name']
await api.logout() # Выходим из СГО
# Смотрим какие объявления новые
difference = []
try:
for item in needed_announcements:
if item not in eval(old_announcements):
difference.append(needed_announcements[item])
except TypeError:
for item in needed_announcements:
if item not in old_announcements:
difference.append(needed_announcements[item])
return needed_announcements, difference
async def correction_mark(login, password, school, url, studentId, subject, mark):
all_marks = await get_marks(login, password, school, url, studentId, subject) # Получаем текущие оценки по этому предмету
if len(all_marks) != 0: # Если есть оценки
average_mark = float(round(sum(all_marks) / len(all_marks), 2)) # Считаем ср. балл
else:
average_mark = 0.0 # Ср. балл = 0
lower_threshold = float(mark) - 0.4 # Считаем нижний порог
len_5 = 0 # Кол-во пятерок
len_4 = 0 # Кол-во четверок
len_3 = 0 # Кол-во троек
if mark == 5: # Если нужна пятерка
corrective_marks = [5] # Исправлять можно только пятерками
elif mark == 4: # Если нужна четверка
corrective_marks = [5,4] # Нужно исправлять пятерками и четверками
elif mark == 3: # Если нужна тройка
corrective_marks = [5,4,3] # исправлять можно пятерками, четверками и тройками
if average_mark >= lower_threshold: # если оценка уже нужная или выше
return '🤔У вас и так нужная оценка'
for i in corrective_marks: # Перебираем оценки, которыми можно исправлять
all_marks = await get_marks(login, password, school, url, studentId, subject) # Получаем все текущие оценки
# Считаем ср. балл
if len(all_marks) != 0:
average_mark = float(round(sum(all_marks) / len(all_marks), 2))
else:
average_mark = 0.0
while average_mark <= lower_threshold: # Пока ср. балл ниже нужной оценки
all_marks.append(i) # добавляем ко всем оценкам новую
average_mark = float(round(sum(all_marks) / len(all_marks), 2)) # Считаем ср. балл
# Добавляем к кол-во нужных оценок еще одну
if i == 5:
len_5 += 1
elif i == 4:
len_4 += 1
elif i == 3:
len_3 += 1
return f'Для исправления оценки по {subject} нужны оценки: \n5️⃣: {len_5} \nили\n4️⃣: {len_4} \nили\n3️⃣: {len_3}'
async def getReportTotal(login, password, school, url, studentId):
api = NetSchoolAPI(url) # Логинимся в СГО
await api.login(login, password, school, studentId)
reportTotal = await api.reportTotal() # Получаем отчет
await api.logout() # Выходим из СГО
result = {}
# Приводим в нужный вид
for period in reportTotal.keys():
if period == 'year': result[period] = f'Оценки за год:'
else: result[period] = f'Оценки за {period} триместр/четверть:'
for i in reportTotal[period].keys():
if i in lessons_and_their_smiles:
result[period] += f'\n{lessons_and_their_smiles[i]}{i}: {reportTotal[period][i]}'
else:
result[period] += f'\n📖{i}: {reportTotal[period][i]}'
# Предупреждение
result['Warning'] = '⚠️Внимание⚠️ \nЕсли у вас триместры, то оценки за год стоят под названием "Оценки за 4 триместр/четверть"'
return result
async def getReportAverageMark(login, password, school, url, studentId):
api = NetSchoolAPI(url) # Логинимся в СГО
await api.login(login, password, school, studentId)
reportAverageMark = await api.reportAverageMark() # Получаем отчет
await api.logout() # Выходим из СГО
# Приводим в нужный вид
result = ['Вот твой средний балл на текущий триместр/четверть:', 'Вот средний балл твоего класса на текущий триместр/четверть:']
for i in reportAverageMark['average'].keys():
if i in lessons_and_their_smiles:
result[0] += f"\n{lessons_and_their_smiles[i]}{i}: {reportAverageMark['average'][i]}"
else:
result[0] += f"\n📖{i}: {reportAverageMark['average'][i]}"
for i in reportAverageMark['AverageInClass'].keys():
if i in lessons_and_their_smiles:
result[1] += f"\n{lessons_and_their_smiles[i]}{i}: {reportAverageMark['AverageInClass'][i]}"
else:
result[1] += f"\n📖{i}: {reportAverageMark['AverageInClass'][i]}"
return result
async def getReportAverageMarkDyn(login, password, school, url, studentId):
api = NetSchoolAPI(url) # Логинимся в СГО
await api.login(login, password, school, studentId)
reportAverageMarkDyn = await api.reportAverageMarkDyn() # Получаем отчет
await api.logout() # Выходим из СГО
# Приводим в нужный вид
result = []
for period in reportAverageMarkDyn['average'].keys():
result.append(f'🙍♂Ср. балл ученика за {period}: {reportAverageMarkDyn["average"][period]}')
for period in reportAverageMarkDyn['AverageInClass'].keys():
result.append(f'👫Ср. балл класса за {period}: {reportAverageMarkDyn["AverageInClass"][period]}')
return result
async def getParentReport(login, password, school, url, studentId, termId):
api = NetSchoolAPI(url) # Логинимся в СГО
await api.login(login, password, school, studentId)
parentReport = await api.parentReport(termId) # Получаем отчет
await api.logout() # Выходим из СГО
result = []
# Приводим в нужный вид
for subject in parentReport['subjects'].keys():
if subject in lessons_and_their_smiles:
result.append(f"{lessons_and_their_smiles[subject]}{subject}:")
else:
result.append(f"👨🎓{subject}:")
for mark in parentReport['subjects'][subject].keys():
if mark != 'average' and mark != 'term':
result[-1] += f"\nОценок '{mark}': {parentReport['subjects'][subject][mark]}"
result[-1] += f"\nСредний балл: {parentReport['subjects'][subject]['average']}"
result[-1] += f"\nИтог: {parentReport['subjects'][subject]['term']}"
result.append('📊Итого по всем предметам:')
for mark in parentReport['total'].keys():
if mark != 'average' and mark != 'average_term':
result[-1] += f"\nОценок '{mark}': {parentReport['total'][mark]}"
result[-1] += f"\nСредний балл: {parentReport['total']['average']}"
result[-1] += f"\nИтог: {parentReport['total']['average_term']}"
return result
async def getReportGrades(login, password, school, url, studentId, subjectId):
api = NetSchoolAPI(url) # Логинимся в СГО
await api.login(login, password, school, studentId)
reportGrades = await api.reportStudentAttendanceGrades(subjectId) # Получаем отчет
await api.logout() # Выходим из СГО
# Приводим в нужный вид
result = []
for i in reportGrades.keys():
result.append(f'📅Месяц: {i} \n🙍♂Ученик: {reportGrades[i]["student"]}% \n👫Среднее по классу: {reportGrades[i]["class"]}% \n👯♂Среднее по параллели: {reportGrades[i]["parallel"]}%')
return result
async def getSubjectsId(login, password, school, url, studentId):
api = NetSchoolAPI(url) # Логинимся в СГО
await api.login(login, password, school, studentId)
subjects = await api.getSubjectId() # Получаем ID предмета
await api.logout() # Выходим из СГО
return subjects
async def getTerms(login, password, school, url, studentId):
api = NetSchoolAPI(url) # Логинимся в СГО
await api.login(login, password, school, studentId)
terms = await api.getTerms() # Получаем все триместры/четверти
await api.logout() # Выходим из СГО
return terms
async def getStudents(login, password, school, url, studentId):
api = NetSchoolAPI(url) # Логинимся в СГО
await api.login(login, password, school, studentId)
students = await api.getStudents()
await api.logout() # Выходим из СГО
return students
async def getCurrentStudentId(login, password, school, url, studentId=None):
api = NetSchoolAPI(url) # Логинимся в СГО
await api.login(login, password, school, studentId)
studentId = await api.getCurrentStudentId() # Получаем ID выбранного ребенка
await api.logout() # Выходим из СГО
return studentId
async def getSettings(login, password, school, url, studentId, clas):
api = NetSchoolAPI(url) # Логинимся в СГО
await api.login(login, password, school, studentId)
settings = await api.userInfo() # Получаем личную инфу из СГО
await api.logout() # Выходим из СГО
result = '🔐Твои личные данные из СГО:\n\n'
result += f'Школа: {school}\n'
result += f'Класс: {clas}\n'
for key in settings.keys():
result += f'{key}: {settings[key]}\n'
return result