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:
parent
8e6db3387d
commit
ca22545542
5 changed files with 47 additions and 45 deletions
15
guess.py
15
guess.py
|
@ -1,15 +1,15 @@
|
||||||
from datetime import datetime, timedelta, time
|
from datetime import datetime, timedelta, time
|
||||||
|
|
||||||
from pytz import timezone
|
|
||||||
|
|
||||||
midday = time(12)
|
|
||||||
|
|
||||||
import astropy.coordinates as coord
|
import astropy.coordinates as coord
|
||||||
from astropy.time import Time
|
from astropy.time import Time
|
||||||
|
from pytz import timezone
|
||||||
|
|
||||||
import config
|
import config
|
||||||
|
|
||||||
tz=timezone(config.tz)
|
midday = time(12)
|
||||||
|
|
||||||
|
tz = timezone(config.tz)
|
||||||
|
|
||||||
|
|
||||||
def time2altitude(time: datetime) -> float:
|
def time2altitude(time: datetime) -> float:
|
||||||
astro_time = Time(tz.localize(time))
|
astro_time = Time(tz.localize(time))
|
||||||
|
@ -18,17 +18,16 @@ def time2altitude(time: datetime) -> float:
|
||||||
return sun.transform_to(altaz).alt.degree
|
return sun.transform_to(altaz).alt.degree
|
||||||
|
|
||||||
|
|
||||||
def get_time(altitude, time=False):
|
def get_time(altitude):
|
||||||
lower = datetime.combine(datetime.now().date(), midday)
|
lower = datetime.combine(datetime.now().date(), midday)
|
||||||
upper = lower + timedelta(hours=12)
|
upper = lower + timedelta(hours=12)
|
||||||
|
|
||||||
while upper - lower > timedelta(seconds=30):
|
while upper - lower > timedelta(seconds=1):
|
||||||
middle = lower + (upper - lower) / 2
|
middle = lower + (upper - lower) / 2
|
||||||
if time2altitude(middle) > altitude:
|
if time2altitude(middle) > altitude:
|
||||||
lower = middle
|
lower = middle
|
||||||
else:
|
else:
|
||||||
upper = middle
|
upper = middle
|
||||||
# print(upper, lower)
|
|
||||||
|
|
||||||
return lower + (upper - lower) / 2
|
return lower + (upper - lower) / 2
|
||||||
|
|
||||||
|
|
|
@ -18,8 +18,7 @@ with open("sonnenuntergang.txt") as f:
|
||||||
angles = []
|
angles = []
|
||||||
|
|
||||||
for line in lines:
|
for line in lines:
|
||||||
if "#" in line:
|
if "#" in line or not line:
|
||||||
print("skipped")
|
|
||||||
continue
|
continue
|
||||||
parsetime = tz.localize(datetime.strptime("2018 " + line, "%Y %d.%m %H:%M"))
|
parsetime = tz.localize(datetime.strptime("2018 " + line, "%Y %d.%m %H:%M"))
|
||||||
print(parsetime.isoformat())
|
print(parsetime.isoformat())
|
||||||
|
|
42
main.py
42
main.py
|
@ -1,17 +1,26 @@
|
||||||
import time
|
import time
|
||||||
from datetime import datetime, date
|
from datetime import timedelta, datetime
|
||||||
|
|
||||||
import schedule
|
import schedule
|
||||||
|
import telegram
|
||||||
|
|
||||||
import guess
|
import guess
|
||||||
from sendmail import sendmail
|
from config import telegram_token, telegram_chat_id
|
||||||
|
|
||||||
|
|
||||||
def send_notification(time, future=False):
|
def timedelta_to_string(delta: timedelta):
|
||||||
subject = "☀️ at {time}".format(time=time)
|
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:
|
if future:
|
||||||
subject += " - {min} minutes left".format(min=future)
|
subject += " - 10 minutes left"
|
||||||
sendmail(subject, subject)
|
bot = telegram.Bot(token=telegram_token)
|
||||||
|
|
||||||
|
bot.sendMessage(chat_id=telegram_chat_id, text=subject)
|
||||||
|
# sendmail(subject, subject)
|
||||||
return schedule.CancelJob
|
return schedule.CancelJob
|
||||||
|
|
||||||
|
|
||||||
|
@ -22,23 +31,24 @@ def create_schedule():
|
||||||
standard_derivation = float(lines[1].strip())
|
standard_derivation = float(lines[1].strip())
|
||||||
print(altitude, standard_derivation)
|
print(altitude, standard_derivation)
|
||||||
|
|
||||||
sunset_time = guess.get_time(altitude).time()
|
sunset = guess.get_time(altitude)
|
||||||
print(sunset_time)
|
accuracy = guess.get_time(altitude - standard_derivation) - sunset
|
||||||
|
print(accuracy)
|
||||||
|
print(sunset)
|
||||||
s = schedule.every().day
|
s = schedule.every().day
|
||||||
s.at_time = sunset_time
|
s.at_time = sunset.time()
|
||||||
s.do(send_notification, sunset_time)
|
s.do(send_notification, sunset, accuracy)
|
||||||
|
|
||||||
prewarn_time = guess.get_time(altitude + standard_derivation * 3).time()
|
p = schedule.every().day
|
||||||
print(prewarn_time)
|
p.at_time = (sunset - timedelta(minutes=10)).time()
|
||||||
s = schedule.every().day
|
p.do(send_notification, sunset, accuracy, True)
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
create_schedule()
|
create_schedule()
|
||||||
|
|
||||||
schedule.every().day.at("12:00").do(create_schedule)
|
schedule.every().day.at("12:00").do(create_schedule)
|
||||||
|
if datetime.now().hour > 12:
|
||||||
|
create_schedule()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
schedule.run_pending()
|
schedule.run_pending()
|
||||||
|
|
|
@ -1,4 +1,13 @@
|
||||||
astropy==3.0.2
|
asn1crypto==0.24.0
|
||||||
numpy==1.14.3
|
astropy==3.1.2
|
||||||
pytz==2018.4
|
certifi==2019.3.9
|
||||||
schedule==0.5.0
|
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
|
||||||
|
|
15
sendmail.py
15
sendmail.py
|
@ -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())
|
|
Loading…
Reference in a new issue