1
00:00:00,180 --> 00:00:03,390
All right. Congratulations on reaching the final project for the day.

2
00:00:03,719 --> 00:00:08,010
Head on over to the course resources and download the starting files for the ISS

3
00:00:08,039 --> 00:00:09,900
overhead notifier project.

4
00:00:10,650 --> 00:00:13,230
If you followed along with all the lessons today,

5
00:00:13,290 --> 00:00:18,290
then you've seen how we can get data from a particular API by simply just

6
00:00:19,860 --> 00:00:24,720
providing an endpoint. So this is the simplest type of API calls.

7
00:00:25,500 --> 00:00:30,450
And then you saw how we could pass, along with the API call, a set of parameters

8
00:00:30,720 --> 00:00:35,720
in the form of a Python dictionary in order to get a specific piece of data that

9
00:00:35,760 --> 00:00:40,320
we want based on the parameters that we pass over. So in this case,

10
00:00:40,320 --> 00:00:44,220
it was the sunrise and sunset time of our local area.

11
00:00:45,150 --> 00:00:45,930
Now later on,

12
00:00:45,930 --> 00:00:50,340
I showed you how we can split the string that we get back from the sunrise and

13
00:00:50,340 --> 00:00:54,840
sunset times in order to get the particular numbers that we're interested in,

14
00:00:55,140 --> 00:01:00,140
which is the time of the sunrise and sunset in the 24-hour clock format.

15
00:01:01,290 --> 00:01:03,450
Now it's time to put all of this together.

16
00:01:03,720 --> 00:01:08,370
If the ISS is close to my current position and it's currently dark,

17
00:01:08,430 --> 00:01:13,140
so basically it's nighttime, then send me an email to tell me to look up.

18
00:01:13,650 --> 00:01:17,310
That's the objective of the challenge. However, as a bonus challenge,

19
00:01:17,460 --> 00:01:21,480
see if you can further modify the code so that it runs every 60 seconds

20
00:01:21,600 --> 00:01:23,400
if the ISS is overhead.

21
00:01:24,300 --> 00:01:29,130
These are the three things that we need to achieve. And just as a quick tip

22
00:01:29,130 --> 00:01:30,240
before you get started,

23
00:01:30,540 --> 00:01:35,540
remember that the sunrise and sunset times are both in the format of strings.

24
00:01:36,630 --> 00:01:40,650
So if you want to be able to work with them to compare to time

25
00:01:40,650 --> 00:01:44,760
now, you also have to change this to an integer.

26
00:01:45,900 --> 00:01:50,520
Now, the second tip I've got is that the ISS position,

27
00:01:50,580 --> 00:01:54,840
the longitude and latitude that you get back, will be decimal numbers,

28
00:01:55,110 --> 00:01:57,360
but it is currently in the format of a string.

29
00:01:57,750 --> 00:02:01,500
So you'll need to convert that into a floating-point number

30
00:02:01,830 --> 00:02:06,660
if you want to compare it against your current position. Now,

31
00:02:06,690 --> 00:02:09,449
when we're checking whether if the ISS is overhead,

32
00:02:09,660 --> 00:02:14,070
we want to have a margin of error. It doesn't have to be right above me,

33
00:02:14,460 --> 00:02:19,380
precise to this number of decimal points. It can be plus or minus five.

34
00:02:19,380 --> 00:02:24,380
So if the ISS's latitude with anywhere between say 56 and,

35
00:02:24,990 --> 00:02:28,890
um, 51 minus five, which is 46,

36
00:02:29,190 --> 00:02:32,940
then that's perfectly fine. That means I'll still be able to see it.

37
00:02:33,510 --> 00:02:37,380
Take a look and find your own latitude and your own longitude

38
00:02:37,740 --> 00:02:42,120
and see if you can figure out how to compare that against the ISS's a latitude

39
00:02:42,120 --> 00:02:46,980
and longitude and adding in that degree of error. In fact,

40
00:02:47,010 --> 00:02:51,450
consider creating a function that returns true if your position is within plus five

41
00:02:51,780 --> 00:02:56,280
or minus five degrees of the ISS position and false otherwise.

42
00:02:57,150 --> 00:03:02,150
So you should now have all the capabilities to complete this challenge.

43
00:03:02,740 --> 00:03:03,573
Pause the video,

44
00:03:03,880 --> 00:03:07,570
give that a little bit of a think and see if you can complete the challenge.

45
00:03:08,970 --> 00:03:09,803
Thank you.

46
00:03:12,600 --> 00:03:13,140
All right. What

47
00:03:13,140 --> 00:03:18,140
we want to do up here is we wanna compare the ISS's position against our

