-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownloadTools.py
More file actions
106 lines (88 loc) · 3.83 KB
/
Copy pathDownloadTools.py
File metadata and controls
106 lines (88 loc) · 3.83 KB
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
# -*- coding: utf-8 -*-
from requests import request
from requests import Session
import EEPROM
from Settings import *
from time import sleep
import os
from bs4 import BeautifulSoup
def get_url(i):
return timetable_url + str(i) + ".html"
def normalise_html(html):
html = html.replace("</br>", "<br>")
html = html.replace(".", "")
return html
def get_html(id, ses):
global session_id
for i in range(100):
try:
sleep(0.2)
# на случай, если появится желание использовать session_id
res = ses.get(get_url(id), cookies={"AMS_LAST_LOGIN": username, "AMS_SESSION_ID": session_id})
if res.status_code != 200:
sleep(2)
print(f"код{res.status_code} на позиции {id} всего{i + 1}")
continue
return res
except Exception:
sleep(2)
print(f"остановка на позиции {id} всего{i + 1}")
return None
def download(password, login=username):
my_session = authentication(login, password)
check_teachers_count(my_session)
teachers_count = EEPROM.read_data("teachers_count")
for i in range(1, teachers_count):
data = get_html(i, my_session)
if data is not None:
data = normalise_html(data.content.decode('cp1251'))
if os.path.exists(f"{html_save_path}{str(i)}.html"):
with open(f"{html_save_path}{str(i)}.html", "r") as last:
last_html = last.read()
last_html = last_html[last_html.find("<BODY>"):last_html.find("</BODY>")]
new_html = data[data.find("<BODY>"):data.find("</BODY>")]
new_html = new_html.replace("\r", "\n")
if last_html != new_html:
with open(f"{html_save_path}{str(i)}.html", "w") as f:
f.write(data)
print(f"Страница {str(i)} была изменена!")
else:
print(f"Страница {str(i)}")
else:
with open(f"{html_save_path}{str(i)}.html", "w+") as f:
f.write(data)
print(f"Страница {str(i)} была создана!")
def authentication(login, password):
global session_id
ses = Session()
response = ses.post(authentication_url, data={"login": login, "password": password})
if response.status_code == 200:
ses_id = response.history[0].cookies.get("AMS_SESSION_ID")
if ses_id is not None:
session_id = ses_id
return ses
return Session()
def check_teachers_count(ses):
global session_id
teachers_count = EEPROM.read_data("teachers_count")
for i in range(100):
try:
sleep(0.2)
res = ses.get(teachers_cont_url, cookies={"AMS_LAST_LOGIN": username, "AMS_SESSION_ID": session_id})
if res.status_code != 200:
sleep(2)
print(f"код{res.status_code} при получении всписка учителей")
continue
soup = BeautifulSoup(normalise_html(res.content.decode('cp1251')), 'lxml')
count = len(soup.body.table.find_all("tr")) - 1
if count != teachers_count:
print(f"колличесто преподавателей изменилось: раньше - {teachers_count}, теперь их {count}")
if teachers_count != count:
EEPROM.write_data("teachers_count", count)
return
except Exception:
sleep(2)
print(f"остановка на получение учителей")
return
if __name__ == '__main__':
download("") # пароль писсать сюда