Fix issues

This commit is contained in:
Midgard 2021-02-25 12:41:57 +01:00
parent f5120dfbd2
commit 18e5b99988
Signed by: midgard
GPG key ID: 511C112F1331BBB4

View file

@ -6,6 +6,7 @@ from time import sleep
import requests
import astral
from astral.sun import sun
from pytz import utc
CHANNEL = os.environ["SUNBOT_ANNOUNCE_CHANNEL"]
AUTHORIZATION = "Bearer " + os.environ["SUNBOT_ANNOUNCE_MATTERMOST_TOKEN"]
@ -13,6 +14,10 @@ URL = os.environ["SUNBOT_ANNOUNCE_MATTERMOST_API_ENDPOINT"] + "/posts"
LOCATION = astral.LocationInfo("Gent", "Belgium", "Europe/Brussels", 51.05, 3.72)
def in_timezone(d):
return d.astimezone(LOCATION.tzinfo)
def post(message):
payload = {
"channel_id": CHANNEL,
@ -30,33 +35,31 @@ def format_in(diff):
def wait_until(t, now):
seconds = (t - now).total_seconds()
print("Waiting {} seconds until {}".format(seconds, t), flush=True)
print("Waiting {} seconds until {}".format(seconds, in_timezone(t)), flush=True)
sleep(seconds)
def announce_sunrise(today):
print("Announcing sunrise", flush=True)
now = LOCATION.tzinfo.localize(datetime.datetime.now())
now = datetime.datetime.now(utc)
post(":sunrise: De zon komt op in Gent!\nZonnemiddag om {:%H:%M %Z} (binnen {})\nZonsondergang om {:%H:%M %Z} (binnen {})".format(
tomorrow["noon"],
format_in(tomorrow["noon"] - now),
tomorrow["sunset"],
format_in(tomorrow["sunset"] - now),
in_timezone(today["noon"]), format_in(today["noon"] - now),
in_timezone(today["sunset"]), format_in(today["sunset"] - now),
))
def announce_sunset(tomorrow):
print("Announcing sunset", flush=True)
now = LOCATION.tzinfo.localize(datetime.datetime.now())
now = datetime.datetime.now(utc)
post(":city_sunset: De zon gaat onder in Gent!\nZonsopkomst om {:%H:%M %Z} (binnen {})".format(
tomorrow["sunrise"],
format_in(tomorrow["sunrise"] - now),
in_timezone(tomorrow["sunrise"]), format_in(tomorrow["sunrise"] - now),
))
if __name__ == "__main__":
def main():
print("Starting Sunbot", flush=True)
while True:
now = LOCATION.tzinfo.localize(datetime.datetime.now())
now = datetime.datetime.now(utc)
today = sun(LOCATION.observer, date=now.date())
tomorrow = sun(LOCATION.observer, date=now.date() + datetime.timedelta(days=1))
@ -71,3 +74,7 @@ if __name__ == "__main__":
else:
wait_until(today["sunrise"], now)
announce_sunrise(today)
if __name__ == "__main__":
main()