48
00:03:19,140 --> 00:03:20,580
position. Now,

49
00:03:20,580 --> 00:03:25,580
what we want to check is to see if our position is within plus or minus five

50
00:03:27,570 --> 00:03:32,250
degrees of the ISS position. So let's take one part of that position,

51
00:03:32,280 --> 00:03:33,810
the ISS's latitude

52
00:03:34,350 --> 00:03:38,670
and we want to check to see if it is close to my latitude.

53
00:03:39,300 --> 00:03:41,550
If my latitude is currently 51,

54
00:03:41,910 --> 00:03:45,210
then 51 minus five would be 46.

55
00:03:45,570 --> 00:03:50,570
So we can check to see if 46 is less than or equal to the ISIS latitude.

56
00:03:51,540 --> 00:03:56,130
And then if the ISS latitude is less than or equal to 51 plus five,

57
00:03:56,130 --> 00:03:59,520
which is 56. Then if this is true,

58
00:03:59,760 --> 00:04:03,810
it means that this value is in between these two values.

59
00:04:04,260 --> 00:04:07,920
So let's replace these numbers with my actual latitude.

60
00:04:08,280 --> 00:04:10,260
So MY_LAT - 5

61
00:04:10,560 --> 00:04:13,440
and then MY_LAT + 5.

62
00:04:15,150 --> 00:04:19,350
And then we have to make sure that it's also true for the longitude.

63
00:04:19,709 --> 00:04:21,720
So we're going to do basically the same thing,

64
00:04:21,720 --> 00:04:24,120
but this time using my longitude.

65
00:04:24,450 --> 00:04:29,100
So in my longitude minus five is less than or equal to the ISS's longitude

66
00:04:29,580 --> 00:04:30,990
and the ISS longitude

67
00:04:31,010 --> 00:04:35,190
should be less than or equal to my longitude plus five.

68
00:04:35,850 --> 00:04:37,740
If both of these things are true,

69
00:04:37,770 --> 00:04:42,660
then my position is pretty much within plus or minus five degrees of the ISS.

70
00:04:44,550 --> 00:04:48,030
At this point, I should probably create some sort of a function for this.

71
00:04:48,030 --> 00:04:52,380
So I could say is_iss_overhead.

72
00:04:53,520 --> 00:04:58,260
And I can indent all of this into that function.

73
00:04:58,620 --> 00:05:02,610
And if this happens to be true, I'm going to return true.

74
00:05:03,660 --> 00:05:05,460
This part deals with checking

75
00:05:05,490 --> 00:05:09,990
whether if the ISS is at a similar position to my position,

76
00:05:10,620 --> 00:05:14,250
the next part deals with figuring out whether if it's nighttime or not.

77
00:05:14,790 --> 00:05:15,540
So again,

78
00:05:15,540 --> 00:05:20,190
let's create a function and I'll call it is_night.

79
00:05:21,330 --> 00:05:26,190
And this function is going to return true when it is actually nighttime. In

80
00:05:26,190 --> 00:05:28,290
order to figure out whether if it is nighttime,

81
00:05:28,890 --> 00:05:33,870
then we're going to get a response - so there's a typo there -

82
00:05:36,210 --> 00:05:40,020
from our request to the sunrise-sunset API

83
00:05:40,290 --> 00:05:44,400
passing in all of these parameters which contain my current location.

84
00:05:44,970 --> 00:05:46,860
And then from the data we get back,

85
00:05:46,890 --> 00:05:51,330
we figure out the sunrise and sunset time by formatting that string

86
00:05:51,600 --> 00:05:53,820
and then turning it into an integer

87
00:05:54,090 --> 00:05:59,090
which is going to represent the current hour in the 24-hour time format.

88
00:05:59,870 --> 00:06:01,670
So then we figure out, well,

89
00:06:01,670 --> 00:06:06,200
what is the current time using the datetime module?

90
00:06:06,680 --> 00:06:09,080
And we can check to see, well,

91
00:06:09,350 --> 00:06:14,350
if the time now is greater than or equal to the sunset time,

92
00:06:14,900 --> 00:06:18,470
or if the time now is less than or equal to the sunrise time,

93
00:06:18,770 --> 00:06:20,420
then it means it's dark.

94
00:06:22,550 --> 00:06:27,550
So now we're getting a warning telling us that this time now is a datetime

95
00:06:28,280 --> 00:06:33,080
object, but the sunset and sunrise are whole numbers or integers.

96
00:06:33,680 --> 00:06:37,340
So what's happened well? Datetime now is, in fact,

97
00:06:37,370 --> 00:06:38,960
the current date and time,

98
00:06:39,320 --> 00:06:44,320
but what we actually want is the current hour and that converts it into an

