man01025

my_code

Jul 20th, 2025 (edited)
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | Source Code | 0 0
  1. #ISS Overhead notification !
  2.  
  3.  
  4.  
  5. #Modules
  6. import requests
  7. import datetime as dt
  8. import time
  9. import smtplib
  10.  
  11. #Parameters
  12. YOUR_LAT = 2.66568449792076 #Redacted
  13. YOUR_LNG = -2.05110689304828 #Redacted
  14. SENDER_EMAIL = "[email protected]" #Redacted
  15. SEMDER_PASS = "xxxx xxxx xxxx xxxx" #Redacted
  16. RECEIVER_EMAIL = "[email protected]" #Redacted
  17. NOW_HOUR = dt.datetime.now().hour
  18.  
  19. #Functions
  20. def is_dark(current_hour, sunrise, sunset):
  21. """Will return true if curent_hour is between sunset and sunrise"""
  22.  
  23. if current_hour > sunset and current_hour < sunrise:
  24. return True
  25. else:
  26. return False
  27.  
  28. def iss_in_range(your_lat, your_lng, iss_lat, iss_lng):
  29. """Will return True if iss is within viewing range"""
  30. #i know this part is ugly and inneficient but at least i understand it
  31. #lat check
  32. if iss_lat > your_lat -5 and iss_lat < your_lat +5:
  33. lat = True
  34. else:
  35. lat = False
  36. #lng check
  37. if iss_lng > your_lng -5 and iss_lng < your_lng +5:
  38. lng = True
  39. else:
  40. lng = False
  41. #decision
  42. if lat and lng:
  43. #print("DEBUG: IN RANGE")
  44. return True
  45. else:
  46. #print("DEBUG: NOT IN RANGE")
  47. return False
  48.  
  49.  
  50. class Iss:
  51. def __init__(self):
  52. self.lat = 0
  53. self.lng = 0
  54.  
  55. def update_localisation(self):
  56. iss_response = requests.get("http://api.open-notify.org/iss-now.json")
  57. iss_response.raise_for_status()
  58. self.lat = float(iss_response.json()["iss_position"]["latitude"])
  59. self.lng = float(iss_response.json()["iss_position"]["longitude"])
  60. iss = Iss()
  61.  
  62.  
  63.  
  64. class Sast:
  65. def __init__(self, your_lat, your_lng):
  66. self.sunrise = 0
  67. self.sunset = 0
  68. self.user_lat = your_lat
  69. self.user_lng = your_lng
  70.  
  71. def update(self):
  72. sun_parameters = {
  73. "lat": str(self.user_lat),
  74. "lng": str(self.user_lng),
  75. "formatted": 0
  76. }
  77. sun_response = requests.get("https://api.sunrise-sunset.org/json", params= sun_parameters)
  78. sun_response.raise_for_status()
  79. #Split required to isolate sunrise/sunset hour
  80. self.sunrise = int(sun_response.json()["results"]["sunrise"].split("T")[1].split(":")[0])
  81. self.sunset = int(sun_response.json()["results"]["sunset"].split("T")[1].split(":")[0])
  82. sast = Sast(YOUR_LAT, YOUR_LNG)
  83.  
  84.  
  85. #Logic
  86. while True:
  87. iss.update_localisation()
  88. sast.update()
  89. if is_dark(NOW_HOUR, sast.sunrise, sast.sunset) and iss_in_range(YOUR_LAT, YOUR_LNG, iss.lat, iss.lng):
  90. print("Iss visible ! sending email !")
  91. with smtplib.SMTP("smtp.gmail.com", port=587) as connection:
  92. connection.starttls()
  93. connection.login(user=SENDER_EMAIL, password=SEMDER_PASS)
  94. connection.sendmail(from_addr=SENDER_EMAIL, to_addrs=RECEIVER_EMAIL, msg="Subject: Look UP !\n\nThe iss should be visible right now !")
  95. print("Email sent !, going idle for 12 hours")
  96. time.sleep(43200)
  97. else:
  98. print("Iss not visible, going idle for 60sec ")
  99. time.sleep(60)
  100.  
  101.  
  102.  
Advertisement
Add Comment
Please, Sign In to add comment