Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ISS Overhead notification !
- #Modules
- import requests
- import datetime as dt
- import time
- import smtplib
- #Parameters
- YOUR_LAT = 2.66568449792076 #Redacted
- YOUR_LNG = -2.05110689304828 #Redacted
- SENDER_EMAIL = "[email protected]" #Redacted
- SEMDER_PASS = "xxxx xxxx xxxx xxxx" #Redacted
- RECEIVER_EMAIL = "[email protected]" #Redacted
- NOW_HOUR = dt.datetime.now().hour
- #Functions
- def is_dark(current_hour, sunrise, sunset):
- """Will return true if curent_hour is between sunset and sunrise"""
- if current_hour > sunset and current_hour < sunrise:
- return True
- else:
- return False
- def iss_in_range(your_lat, your_lng, iss_lat, iss_lng):
- """Will return True if iss is within viewing range"""
- #i know this part is ugly and inneficient but at least i understand it
- #lat check
- if iss_lat > your_lat -5 and iss_lat < your_lat +5:
- lat = True
- else:
- lat = False
- #lng check
- if iss_lng > your_lng -5 and iss_lng < your_lng +5:
- lng = True
- else:
- lng = False
- #decision
- if lat and lng:
- #print("DEBUG: IN RANGE")
- return True
- else:
- #print("DEBUG: NOT IN RANGE")
- return False
- class Iss:
- def __init__(self):
- self.lat = 0
- self.lng = 0
- def update_localisation(self):
- iss_response = requests.get("http://api.open-notify.org/iss-now.json")
- iss_response.raise_for_status()
- self.lat = float(iss_response.json()["iss_position"]["latitude"])
- self.lng = float(iss_response.json()["iss_position"]["longitude"])
- iss = Iss()
- class Sast:
- def __init__(self, your_lat, your_lng):
- self.sunrise = 0
- self.sunset = 0
- self.user_lat = your_lat
- self.user_lng = your_lng
- def update(self):
- sun_parameters = {
- "lat": str(self.user_lat),
- "lng": str(self.user_lng),
- "formatted": 0
- }
- sun_response = requests.get("https://api.sunrise-sunset.org/json", params= sun_parameters)
- sun_response.raise_for_status()
- #Split required to isolate sunrise/sunset hour
- self.sunrise = int(sun_response.json()["results"]["sunrise"].split("T")[1].split(":")[0])
- self.sunset = int(sun_response.json()["results"]["sunset"].split("T")[1].split(":")[0])
- sast = Sast(YOUR_LAT, YOUR_LNG)
- #Logic
- while True:
- iss.update_localisation()
- sast.update()
- if is_dark(NOW_HOUR, sast.sunrise, sast.sunset) and iss_in_range(YOUR_LAT, YOUR_LNG, iss.lat, iss.lng):
- print("Iss visible ! sending email !")
- with smtplib.SMTP("smtp.gmail.com", port=587) as connection:
- connection.starttls()
- connection.login(user=SENDER_EMAIL, password=SEMDER_PASS)
- connection.sendmail(from_addr=SENDER_EMAIL, to_addrs=RECEIVER_EMAIL, msg="Subject: Look UP !\n\nThe iss should be visible right now !")
- print("Email sent !, going idle for 12 hours")
- time.sleep(43200)
- else:
- print("Iss not visible, going idle for 60sec ")
- time.sleep(60)
Advertisement
Add Comment
Please, Sign In to add comment