99
00:06:44,360 --> 00:06:46,670
integer making this comparison valid.

100
00:06:47,270 --> 00:06:50,360
So if it is currently nighttime,

101
00:06:50,600 --> 00:06:55,370
then we're going to return true. And if the ISS is overhead,

102
00:06:55,430 --> 00:07:00,290
then we're also going to return true. So now we've done these two parts,

103
00:07:00,410 --> 00:07:04,070
all that's left to do is once both of these things are true

104
00:07:04,430 --> 00:07:08,060
we can send ourselves an email to tell us to look up.

105
00:07:08,660 --> 00:07:13,660
So if the ISS is overhead and it is currently nighttime,

106
00:07:15,260 --> 00:07:18,020
then we're going to initiate our email sending

107
00:07:18,110 --> 00:07:21,020
which is going to require our smtplib.

108
00:07:21,590 --> 00:07:25,850
And I'm also going to add my email and password as constants up here.

109
00:07:27,950 --> 00:07:30,230
And now we can create our connection

110
00:07:33,050 --> 00:07:35,630
and I'm using again a Gmail server.

111
00:07:35,660 --> 00:07:40,340
So it's going to be smtp.gmail.com.

112
00:07:40,790 --> 00:07:44,450
So we went through all of this when we talked about email in detail.

113
00:07:44,720 --> 00:07:46,850
So I won't waste time talking about it again,

114
00:07:47,680 --> 00:07:48,513
right?

115
00:07:50,080 --> 00:07:55,080
I'm going to send this email from my own address and also to my own address

116
00:07:55,780 --> 00:08:00,780
because after all I'm just making a notification for myself. And the message in this

117
00:08:02,080 --> 00:08:07,080
case is going to have the subject line of look up and I can even add an emoji

118
00:08:09,910 --> 00:08:10,743
here.

119
00:08:12,970 --> 00:08:16,570
And then the content is going to say

120
00:08:16,570 --> 00:08:20,740
The ISS is above you in the sky.

121
00:08:22,780 --> 00:08:25,690
That's it. That's all there is to this. Now,

122
00:08:25,720 --> 00:08:30,220
it'd be quite hard to test this code because we have to wait for the perfect

123
00:08:30,250 --> 00:08:35,250
condition before all of these things will actually be true. While this is the end

124
00:08:36,190 --> 00:08:37,600
of the challenge for you,

125
00:08:37,900 --> 00:08:41,380
there's one thing I want to change with this code to make it a little bit

126
00:08:41,380 --> 00:08:42,212
better.

127
00:08:42,429 --> 00:08:46,450
We can actually put this if statement inside a while loop.

128
00:08:46,900 --> 00:08:50,380
So we could say while true,

129
00:08:50,440 --> 00:08:53,860
which means this loop is going to go again and again, and again,

130
00:08:53,890 --> 00:08:56,730
checking if the ISS is overhead and it's at night.

131
00:08:57,090 --> 00:09:00,750
Now this is going to happen quite frequently because its in a while loop,

132
00:09:01,050 --> 00:09:04,260
but we can slow it down by using the time module.

133
00:09:04,590 --> 00:09:09,590
So we could import the time module and then say time.sleep

134
00:09:11,130 --> 00:09:11,630
and we can get

135
00:09:11,630 --> 00:09:16,630
it to sleep for 60 seconds or however much long you want to wait between

136
00:09:17,480 --> 00:09:20,990
running the script. So now if you hit run,

137
00:09:21,740 --> 00:09:25,850
you won't see that final code where it tells you that your program is done

138
00:09:25,850 --> 00:09:26,683
running.

139
00:09:26,780 --> 00:09:31,400
It's actually continuously running in the background and it's going to execute

140
00:09:31,400 --> 00:09:33,800
this code every 60 seconds.

141
00:09:34,160 --> 00:09:37,250
So as long as you have your computer on and this is running,

142
00:09:37,610 --> 00:09:41,180
then you can run this for a whole 24 hours. And I'll bet you

143
00:09:41,180 --> 00:09:42,290
at some point in the night,

144
00:09:42,320 --> 00:09:46,070
you'll get two or three emails telling you that the ISS is overhead.

145
00:09:46,280 --> 00:09:50,600
So all you have to monitor is just to switch your notifications on on your phone

146
00:09:50,990 --> 00:09:54,530
and when that email comes in to look up into the sky.

147
00:09:55,850 --> 00:09:58,760
So I hope you enjoyed building this project with me

148
00:09:58,880 --> 00:10:02,240
and I hope that you're going to be able to spot the ISS tonight.

149
00:10:02,900 --> 00:10:05,930
If you do, take a video and let us know what it looks like.

