-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiary.py
191 lines (149 loc) · 5.7 KB
/
diary.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
import requests
from bs4 import BeautifulSoup
from datetime import datetime
try:
from url_helper import *
from exceptions import *
except ImportError:
from edu_parser.url_helper import *
from edu_parser.exceptions import *
def get_timestamp(date):
base_date = datetime.strptime('01.05.2019', '%d.%m.%Y')
base_timestamp = 1556658000
new_timestamp = base_timestamp + (date - base_date).days * 86400
return new_timestamp
class DiaryGrade:
def __init__(self, data):
self.grade = data.div.string
self.comment = data['title']
def __repr__(self):
return '({}, {})'.format(self.grade, self.comment)
def __str__(self):
return __repr__()
class DiarySubject:
def __init__(self, data):
self.data = {}
self.time_start = data[0].contents[0]
self.data['time_start'] = self.time_start
self.time_end = data[0].contents[-1]
self.data['time_end'] = self.time_end
self.time = '{} - {}'.format(self.time_start, self.time_end)
self.data['time'] = self.time
self.name = data[1].string
self.data['name'] = self.name
if data[2].p is not None:
self.homework = data[2].p.string
else:
self.homework = ''
self.data['homework'] = self.homework
if data[3].i is not None:
self.comment = data[3].i.string
else:
self.comment = ''
self.data['comment'] = self.comment
if data[4].find('table', attrs={'class': 'marks'}) is not None:
cols = data[4].tr.find_all('td')
self.grades = [DiaryGrade(col) for col in cols]
self.data['grades'] = self.grades
else:
self.grades = []
self.data['grades'] = self.grades
def __repr__(self):
return str(self.data)
def get_str(self):
homework = self.homework
if homework is None:
homework = "Ничего не задано"
return '_' + str(self.time) + '_ *' + str(self.name) + '* - `' + homework + '`'
class DiaryDay:
def __init__(self, session, date, proxy):
self.date = datetime.strptime(date, '%d.%m.%Y')
self.date_str = date
self.weekday = self.date.weekday()
response = session.get(diary_day_url + str(get_timestamp(self.date)), proxies=proxy)
if 'не найден' in response.text:
raise LoginError('it appears that you are not logged in')
html = BeautifulSoup(response.text, 'html.parser')
main_table = html.find('table', attrs={'class': 'main'})
rows = main_table.find_all('tr')
self.subjects = []
for row in rows[1:]:
cols = row.find_all('td')
try:
cols[0]['title']
except KeyError:
self.subjects.append(DiarySubject(
[col for col in cols]
))
def __repr__(self):
return 'instance of DiaryDay class with date {}'.format(self.date_str)
def __str__(self):
return str(dict([
('weekday', self.weekday),
('date_str', self.date_str),
('subjects', self.subjects)
]))
return '"weekday": {}, "date_str": "{}", "subjects": {}'.format(self.weekday, self.date_str, str(self.subjects))
class TermSubject:
def __init__(self, data, grades_count):
self.data = {}
self.name = data[0].string
self.data['name'] = self.name
self.grades = []
raw_grades = data[1:grades_count + 1]
for raw_grade in raw_grades:
grade = raw_grade.string
if grade is not None:
self.grades.append(int(grade))
self.data['grades'] = str(self.grades)
try:
self.average_grade = float(data[grades_count + 1].string)
except TypeError:
self.average_grade = 0.0
self.data['average_grade'] = self.average_grade
self.final_grade = data[-1].string
self.data['final_grade'] = self.final_grade
def __repr__(self):
return str(self.data)
def __str__(self):
return __repr__()
def predict(self, new_grades):
return (sum(self.grades) + sum(new_grades)) / (len(self.grades) + len(new_grades))
class DiaryTerm:
def __init__(self, session, term, proxy):
self.term_number = term
response = session.get(term_url + term, proxies=proxy)
if 'не найден' in response.text:
raise LoginError('it appears that you are not logged in')
html = BeautifulSoup(response.text, 'html.parser')
main_table = html.find('table', attrs={'class': 'term-marks'})
rows = main_table.find_all('tr')
self.grades_count = int(rows[0].find_all('td')[1]['colspan'])
self.subjects = {}
for row in rows[1:-1]:
cols = row.find_all('td')
subject = TermSubject(
[col for col in cols], self.grades_count
)
self.subjects[subject.name] = subject
def get_subject(self, input_name):
name = ''
count = 0
for key in self.subjects.keys():
if input_name.lower() in key.lower():
name = key
count += 1
if count > 1:
return None
return self.subjects.get(name)
def __repr__(self):
if self.term_number == '':
return 'instance of DiaryTerm class for the current term'
else:
return 'instance of DiaryTerm class for the {} term'.format(self.term_number)
def __str__(self):
return str(dict([
('term_number', self.term_number),
('grades_count', self.grades_count),
('subjects', self.subjects)
]))