WEBVTT

0
00:00.180 --> 00:03.390
All right. Congratulations on reaching the final project for the day.

1
00:03.720 --> 00:08.010
Head on over to the course resources and download the starting files for the ISS

2
00:08.040 --> 00:09.900
overhead notifier project.

3
00:10.650 --> 00:13.230
If you followed along with all the lessons today,

4
00:13.290 --> 00:18.290
then you've seen how we can get data from a particular API by simply just

5
00:19.860 --> 00:24.720
providing an endpoint. So this is the simplest type of API calls.

6
00:25.500 --> 00:30.450
And then you saw how we could pass, along with the API call, a set of parameters

7
00:30.720 --> 00:35.720
in the form of a Python dictionary in order to get a specific piece of data that

8
00:35.760 --> 00:40.320
we want based on the parameters that we pass over. So in this case,

9
00:40.320 --> 00:44.220
it was the sunrise and sunset time of our local area.

10
00:45.150 --> 00:45.930
Now later on,

11
00:45.930 --> 00:50.340
I showed you how we can split the string that we get back from the sunrise and

12
00:50.340 --> 00:54.840
sunset times in order to get the particular numbers that we're interested in,

13
00:55.140 --> 01:00.140
which is the time of the sunrise and sunset in the 24-hour clock format.

14
01:01.290 --> 01:03.450
Now it's time to put all of this together.

15
01:03.720 --> 01:08.370
If the ISS is close to my current position and it's currently dark,

16
01:08.430 --> 01:13.140
so basically it's nighttime, then send me an email to tell me to look up.

17
01:13.650 --> 01:17.310
That's the objective of the challenge. However, as a bonus challenge,

18
01:17.460 --> 01:21.480
see if you can further modify the code so that it runs every 60 seconds

19
01:21.600 --> 01:23.400
if the ISS is overhead.

20
01:24.300 --> 01:29.130
These are the three things that we need to achieve. And just as a quick tip

21
01:29.130 --> 01:30.240
before you get started,

22
01:30.540 --> 01:35.540
remember that the sunrise and sunset times are both in the format of strings.

23
01:36.630 --> 01:40.650
So if you want to be able to work with them to compare to time

24
01:40.650 --> 01:44.760
now, you also have to change this to an integer.

25
01:45.900 --> 01:50.520
Now, the second tip I've got is that the ISS position,

26
01:50.580 --> 01:54.840
the longitude and latitude that you get back, will be decimal numbers,

27
01:55.110 --> 01:57.360
but it is currently in the format of a string.

28
01:57.750 --> 02:01.500
So you'll need to convert that into a floating-point number

29
02:01.830 --> 02:06.660
if you want to compare it against your current position. Now,

30
02:06.690 --> 02:09.450
when we're checking whether if the ISS is overhead,

31
02:09.660 --> 02:14.070
we want to have a margin of error. It doesn't have to be right above me,

32
02:14.460 --> 02:19.380
precise to this number of decimal points. It can be plus or minus five.

33
02:19.380 --> 02:24.380
So if the ISS's latitude with anywhere between say 56 and,

34
02:24.990 --> 02:28.890
um, 51 minus five, which is 46,

35
02:29.190 --> 02:32.940
then that's perfectly fine. That means I'll still be able to see it.

36
02:33.510 --> 02:37.380
Take a look and find your own latitude and your own longitude

37
02:37.740 --> 02:42.120
and see if you can figure out how to compare that against the ISS's a latitude

38
02:42.120 --> 02:46.980
and longitude and adding in that degree of error. In fact,

39
02:47.010 --> 02:51.450
consider creating a function that returns true if your position is within plus five

40
02:51.780 --> 02:56.280
or minus five degrees of the ISS position and false otherwise.

41
02:57.150 --> 03:02.150
So you should now have all the capabilities to complete this challenge.

42
03:02.740 --> 03:03.573
Pause the video,

43
03:03.880 --> 03:07.570
give that a little bit of a think and see if you can complete the challenge.

44
03:08.970 --> 03:09.803
<v 1>Thank you.</v>

45
03:12.600 --> 03:13.140
<v 0>All right. What</v>

46
03:13.140 --> 03:18.140
we want to do up here is we wanna compare the ISS's position against our

