Last active
December 20, 2020 17:14
-
-
Save d33tah/8225c49d999d71a1a526dbacea5d1e59 to your computer and use it in GitHub Desktop.
isitopen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: '3.8' | |
services: | |
isitopen: | |
build: . | |
restart: unless-stopped |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM python:3.8 | |
ADD ./requirements.txt . | |
RUN python3 -m pip install -r requirements.txt | |
ADD ./main.py . | |
ENTRYPOINT ["./main.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3.8 | |
import logging | |
import time | |
import requests | |
import irc.client | |
def notify(msg): | |
client = irc.client.Reactor() | |
server = client.server() | |
server.connect("irc.freenode.net", 6667, "hsldzat") | |
def on_connect(connection, event): | |
connection.join("#hakierspejs") | |
def on_join(connection, event): | |
connection.privmsg("#hakierspejs", msg) | |
connection.quit() | |
raise RuntimeError() | |
client.add_global_handler("welcome", on_connect) | |
client.add_global_handler("join", on_join) | |
try: | |
client.process_forever() | |
except RuntimeError: | |
pass | |
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 = isitopen() | |
while True: | |
time.sleep(600) | |
nowisopen = isitopen() | |
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[tool.black] | |
line-length = 79 | |
target-version = ['py37'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
requests | |
irc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment