-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
118 lines (91 loc) · 2.88 KB
/
main.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
import requests
import hashlib
from bs4 import BeautifulSoup
import os
from dotenv import load_dotenv
dotenv_path = os.path.join(os.path.dirname(__file__), '.env')
if os.path.exists(dotenv_path):
load_dotenv(dotenv_path)
BASE_URL = os.environ.get("BASE_URL")
BUF_SIZE = int(os.environ.get("BUF_SIZE"))
url = os.environ.get("url")
token = os.environ.get("token")
ids = os.environ.get("ids").split(',')
filename = os.environ.get("filename")
group = os.environ.get("group")
filename = dotenv_path = os.path.join(os.path.dirname(__file__), filename)
def checkfile():
isExists = os.path.exists(filename)
print(f"[+] Файл {filename} существует: {isExists}")
return isExists
def hashfile():
sha256 = hashlib.sha256()
with open(filename, 'rb') as f:
while True:
data = f.read(BUF_SIZE)
if not data:
break
sha256.update(data)
sha256hex = sha256.hexdigest()
print(f"[+] SHA256 = {sha256hex}")
return sha256hex
def getPage(url):
r = requests.get(url)
if r.status_code == 200:
src = r.text
else:
print(f"Ошибка сети {r.status_code}")
return(src)
def parsePage(page):
text = ''
url = ''
soup = BeautifulSoup(page, "lxml")
page_all_a=soup.find_all("a")
for i in page_all_a:
url=i.get("href")
text=i.text
if text == group:
print(f"[+] {text} {url}")
return url
def downloadFile(url):
url = BASE_URL + url
response = requests.get(url)
print(f"[+] Найден файл {url}")
with open(filename, mode="wb") as file:
file.write(response.content)
print(f"[+] Загружен файл {filename}")
def sendMessage(chatid,message):
data = {
'chat_id': chatid,
'text': message
}
try:
response = requests.post(f'https://api.telegram.org/bot{token}/sendMessage', json=data)
except Exception as e:
print(e)
def sendFile(chatid):
print(f"[+] Отправляю файл {filename}")
data = {
'chat_id': (None, chatid),
'document': open(filename, 'rb')
}
try:
response = requests.post(f'https://api.telegram.org/bot{token}/sendDocument', files=data)
print(f"[+] Файл {filename} отправлен {chatid}")
except Exception as e:
print(e)
def sendFileToEverybody():
for chatid in ids:
sendMessage(chatid,'Новое рассписание!')
sendFile(chatid)
if __name__ == "__main__":
if checkfile():
old=hashfile()
downloadFile(parsePage(getPage(url)))
new=hashfile()
if old != new:
print("[+] Новое рассписание")
sendFileToEverybody()
else:
downloadFile(parsePage(getPage(url)))
sendFileToEverybody()