-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsing.py
80 lines (76 loc) · 4.27 KB
/
parsing.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
import re
import redis
import requests
import datetime
import exceptions
import services
def parsing_schedule(connection, groupoid, redis_obj: redis.Redis):
num_dayweek_to_string = {
1: 'Понедельник',
2: 'Вторник',
3: 'Среда',
4: 'Четверг',
5: 'Пятница',
6: 'Суббота',
7: 'Воскресенье'
}
redis_obj.sadd('has_schedule', groupoid)
url = f'http://ts.mpei.ru/api/schedule/group/{groupoid}'
today = datetime.datetime.now() + datetime.timedelta(hours=3)
monday_of_this_week = today + datetime.timedelta(days=-today.weekday())
monday_of_next_week = monday_of_this_week + datetime.timedelta(weeks=1)
saturday_of_this_week = monday_of_this_week + datetime.timedelta(days=6)
saturday_of_next_week = monday_of_next_week + datetime.timedelta(days=6)
start_string_this_week = f'{monday_of_this_week.year}.{monday_of_this_week.month // 10}{monday_of_this_week.month % 10}.{monday_of_this_week.day // 10}{monday_of_this_week.day % 10}'
start_string_next_week = f'{monday_of_next_week.year}.{monday_of_next_week.month // 10}{monday_of_next_week.month % 10}.{monday_of_next_week.day // 10}{monday_of_next_week.day % 10}'
finish_string_this_week = f'{saturday_of_this_week.year}.{saturday_of_this_week.month // 10}{saturday_of_this_week.month % 10}.{saturday_of_this_week.day // 10}{saturday_of_this_week.day % 10}'
finish_string_next_week = f'{saturday_of_next_week.year}.{saturday_of_next_week.month // 10}{saturday_of_next_week.month % 10}.{saturday_of_next_week.day // 10}{saturday_of_next_week.day % 10}'
json_obj_this_week = requests.get(url, params={
'start': start_string_this_week,
'finish': finish_string_this_week
}).json()
json_obj_next_week = requests.get(url, params={
'start': start_string_next_week,
'finish': finish_string_next_week
}).json()
with connection as conn:
with conn.cursor() as cursor:
for item in json_obj_this_week:
WeekDay = num_dayweek_to_string[item['dayOfWeek']]
num_object = item['lessonNumberStart']
auditory = item['auditorium']
teacher = item['lecturer'] if 'не определена' not in item['lecturer'].lower() else 'Не определен'
obj = item['discipline']
object_type = item['kindOfWork']
slug = services.generate_slug(redis_obj)
week = int(redis_obj.get('current_week').decode('utf8'))
query = f"""
INSERT INTO schedule(WeekDay, num_object, groupoid, auditory, teacher, object, object_type, slug, week) VALUES {WeekDay, num_object, groupoid, auditory, teacher, obj, object_type, slug, week}
"""
cursor.execute(query)
for item in json_obj_next_week:
WeekDay = num_dayweek_to_string[item['dayOfWeek']]
num_object = item['lessonNumberStart']
auditory = item['auditorium']
teacher = item['lecturer'] if 'не определена' not in item['lecturer'].lower() else 'Не определен'
obj = item['discipline']
object_type = item['kindOfWork']
slug = services.generate_slug(redis_obj)
week = int(redis_obj.get('current_week').decode('utf8')) + 1
query = f"""
INSERT INTO schedule(WeekDay, num_object, groupoid, auditory, teacher, object, object_type, slug, week) VALUES {WeekDay, num_object, groupoid, auditory, teacher, obj, object_type, slug, week}
"""
cursor.execute(query)
def get_groupoid_or_raise_exception(group, redis_obj):
groupoid = redis_obj.get(f'group:{group}')
if groupoid:
return int(groupoid.decode('utf8'))
url = requests.get(f'http://mpei.ru/Education/timetable/Pages/default.aspx?group={group}').url
try:
groupoid = re.findall(r'groupoid=(\d+)', url)[0]
except IndexError:
raise exceptions.MpeiBotException(
f'Похоже группы, которую Вы ввели, не существует 😰')
redis_obj.set(f'groupoid:{groupoid}', group)
redis_obj.set(f'group:{group}', groupoid)
return groupoid