1
0
Fork 0
mirror of https://github.com/Findus23/suntracking.git synced 2024-08-27 19:52:18 +02:00

refresh notifications and use telegram instead of mail

This commit is contained in:
Lukas Winkler 2019-04-28 19:26:31 +02:00
parent 8e6db3387d
commit ca22545542
Signed by: lukas
GPG key ID: 54DE4D798D244853
5 changed files with 47 additions and 45 deletions

View file

@ -1,16 +1,16 @@
from datetime import datetime, timedelta, time
from pytz import timezone
midday = time(12)
import astropy.coordinates as coord
from astropy.time import Time
from pytz import timezone
import config
midday = time(12)
tz = timezone(config.tz)
def time2altitude(time: datetime) -> float:
astro_time = Time(tz.localize(time))
altaz = coord.AltAz(location=config.loc, obstime=astro_time)
@ -18,17 +18,16 @@ def time2altitude(time: datetime) -> float:
return sun.transform_to(altaz).alt.degree
def get_time(altitude, time=False):
def get_time(altitude):
lower = datetime.combine(datetime.now().date(), midday)
upper = lower + timedelta(hours=12)
while upper - lower > timedelta(seconds=30):
while upper - lower > timedelta(seconds=1):
middle = lower + (upper - lower) / 2
if time2altitude(middle) > altitude:
lower = middle
else:
upper = middle
# print(upper, lower)
return lower + (upper - lower) / 2

View file

@ -18,8 +18,7 @@ with open("sonnenuntergang.txt") as f:
angles = []
for line in lines:
if "#" in line:
print("skipped")
if "#" in line or not line:
continue
parsetime = tz.localize(datetime.strptime("2018 " + line, "%Y %d.%m %H:%M"))
print(parsetime.isoformat())

42
main.py
View file

@ -1,17 +1,26 @@
import time
from datetime import datetime, date
from datetime import timedelta, datetime
import schedule
import telegram
import guess
from sendmail import sendmail
from config import telegram_token, telegram_chat_id
def send_notification(time, future=False):
subject = "☀️ at {time}".format(time=time)
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))
if future:
subject += " - {min} minutes left".format(min=future)
sendmail(subject, subject)
subject += " - 10 minutes left"
bot = telegram.Bot(token=telegram_token)
bot.sendMessage(chat_id=telegram_chat_id, text=subject)
# sendmail(subject, subject)
return schedule.CancelJob
@ -22,23 +31,24 @@ def create_schedule():
standard_derivation = float(lines[1].strip())
print(altitude, standard_derivation)
sunset_time = guess.get_time(altitude).time()
print(sunset_time)
sunset = guess.get_time(altitude)
accuracy = guess.get_time(altitude - standard_derivation) - sunset
print(accuracy)
print(sunset)
s = schedule.every().day
s.at_time = sunset_time
s.do(send_notification, sunset_time)
s.at_time = sunset.time()
s.do(send_notification, sunset, accuracy)
prewarn_time = guess.get_time(altitude + standard_derivation * 3).time()
print(prewarn_time)
s = schedule.every().day
s.at_time = prewarn_time
diff = datetime.combine(date.today(), sunset_time) - datetime.combine(date.today(), prewarn_time)
s.do(send_notification, sunset_time, future=diff)
p = schedule.every().day
p.at_time = (sunset - timedelta(minutes=10)).time()
p.do(send_notification, sunset, accuracy, True)
create_schedule()
schedule.every().day.at("12:00").do(create_schedule)
if datetime.now().hour > 12:
create_schedule()
while True:
schedule.run_pending()

View file

@ -1,4 +1,13 @@
astropy==3.0.2
numpy==1.14.3
pytz==2018.4
schedule==0.5.0
asn1crypto==0.24.0
astropy==3.1.2
certifi==2019.3.9
cffi==1.12.3
cryptography==2.6.1
future==0.17.1
numpy==1.16.3
pycparser==2.19
python-telegram-bot==12.0.0b1
pytz==2019.1
schedule==0.6.0
six==1.12.0
tornado==6.0.2

View file

@ -1,15 +0,0 @@
import smtplib
from email.mime.text import MIMEText
from config import by, to
def sendmail(subject, text):
msg = MIMEText(text)
msg['Subject'] = subject
msg['From'] = by
msg['To'] = to
s = smtplib.SMTP('localhost')
s.sendmail(by, [to], msg.as_string())