1
00:00:00,030 --> 00:00:01,800
Now that we've got our app working,

2
00:00:02,040 --> 00:00:04,770
you might think we've reached the end of today's lessons,

3
00:00:05,070 --> 00:00:08,010
but there's just one final thing I want to talk to you about.

4
00:00:08,550 --> 00:00:12,210
And that is the environment. Not the environment as a whole,

5
00:00:12,330 --> 00:00:15,510
but more precisely environment variables.

6
00:00:16,260 --> 00:00:18,510
Now what our environment variables anyways?

7
00:00:18,870 --> 00:00:20,760
If you go into your PyCharm project

8
00:00:20,880 --> 00:00:24,780
and you go into this tab where it says "Terminal", here

9
00:00:24,810 --> 00:00:27,840
if you type in env (set on Windows) and hit "Enter",

10
00:00:28,170 --> 00:00:31,020
you can see a whole bunch of variables.

11
00:00:31,050 --> 00:00:35,250
So you've got a key and a value

12
00:00:35,760 --> 00:00:39,270
and in between there is just a single equal (=) sign.

13
00:00:39,960 --> 00:00:44,790
So these are the different variables that are set in the environment in which

14
00:00:44,820 --> 00:00:45,720
your code is run.

15
00:00:46,350 --> 00:00:49,980
These variables have values which are strings

16
00:00:50,400 --> 00:00:54,300
which can be used in our applications or our code.

17
00:00:55,230 --> 00:00:56,490
Now inside Python

18
00:00:56,490 --> 00:01:00,750
anywhere you can also type env and you can see some slightly different

19
00:01:00,750 --> 00:01:05,099
environment variables which are relative to the Python anywhere environment.

20
00:01:05,580 --> 00:01:09,660
What exactly all these environment variables used for? Well,

21
00:01:09,840 --> 00:01:14,370
there's two major use cases. One is for convenience.

22
00:01:14,970 --> 00:01:18,690
Normally when you deploy a large application,

23
00:01:19,140 --> 00:01:23,340
the process is quite complicated. And once you've done it,

24
00:01:23,670 --> 00:01:28,230
you kind of don't want to mess around with the code base and update the code

25
00:01:28,230 --> 00:01:31,890
files like your main.py for example. Instead,

26
00:01:31,920 --> 00:01:36,450
you could have these environment variables, which you can change. For example,

27
00:01:36,510 --> 00:01:40,260
if you had an application that was sending you emails out to your clients,

28
00:01:40,500 --> 00:01:43,500
then your client base emails might change day to day.

29
00:01:44,070 --> 00:01:48,840
So certain variables that are being used in your code base could be set as

30
00:01:48,840 --> 00:01:49,980
environment variables

31
00:01:50,220 --> 00:01:53,640
and you can modify those variables without having to touch the code.

32
00:01:54,510 --> 00:01:57,480
A second reason might be for security.

33
00:01:58,140 --> 00:01:59,700
So when you're developing software,

34
00:01:59,730 --> 00:02:04,110
you might be uploading your code based somewhere, for example

35
00:02:04,110 --> 00:02:07,590
to store it online or to a service like PythonAnywhere.

36
00:02:08,520 --> 00:02:13,440
And it's usually not a good idea to have things like your authentication keys or

37
00:02:13,440 --> 00:02:18,440
your API keys to be stored in the same place as the rest of your code.

38
00:02:19,140 --> 00:02:21,180
That's where environment variables come in.

39
00:02:21,420 --> 00:02:26,420
So environment variables essentially allow us to separate out where we store

40
00:02:26,490 --> 00:02:28,590
our keys, our secret stuff,

41
00:02:28,800 --> 00:02:33,480
and various other variables away from where our code base is located.

42
00:02:34,140 --> 00:02:34,973
In our case,

43
00:02:35,010 --> 00:02:39,270
it doesn't really matter because we haven't got a paid account anywhere in here.

44
00:02:39,750 --> 00:02:44,370
It doesn't really matter if somebody steals our auth token or our API key,

45
00:02:44,490 --> 00:02:49,170
because none of those are linked to our payment details. Now, however,

46
00:02:49,170 --> 00:02:53,130
if we were to upgrade our accounts on open weather map's API

47
00:02:53,400 --> 00:02:54,900
or on Twilio's API,

48
00:02:55,200 --> 00:03:00,200
then we definitely want to keep these two things secret; auth token and the API

49
00:03:00,790 --> 00:03:01,623
key.

50
00:03:01,810 --> 00:03:05,860
Instead of having it located in the same place where we've got our code base

51
00:03:06,310 --> 00:03:10,210
which means you might accidentally upload it somewhere on the internet where

52
00:03:10,210 --> 00:03:12,610
other people can see it, instead,

53
00:03:12,640 --> 00:03:15,910
we can store these two things as environment variables.

54
00:03:16,600 --> 00:03:21,340
We can create an environment variable by simply typing export