47
03:19.140 --> 03:20.580
position. Now,

48
03:20.580 --> 03:25.580
what we want to check is to see if our position is within plus or minus five

49
03:27.570 --> 03:32.250
degrees of the ISS position. So let's take one part of that position,

50
03:32.280 --> 03:33.810
the ISS's latitude

51
03:34.350 --> 03:38.670
and we want to check to see if it is close to my latitude.

52
03:39.300 --> 03:41.550
If my latitude is currently 51,

53
03:41.910 --> 03:45.210
then 51 minus five would be 46.

54
03:45.570 --> 03:50.570
So we can check to see if 46 is less than or equal to the ISIS latitude.

55
03:51.540 --> 03:56.130
And then if the ISS latitude is less than or equal to 51 plus five,

56
03:56.130 --> 03:59.520
which is 56. Then if this is true,

57
03:59.760 --> 04:03.810
it means that this value is in between these two values.

58
04:04.260 --> 04:07.920
So let's replace these numbers with my actual latitude.

59
04:08.280 --> 04:10.260
So MY_LAT - 5

60
04:10.560 --> 04:13.440
and then MY_LAT + 5.

61
04:15.150 --> 04:19.350
And then we have to make sure that it's also true for the longitude.

62
04:19.710 --> 04:21.720
So we're going to do basically the same thing,

63
04:21.720 --> 04:24.120
but this time using my longitude.

64
04:24.450 --> 04:29.100
So in my longitude minus five is less than or equal to the ISS's longitude

65
04:29.580 --> 04:30.990
and the ISS longitude

66
04:31.010 --> 04:35.190
should be less than or equal to my longitude plus five.

67
04:35.850 --> 04:37.740
If both of these things are true,

68
04:37.770 --> 04:42.660
then my position is pretty much within plus or minus five degrees of the ISS.

69
04:44.550 --> 04:48.030
At this point, I should probably create some sort of a function for this.

70
04:48.030 --> 04:52.380
So I could say is_iss_overhead.

71
04:53.520 --> 04:58.260
And I can indent all of this into that function.

72
04:58.620 --> 05:02.610
And if this happens to be true, I'm going to return true.

73
05:03.660 --> 05:05.460
This part deals with checking

74
05:05.490 --> 05:09.990
whether if the ISS is at a similar position to my position,

75
05:10.620 --> 05:14.250
the next part deals with figuring out whether if it's nighttime or not.

76
05:14.790 --> 05:15.540
So again,

77
05:15.540 --> 05:20.190
let's create a function and I'll call it is_night.

78
05:21.330 --> 05:26.190
And this function is going to return true when it is actually nighttime. In

79
05:26.190 --> 05:28.290
order to figure out whether if it is nighttime,

80
05:28.890 --> 05:33.870
then we're going to get a response - so there's a typo there -

81
05:36.210 --> 05:40.020
from our request to the sunrise-sunset API

82
05:40.290 --> 05:44.400
passing in all of these parameters which contain my current location.

83
05:44.970 --> 05:46.860
And then from the data we get back,

84
05:46.890 --> 05:51.330
we figure out the sunrise and sunset time by formatting that string

85
05:51.600 --> 05:53.820
and then turning it into an integer

86
05:54.090 --> 05:59.090
which is going to represent the current hour in the 24-hour time format.

87
05:59.870 --> 06:01.670
So then we figure out, well,

88
06:01.670 --> 06:06.200
what is the current time using the datetime module?

89
06:06.680 --> 06:09.080
And we can check to see, well,

90
06:09.350 --> 06:14.350
if the time now is greater than or equal to the sunset time,

91
06:14.900 --> 06:18.470
or if the time now is less than or equal to the sunrise time,

92
06:18.770 --> 06:20.420
then it means it's dark.

93
06:22.550 --> 06:27.550
So now we're getting a warning telling us that this time now is a datetime

94
06:28.280 --> 06:33.080
object, but the sunset and sunrise are whole numbers or integers.

95
06:33.680 --> 06:37.340
So what's happened well? Datetime now is, in fact,

96
06:37.370 --> 06:38.960
the current date and time,

97
06:39.320 --> 06:44.320
but what we actually want is the current hour and that converts it into an

