All right. Congratulations on reaching the final project for the day.
Head on over to the course resources and download the starting files for the ISS
overhead notifier project.
If you followed along with all the lessons today,
then you've seen how we can get data from a particular API by simply just
providing an endpoint. So this is the simplest type of API calls.
And then you saw how we could pass, along with the API call, a set of parameters
in the form of a Python dictionary in order to get a specific piece of data that
we want based on the parameters that we pass over. So in this case,
it was the sunrise and sunset time of our local area.
Now later on,
I showed you how we can split the string that we get back from the sunrise and
sunset times in order to get the particular numbers that we're interested in,
which is the time of the sunrise and sunset in the 24-hour clock format.
Now it's time to put all of this together.
If the ISS is close to my current position and it's currently dark,
so basically it's nighttime, then send me an email to tell me to look up.
That's the objective of the challenge. However, as a bonus challenge,
see if you can further modify the code so that it runs every 60 seconds
if the ISS is overhead.
These are the three things that we need to achieve. And just as a quick tip
before you get started,
remember that the sunrise and sunset times are both in the format of strings.
So if you want to be able to work with them to compare to time
now, you also have to change this to an integer.
Now, the second tip I've got is that the ISS position,
the longitude and latitude that you get back, will be decimal numbers,
but it is currently in the format of a string.
So you'll need to convert that into a floating-point number
if you want to compare it against your current position. Now,
when we're checking whether if the ISS is overhead,
we want to have a margin of error. It doesn't have to be right above me,
precise to this number of decimal points. It can be plus or minus five.
So if the ISS's latitude with anywhere between say 56 and,
um, 51 minus five, which is 46,
then that's perfectly fine. That means I'll still be able to see it.
Take a look and find your own latitude and your own longitude
and see if you can figure out how to compare that against the ISS's a latitude
and longitude and adding in that degree of error. In fact,
consider creating a function that returns true if your position is within plus five
or minus five degrees of the ISS position and false otherwise.
So you should now have all the capabilities to complete this challenge.
Pause the video,
give that a little bit of a think and see if you can complete the challenge.
1: Thank you.
0: All right. What
we want to do up here is we wanna compare the ISS's position against our
position. Now,
what we want to check is to see if our position is within plus or minus five
degrees of the ISS position. So let's take one part of that position,
the ISS's latitude
and we want to check to see if it is close to my latitude.
If my latitude is currently 51,
then 51 minus five would be 46.
So we can check to see if 46 is less than or equal to the ISIS latitude.
And then if the ISS latitude is less than or equal to 51 plus five,
which is 56. Then if this is true,
it means that this value is in between these two values.
So let's replace these numbers with my actual latitude.
So MY_LAT - 5
and then MY_LAT + 5.
And then we have to make sure that it's also true for the longitude.
So we're going to do basically the same thing,
but this time using my longitude.
So in my longitude minus five is less than or equal to the ISS's longitude
and the ISS longitude
should be less than or equal to my longitude plus five.
If both of these things are true,
then my position is pretty much within plus or minus five degrees of the ISS.
At this point, I should probably create some sort of a function for this.
So I could say is_iss_overhead.
And I can indent all of this into that function.
And if this happens to be true, I'm going to return true.
This part deals with checking
whether if the ISS is at a similar position to my position,
the next part deals with figuring out whether if it's nighttime or not.
So again,
let's create a function and I'll call it is_night.
And this function is going to return true when it is actually nighttime. In
order to figure out whether if it is nighttime,
then we're going to get a response - so there's a typo there -
from our request to the sunrise-sunset API
passing in all of these parameters which contain my current location.
And then from the data we get back,
we figure out the sunrise and sunset time by formatting that string
and then turning it into an integer
which is going to represent the current hour in the 24-hour time format.
So then we figure out, well,
what is the current time using the datetime module?
And we can check to see, well,
if the time now is greater than or equal to the sunset time,
or if the time now is less than or equal to the sunrise time,
then it means it's dark.
So now we're getting a warning telling us that this time now is a datetime
object, but the sunset and sunrise are whole numbers or integers.
So what's happened well? Datetime now is, in fact,
the current date and time,
but what we actually want is the current hour and that converts it into an
integer making this comparison valid.
So if it is currently nighttime,
then we're going to return true. And if the ISS is overhead,
then we're also going to return true. So now we've done these two parts,
all that's left to do is once both of these things are true
we can send ourselves an email to tell us to look up.
So if the ISS is overhead and it is currently nighttime,
then we're going to initiate our email sending
which is going to require our smtplib.
And I'm also going to add my email and password as constants up here.
And now we can create our connection
and I'm using again a Gmail server.
So it's going to be smtp.gmail.com.
So we went through all of this when we talked about email in detail.
So I won't waste time talking about it again,
2: right?
0: I'm going to send this email from my own address and also to my own address
because after all I'm just making a notification for myself. And the message in this
case is going to have the subject line of look up and I can even add an emoji
here.
And then the content is going to say
The ISS is above you in the sky.
That's it. That's all there is to this. Now,
it'd be quite hard to test this code because we have to wait for the perfect
condition before all of these things will actually be true. While this is the end
of the challenge for you,
there's one thing I want to change with this code to make it a little bit
better.
We can actually put this if statement inside a while loop.
So we could say while true,
which means this loop is going to go again and again, and again,
checking if the ISS is overhead and it's at night.
Now this is going to happen quite frequently because its in a while loop,
but we can slow it down by using the time module.
So we could import the time module and then say time.sleep
and we can get
it to sleep for 60 seconds or however much long you want to wait between
running the script. So now if you hit run,
you won't see that final code where it tells you that your program is done
running.
It's actually continuously running in the background and it's going to execute
this code every 60 seconds.
So as long as you have your computer on and this is running,
then you can run this for a whole 24 hours. And I'll bet you
at some point in the night,
you'll get two or three emails telling you that the ISS is overhead.
So all you have to monitor is just to switch your notifications on on your phone
and when that email comes in to look up into the sky.
So I hope you enjoyed building this project with me
and I hope that you're going to be able to spot the ISS tonight.
If you do, take a video and let us know what it looks like.