55
00:03:21,760 --> 00:03:26,050
and then the name of the variable which I'll call OWM

56
00:03:26,410 --> 00:03:31,270
_API_KEY. And then it's really important that we have no spaces,

57
00:03:31,480 --> 00:03:33,640
but just a single equal sign.

58
00:03:34,360 --> 00:03:37,720
And then we're going to store everything that's in between the quotation marks.

59
00:03:38,020 --> 00:03:39,880
So I'm going to copy that from over here,

60
00:03:40,900 --> 00:03:45,430
and I'm going to paste that in here. Once I've exported that

61
00:03:46,180 --> 00:03:49,330
and then if I hit "env" again, you'll see

62
00:03:49,330 --> 00:03:51,370
now in this updated environment

63
00:03:51,430 --> 00:03:55,090
you can see that environment variable go in right there.

64
00:03:55,510 --> 00:04:00,100
And now we can tap into that environment variable in any of the code that we run

65
00:04:00,160 --> 00:04:05,160
from this particular environment. To do that we have to use this os module that

66
00:04:05,740 --> 00:04:06,573
we've already got.

67
00:04:06,940 --> 00:04:10,780
So I'm going to delete everything that's currently stored in the API key.

68
00:04:11,230 --> 00:04:15,250
And instead, I'm going to tap into os.environ,

69
00:04:16,029 --> 00:04:21,029
and then I'm going to use a method called get to get the value of a particular

70
00:04:21,070 --> 00:04:22,060
environment variable.

71
00:04:22,960 --> 00:04:26,500
The name is everything that's before the equal sign.

72
00:04:26,980 --> 00:04:31,960
So let's go ahead and paste it in here. And now when I run this code,

73
00:04:32,320 --> 00:04:36,010
so using python3 and then the name of the file,

74
00:04:36,760 --> 00:04:41,470
you can see that it successfully runs queuing our message into Twilio.

75
00:04:42,250 --> 00:04:45,730
Now go ahead and do the same thing with the auth token.

76
00:04:46,360 --> 00:04:48,520
You want to save this as an environment

77
00:04:48,520 --> 00:04:52,510
variable code auth_token in all caps,

78
00:04:52,900 --> 00:04:54,430
similar to what we've got here.

79
00:04:55,060 --> 00:04:58,390
And you want to store this value in the environment variable.

80
00:04:58,630 --> 00:05:02,850
Pause the video and give that a go. All right.

81
00:05:02,880 --> 00:05:06,090
So we're going to follow the exact same thing that we did before. We use the

82
00:05:06,090 --> 00:05:10,920
export keyword and then we type in the name of the environment variable

83
00:05:11,190 --> 00:05:15,420
which is going to be AUTH_TOKEN. And then really importantly,

84
00:05:15,450 --> 00:05:17,670
we just have an equal sign, no spaces,

85
00:05:18,090 --> 00:05:21,840
and we're going to store this value without the double-quotes

86
00:05:22,440 --> 00:05:26,970
and we're going to put it in here. Now in our environment variables,

87
00:05:27,000 --> 00:05:30,120
you can see we've got our open weather map API key.

88
00:05:30,540 --> 00:05:35,280
And if we scroll up a bit more, you can see our auth token as well.

89
00:05:36,750 --> 00:05:41,750
So now we can replace this string with our os.environ.get

90
00:05:43,560 --> 00:05:47,580
and then we pass in the name of the key which is AUTH_TOKEN.

91
00:05:48,630 --> 00:05:53,630
And now let's run our code again with python3 main.py.

92
00:05:54,090 --> 00:05:56,580
And you can see it's still works as before.

93
00:05:57,320 --> 00:06:01,730
But now if somebody comes across this particular code base on the internet,

94
00:06:02,030 --> 00:06:07,030
they won't be able to use our Twilio account or our open weather map account

95
00:06:07,460 --> 00:06:12,350
because both of these keys are now hidden. Let's go ahead and hit save.

96
00:06:13,760 --> 00:06:15,410
So now we're going to do the same thing,

97
00:06:15,680 --> 00:06:19,340
but at the point where we run our code from our task scheduler.

98
00:06:19,880 --> 00:06:24,590
So go ahead and right-click on this snake and then open the link in a new tab so

99
00:06:24,590 --> 00:06:27,470
that we can go to our tasks section.

100
00:06:27,920 --> 00:06:32,480
And here we're going to edit our command. Instead of just running Python main

101
00:06:32,480 --> 00:06:36,740
.py, we're going to export those two environment variables.

102
00:06:37,280 --> 00:06:41,810
This is why we've got two screens open up so we can actually copy the actual

103
00:06:41,900 --> 00:06:46,430
values. The first value is going to be our open weather map

104
00:06:46,460 --> 00:06:49,790
API key. So I'm going to copy that. And then here,

105
00:06:49,820 --> 00:06:54,470
I'm going to write export and then paste in that first key. Now,

