Fix issues
This commit is contained in:
parent
f5120dfbd2
commit
18e5b99988
1 changed files with 18 additions and 11 deletions
|
@ -6,6 +6,7 @@ from time import sleep
|
||||||
import requests
|
import requests
|
||||||
import astral
|
import astral
|
||||||
from astral.sun import sun
|
from astral.sun import sun
|
||||||
|
from pytz import utc
|
||||||
|
|
||||||
CHANNEL = os.environ["SUNBOT_ANNOUNCE_CHANNEL"]
|
CHANNEL = os.environ["SUNBOT_ANNOUNCE_CHANNEL"]
|
||||||
AUTHORIZATION = "Bearer " + os.environ["SUNBOT_ANNOUNCE_MATTERMOST_TOKEN"]
|
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)
|
LOCATION = astral.LocationInfo("Gent", "Belgium", "Europe/Brussels", 51.05, 3.72)
|
||||||
|
|
||||||
|
def in_timezone(d):
|
||||||
|
return d.astimezone(LOCATION.tzinfo)
|
||||||
|
|
||||||
|
|
||||||
def post(message):
|
def post(message):
|
||||||
payload = {
|
payload = {
|
||||||
"channel_id": CHANNEL,
|
"channel_id": CHANNEL,
|
||||||
|
@ -30,33 +35,31 @@ def format_in(diff):
|
||||||
|
|
||||||
def wait_until(t, now):
|
def wait_until(t, now):
|
||||||
seconds = (t - now).total_seconds()
|
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)
|
sleep(seconds)
|
||||||
|
|
||||||
|
|
||||||
def announce_sunrise(today):
|
def announce_sunrise(today):
|
||||||
print("Announcing sunrise", flush=True)
|
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(
|
post(":sunrise: De zon komt op in Gent!\nZonnemiddag om {:%H:%M %Z} (binnen {})\nZonsondergang om {:%H:%M %Z} (binnen {})".format(
|
||||||
tomorrow["noon"],
|
in_timezone(today["noon"]), format_in(today["noon"] - now),
|
||||||
format_in(tomorrow["noon"] - now),
|
in_timezone(today["sunset"]), format_in(today["sunset"] - now),
|
||||||
tomorrow["sunset"],
|
|
||||||
format_in(tomorrow["sunset"] - now),
|
|
||||||
))
|
))
|
||||||
|
|
||||||
|
|
||||||
def announce_sunset(tomorrow):
|
def announce_sunset(tomorrow):
|
||||||
print("Announcing sunset", flush=True)
|
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(
|
post(":city_sunset: De zon gaat onder in Gent!\nZonsopkomst om {:%H:%M %Z} (binnen {})".format(
|
||||||
tomorrow["sunrise"],
|
in_timezone(tomorrow["sunrise"]), format_in(tomorrow["sunrise"] - now),
|
||||||
format_in(tomorrow["sunrise"] - now),
|
|
||||||
))
|
))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
def main():
|
||||||
print("Starting Sunbot", flush=True)
|
print("Starting Sunbot", flush=True)
|
||||||
while True:
|
while True:
|
||||||
now = LOCATION.tzinfo.localize(datetime.datetime.now())
|
now = datetime.datetime.now(utc)
|
||||||
today = sun(LOCATION.observer, date=now.date())
|
today = sun(LOCATION.observer, date=now.date())
|
||||||
tomorrow = sun(LOCATION.observer, date=now.date() + datetime.timedelta(days=1))
|
tomorrow = sun(LOCATION.observer, date=now.date() + datetime.timedelta(days=1))
|
||||||
|
|
||||||
|
@ -71,3 +74,7 @@ if __name__ == "__main__":
|
||||||
else:
|
else:
|
||||||
wait_until(today["sunrise"], now)
|
wait_until(today["sunrise"], now)
|
||||||
announce_sunrise(today)
|
announce_sunrise(today)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|
Loading…
Reference in a new issue