diff --git a/homepage/participo/bdCheck.py b/homepage/participo/bdCheck.py new file mode 100644 index 0000000..517ff82 --- /dev/null +++ b/homepage/participo/bdCheck.py @@ -0,0 +1,73 @@ +import testApi +from datetime import datetime +from MatrixIm import MatrixIm + + +def get_days_since_birthday(birthday: datetime.date, today: datetime.date=datetime.now()) -> int: + # get number of days distance to birthday in this year: + this_year = (datetime(today.year, birthday.month, birthday.day) - today).days + # The birthday is already past return it + if this_year <= 0: + return this_year + # otherwise we have to take the distance to last years birthday + return (datetime(today.year-1, birthday.month, birthday.day) - today).days + +def get_days_until_birthday(birthday: datetime.date, today: datetime.date=datetime.now()) -> int: + # get number of days distance to birthday in this year: + this_year = (datetime(today.year, birthday.month, birthday.day) - today).days + # The birthday is already past return it + if this_year >= 0: + return this_year + # otherwise we have to take the distance to next years birthday + return (datetime(today.year+1, birthday.month, birthday.day) - today).days + + + +with open("testApi.config.yaml", "r") as yaml_file: + from yaml import safe_load + config = safe_load(yaml_file) + +trainees = [{ + "name": {"family": t['name'], "given": t['vorname']} + , "date_of_birth": datetime.strptime(t['gebDatum'], "%Y-%m-%d") +} for t in testApi.apiCall.call( + method="GET", + host="cwsvJudo.bplaced.net", + url="participo/api/trainees", + headers={ + "Authorization": f"Basic: {config['apiKey']}" + } +) if t['gebDatum'] not in [None, "0000-00-00"]] + + +MatrixIm.post( + message="last birthdays: \n" + "\n".join([ + f"- {t['name']['given']} {t['name']['family']}: {get_days_since_birthday(t['date_of_birth'])} ({t['date_of_birth'].strftime('%d.%m.')})" + for t in sorted( + [t for t in trainees if get_days_since_birthday(t['date_of_birth'])>-14] + , key=lambda t: get_days_since_birthday(t['date_of_birth']) + ) + ]) + , home_server_url="https://matrix.org" + , room_id="!vFXxgKsLbRLdWCSTIO:matrix.org" + , credentials={ + 'user': "@ukenth-the-grumpy:matrix.org" + , 'password': "DQpSs1yn9Y26c1zo" + } +) + +MatrixIm.post( + message="next birthdays: \n" + "\n".join([ + f"- {t['name']['given']} {t['name']['family']}: {get_days_until_birthday(t['date_of_birth'])} ({t['date_of_birth'].strftime('%d.%m.')})" + for t in sorted( + [t for t in trainees if get_days_until_birthday(t['date_of_birth'])<14] + , key=lambda t: get_days_until_birthday(t['date_of_birth']) + ) + ]) + , home_server_url="https://matrix.org" + , room_id="!vFXxgKsLbRLdWCSTIO:matrix.org" + , credentials={ + 'user': "@ukenth-the-grumpy:matrix.org" + , 'password': "DQpSs1yn9Y26c1zo" + } +) \ No newline at end of file diff --git a/homepage/participo/bdCheck.sh b/homepage/participo/bdCheck.sh new file mode 100755 index 0000000..0d5a628 --- /dev/null +++ b/homepage/participo/bdCheck.sh @@ -0,0 +1,13 @@ +#!/bin/sh + +# If started as root, then re-start as user "marko": +# if [ "$(id -u)" -eq 0 ]; then +# exec sudo -H -u marko $0 "$@" +# echo "This is never reached."; +# fi + +# echo "This runs as user $(id -un)"; +# # prints "marko" +cd /home/marko/cwsvJudo/homepage/participo/; . .venv/bin/activate; python bdCheck.py ; deactivate; + +exit 0;