106
00:06:54,500 --> 00:06:58,790
afterward, I'm going to use a semi-colon to denote a new line

107
00:06:59,210 --> 00:07:03,140
and then I'm going to export the next key. Scrolling up,

108
00:07:03,170 --> 00:07:07,220
we can see our auth token right here so we're going to copy that as well

109
00:07:07,550 --> 00:07:11,330
and we're going to export that as the other environment variable.

110
00:07:11,780 --> 00:07:14,480
And then finally, we're going to cap it off with a semicolon.

111
00:07:14,930 --> 00:07:18,860
So now you should have your export OWM_API_KEY

112
00:07:19,430 --> 00:07:23,750
and then your exporting your auth token as well. And then finally,

113
00:07:23,750 --> 00:07:26,960
on the last line, we're running our python3 main.py.

114
00:07:27,770 --> 00:07:31,550
Now let's change this to run at the current time.

115
00:07:31,550 --> 00:07:35,690
So it's going to be UTC time which happens to be 10 o'clock

116
00:07:35,720 --> 00:07:38,600
and then instead of 46, it's now 48

117
00:07:38,600 --> 00:07:43,340
so I'm going to change that to 49. And then I'm gonna hit the tick.

118
00:07:44,060 --> 00:07:47,120
And now we're going to wait to see if it actually works

119
00:07:47,330 --> 00:07:51,410
even though we've taken out the API keys from our main.py

120
00:07:51,650 --> 00:07:53,960
because we're exporting it in our command.

121
00:07:54,200 --> 00:07:56,870
Let's see if we still get the notification message.

122
00:07:58,490 --> 00:08:01,310
There you have it. It's still working as before.

123
00:08:02,180 --> 00:08:04,760
But now this is invisible to people

124
00:08:05,180 --> 00:08:07,910
if they happen to access our main.py.

125
00:08:08,510 --> 00:08:12,020
Now don't worry. The service of Python anywhere are pretty secure.

126
00:08:12,050 --> 00:08:14,000
Nobody's really going to hack into your account

127
00:08:14,300 --> 00:08:16,130
just to look into your main.py.

128
00:08:16,760 --> 00:08:20,390
The bigger problem is when you upload your own code to places like Git

129
00:08:20,390 --> 00:08:21,890
Hub or a BitBucket,

130
00:08:22,160 --> 00:08:25,580
basically places that are essentially a Dropbox for code.

131
00:08:26,150 --> 00:08:29,120
And when you have it on there, because your code could be public,

132
00:08:29,420 --> 00:08:31,280
that's where the problem happens.

133
00:08:31,580 --> 00:08:35,659
If people look at your code file and they see that you've got API keys in there,

134
00:08:35,659 --> 00:08:40,130
then they might steal them. So whenever you're uploading your code publicly,

135
00:08:40,309 --> 00:08:45,310
always be careful that you strip it of all of the API keys and instead use

136
00:08:45,560 --> 00:08:50,450
environment variables like we have here to pull it from the environment.

137
00:08:51,440 --> 00:08:56,070
All that's left to do for you is to update your latitude and longitude to your

138
00:08:56,070 --> 00:09:00,810
actual local latitude and longitude instead of the rainy place that we're

139
00:09:00,810 --> 00:09:05,550
testing. And you've now completed the rain alert application.

140
00:09:06,210 --> 00:09:08,970
Now I've showed you quite a few different APIs,

141
00:09:09,000 --> 00:09:12,300
but there's a whole world of APIs for you to explore.

142
00:09:12,810 --> 00:09:16,680
And if you have the patience to read their documentation and understand how to

143
00:09:16,680 --> 00:09:20,700
work with their APIs, then you now know how to work with their endpoints,

144
00:09:20,940 --> 00:09:25,560
how to pass parameters and also how to authenticate yourself with their servers.

145
00:09:26,100 --> 00:09:31,100
So here's a list of fun APIs that you can use to build applications with.

146
00:09:32,700 --> 00:09:37,700
I've shown you how you can make a rain alert and send yourself SMS messages,

147
00:09:38,520 --> 00:09:43,110
or how to get the location of the ISS or the sunrise and sunset times,

148
00:09:43,410 --> 00:09:48,090
but there's a whole bunch of other APIs for you to explore. For example,

149
00:09:48,090 --> 00:09:50,460
things like the open movie database API,

150
00:09:50,910 --> 00:09:53,970
which contains a whole bunch of movie related data,

151
00:09:54,390 --> 00:09:59,390
or looking up songs and artists using the Spotify API and a whole lot more.

152
00:10:00,840 --> 00:10:02,760
So I'll leave you to explore that.

153
00:10:03,090 --> 00:10:06,000
And if you come up with something innovative and interesting,

154
00:10:06,240 --> 00:10:10,380
be sure to share it with the rest of us in the Q&A below this lesson so that

155
00:10:10,380 --> 00:10:12,810
we can all check it out and admire your hard work.

