2018-05-18 21:59:06 +02:00
|
|
|
import time
|
2019-04-28 19:26:31 +02:00
|
|
|
from datetime import timedelta, datetime
|
2018-05-18 21:59:06 +02:00
|
|
|
|
|
|
|
import schedule
|
2019-04-28 19:26:31 +02:00
|
|
|
import telegram
|
2018-05-18 21:59:06 +02:00
|
|
|
|
|
|
|
import guess
|
2019-04-28 19:26:31 +02:00
|
|
|
from config import telegram_token, telegram_chat_id
|
2018-05-18 21:59:06 +02:00
|
|
|
|
|
|
|
|
2019-04-28 19:26:31 +02:00
|
|
|
def timedelta_to_string(delta: timedelta):
|
|
|
|
minutes, seconds = divmod(delta.seconds, 60)
|
|
|
|
return ":".join(map(str, [minutes, seconds]))
|
|
|
|
|
|
|
|
|
|
|
|
def send_notification(time, accuracy, future=False):
|
|
|
|
subject = "☀️ at {time} (±{acc})".format(time=time.strftime("%H:%M:%S"), acc=timedelta_to_string(accuracy))
|
2018-05-18 22:14:57 +02:00
|
|
|
if future:
|
2019-04-28 19:26:31 +02:00
|
|
|
subject += " - 10 minutes left"
|
|
|
|
bot = telegram.Bot(token=telegram_token)
|
|
|
|
|
|
|
|
bot.sendMessage(chat_id=telegram_chat_id, text=subject)
|
|
|
|
# sendmail(subject, subject)
|
2018-05-18 21:59:06 +02:00
|
|
|
return schedule.CancelJob
|
|
|
|
|
|
|
|
|
|
|
|
def create_schedule():
|
|
|
|
with open("average.txt") as file:
|
|
|
|
lines = file.readlines()
|
|
|
|
altitude = float(lines[0].strip())
|
|
|
|
standard_derivation = float(lines[1].strip())
|
|
|
|
print(altitude, standard_derivation)
|
|
|
|
|
2019-04-28 19:26:31 +02:00
|
|
|
sunset = guess.get_time(altitude)
|
|
|
|
accuracy = guess.get_time(altitude - standard_derivation) - sunset
|
|
|
|
print(accuracy)
|
|
|
|
print(sunset)
|
2018-05-18 22:14:57 +02:00
|
|
|
s = schedule.every().day
|
2019-04-28 19:26:31 +02:00
|
|
|
s.at_time = sunset.time()
|
|
|
|
s.do(send_notification, sunset, accuracy)
|
2018-05-18 21:59:06 +02:00
|
|
|
|
2019-04-28 19:26:31 +02:00
|
|
|
p = schedule.every().day
|
|
|
|
p.at_time = (sunset - timedelta(minutes=10)).time()
|
|
|
|
p.do(send_notification, sunset, accuracy, True)
|
2018-05-18 21:59:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
create_schedule()
|
|
|
|
|
|
|
|
schedule.every().day.at("12:00").do(create_schedule)
|
2019-04-28 19:26:31 +02:00
|
|
|
if datetime.now().hour > 12:
|
|
|
|
create_schedule()
|
2018-05-18 21:59:06 +02:00
|
|
|
|
|
|
|
while True:
|
|
|
|
schedule.run_pending()
|
|
|
|
time.sleep(1)
|