-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·69 lines (53 loc) · 1.66 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
#!/usr/bin/env python3.8
import logging
import time
import asyncio
import requests
import nio
async def sendmsg(msg) -> None:
client = nio.AsyncClient("https://matrix.org", "@d33tah-bot1:matrix.org")
await client.login(open('haslo.txt').read().strip())
result = await client.room_send(
room_id=open('room_id.txt').read().strip(),
message_type="m.room.message",
content={
"msgtype": "m.text",
"body": msg,
}
)
logging.info('sent notification(msg=%r) => %s', msg, result)
def notify(msg):
asyncio.get_event_loop().run_until_complete(sendmsg(msg))
def isitopen():
return bool(
len(
requests.get(
"https://at.hs-ldz.pl/api/v1/users?online=true"
).json()
)
)
def is_status_stable(nowisopen, num_checks):
for i in range(num_checks):
if isitopen() != nowisopen:
return False
time.sleep(60)
return True
def main():
isopen = False
while True:
time.sleep(60)
nowisopen = isitopen()
logging.debug('nowisopen=%s, isopen=%s', nowisopen, isopen)
if nowisopen and not isopen:
if is_status_stable(nowisopen, num_checks=1):
notify("Spejs jest otwarty! Więcej info: https://at.hs-ldz.pl")
isopen = nowisopen
elif isopen and not nowisopen:
if is_status_stable(nowisopen, num_checks=15):
notify(
"Spejs jest zamknięty! Więcej info: https://at.hs-ldz.pl"
)
isopen = nowisopen
if __name__ == "__main__":
logging.basicConfig(level="DEBUG")
main()