1. 나의 경도, 위도 찾기
MY_LAT = 37.663998 # 위도 latitude
MY_LONG = 127.978462 # 경도 longitude
Latitude and Longitude Finder on Map Get Coordinates
What is Latitude and Longitude? Just like every actual house has its address (which includes the number, the name of the street, city, etc), every single point on the surface of earth can be specified by the latitude and longitude coordinates. Therefore, b
www.latlong.net
2. API
iss API
iss : International Space Station 국제우주정거장
국제우주정거장의 현재 위치가 담긴 API를 요청한다.
http://open-notify.org/Open-Notify-API/ISS-Location-Now/
Open Notify -- API Doc | ISS Current Location
International Space Station Current Location The International Space Station is moving at close to 28,000 km/h so its location changes really fast! Where is it right now? Overview This is a simple api to return the current location of the ISS. It returns t
open-notify.org
iss의 위치가 내 위치와 같다면 True를 반환한다.
def is_iss_overhead():
response = requests.get(url="http://api.open-notify.org/iss-now.json")
response.raise_for_status()
data = response.json()
iss_latitude = float(data["iss_position"]["latitude"])
iss_longitude = float(data["iss_position"]["longitude"])
if MY_LAT-5 <- iss_latitude <= MY_LAT+5 and MY_LONG-5 <= iss_longitude <= MY_LONG+5:
return True
3. 시간대 확인
현재가 밤인지 확인한다.
밤이라면 True를 반환한다.
import datetime
def is_night():
parameters = {
"lat": MY_LAT,
"lng": MY_LONG,
"formatted": 0,
}
response = requests.get("https://api.sunrise-sunset.org/json", params=parameters)
response.raise_for_status()
data = response.json()
sunrise = int(data["results"]["sunrise"].split("T")[1].split(":")[0])
sunset = int(data["results"]["sunset"].split("T")[1].split(":")[0])
time_now = datetime.now().hour
if time_now >= sunset or time_now <= sunrise:
return True
4. 이메일 보내기
time.sleep(60) 으로 60초마다 while문을 실행한다.
smtp 모듈을 사용하여 만약 우주정거장과 내 위치가 같다면
해당 메시지가 담긴 이메일을 보낸다.
import time
import smtplib
MY_EMAIL = "이메일 주소"
MY_PASSWORD = "비밀번호"
while True:
time.sleep(60)
if is_iss_overhead() and is_night():
connection = smtplib.SMTP("SMTP 주소")
connection.starttls()
connection.login(MY_EMAIL, MY_PASSWORD)
connection.sendmail(
from_addr=MY_EMAIL,
to_addrs=MY_EMAIL,
msg="Subject: 지금 머리 위에 국제우주정거장이 있습니다!."
)
<최종코드>
import requests
import datetime
import time
import smtplib
MY_LAT = 37.663998 # 위도 latitude
MY_LONG = 127.978462 # 경도 longitude
MY_EMAIL = "이메일 주소"
MY_PASSWORD = "비밀번호"
def is_iss_overhead():
response = requests.get(url="http://api.open-notify.org/iss-now.json")
response.raise_for_status()
data = response.json()
iss_latitude = float(data["iss_position"]["latitude"])
iss_longitude = float(data["iss_position"]["longitude"])
if MY_LAT-5 <- iss_latitude <= MY_LAT+5 and MY_LONG-5 <= iss_longitude <= MY_LONG+5:
return True
def is_night():
parameters = {
"lat": MY_LAT,
"lng": MY_LONG,
"formatted": 0,
}
response = requests.get("https://api.sunrise-sunset.org/json", params=parameters)
response.raise_for_status()
data = response.json()
sunrise = int(data["results"]["sunrise"].split("T")[1].split(":")[0])
sunset = int(data["results"]["sunset"].split("T")[1].split(":")[0])
time_now = datetime.now().hour
if time_now >= sunset or time_now <= sunrise:
return True
while True:
time.sleep(60)
if is_iss_overhead() and is_night():
connection = smtplib.SMTP("SMTP 주소")
connection.starttls()
connection.login(MY_EMAIL, MY_PASSWORD)
connection.sendmail(
from_addr=MY_EMAIL,
to_addrs=MY_EMAIL,
msg="Subject: 지금 머리 위에 국제우주정거장이 있습니다!."
)