98
06:44.360 --> 06:46.670
integer making this comparison valid.

99
06:47.270 --> 06:50.360
So if it is currently nighttime,

100
06:50.600 --> 06:55.370
then we're going to return true. And if the ISS is overhead,

101
06:55.430 --> 07:00.290
then we're also going to return true. So now we've done these two parts,

102
07:00.410 --> 07:04.070
all that's left to do is once both of these things are true

103
07:04.430 --> 07:08.060
we can send ourselves an email to tell us to look up.

104
07:08.660 --> 07:13.660
So if the ISS is overhead and it is currently nighttime,

105
07:15.260 --> 07:18.020
then we're going to initiate our email sending

106
07:18.110 --> 07:21.020
which is going to require our smtplib.

107
07:21.590 --> 07:25.850
And I'm also going to add my email and password as constants up here.

108
07:27.950 --> 07:30.230
And now we can create our connection

109
07:33.050 --> 07:35.630
and I'm using again a Gmail server.

110
07:35.660 --> 07:40.340
So it's going to be smtp.gmail.com.

111
07:40.790 --> 07:44.450
So we went through all of this when we talked about email in detail.

112
07:44.720 --> 07:46.850
So I won't waste time talking about it again,

113
07:47.680 --> 07:48.513
<v 2>right?</v>

114
07:50.080 --> 07:55.080
<v 0>I'm going to send this email from my own address and also to my own address</v>

115
07:55.780 --> 08:00.780
because after all I'm just making a notification for myself. And the message in this

116
08:02.080 --> 08:07.080
case is going to have the subject line of look up and I can even add an emoji

117
08:09.910 --> 08:10.743
here.

118
08:12.970 --> 08:16.570
And then the content is going to say

119
08:16.570 --> 08:20.740
The ISS is above you in the sky.

120
08:22.780 --> 08:25.690
That's it. That's all there is to this. Now,

121
08:25.720 --> 08:30.220
it'd be quite hard to test this code because we have to wait for the perfect

122
08:30.250 --> 08:35.250
condition before all of these things will actually be true. While this is the end

123
08:36.190 --> 08:37.600
of the challenge for you,

124
08:37.900 --> 08:41.380
there's one thing I want to change with this code to make it a little bit

125
08:41.380 --> 08:42.213
better.

126
08:42.430 --> 08:46.450
We can actually put this if statement inside a while loop.

127
08:46.900 --> 08:50.380
So we could say while true,

128
08:50.440 --> 08:53.860
which means this loop is going to go again and again, and again,

129
08:53.890 --> 08:56.730
checking if the ISS is overhead and it's at night.

130
08:57.090 --> 09:00.750
Now this is going to happen quite frequently because its in a while loop,

131
09:01.050 --> 09:04.260
but we can slow it down by using the time module.

132
09:04.590 --> 09:09.590
So we could import the time module and then say time.sleep

133
09:11.130 --> 09:11.630
and we can get

134
09:11.630 --> 09:16.630
it to sleep for 60 seconds or however much long you want to wait between

135
09:17.480 --> 09:20.990
running the script. So now if you hit run,

136
09:21.740 --> 09:25.850
you won't see that final code where it tells you that your program is done

137
09:25.850 --> 09:26.683
running.

138
09:26.780 --> 09:31.400
It's actually continuously running in the background and it's going to execute

139
09:31.400 --> 09:33.800
this code every 60 seconds.

140
09:34.160 --> 09:37.250
So as long as you have your computer on and this is running,

141
09:37.610 --> 09:41.180
then you can run this for a whole 24 hours. And I'll bet you

142
09:41.180 --> 09:42.290
at some point in the night,

143
09:42.320 --> 09:46.070
you'll get two or three emails telling you that the ISS is overhead.

144
09:46.280 --> 09:50.600
So all you have to monitor is just to switch your notifications on on your phone

145
09:50.990 --> 09:54.530
and when that email comes in to look up into the sky.

146
09:55.850 --> 09:58.760
So I hope you enjoyed building this project with me

147
09:58.880 --> 10:02.240
and I hope that you're going to be able to spot the ISS tonight.

148
10:02.900 --> 10:05.930
If you do, take a video and let us know what it looks like.