Skip to content

Instantly share code, notes, and snippets.

@d33tah
Last active December 20, 2020 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d33tah/8225c49d999d71a1a526dbacea5d1e59 to your computer and use it in GitHub Desktop.
Save d33tah/8225c49d999d71a1a526dbacea5d1e59 to your computer and use it in GitHub Desktop.
isitopen
version: '3.8'
services:
isitopen:
build: .
restart: unless-stopped
FROM python:3.8
ADD ./requirements.txt .
RUN python3 -m pip install -r requirements.txt
ADD ./main.py .
ENTRYPOINT ["./main.py"]
#!/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()
[tool.black]
line-length = 79
target-version = ['py37']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment