WEBVTT

0
00:00.180 --> 00:02.550
So previously we've managed to check

1
00:02.550 --> 00:04.800
the stock price difference between the day

2
00:04.800 --> 00:07.020
before yesterday and yesterday.

3
00:07.020 --> 00:09.600
And then based on when that different

4
00:09.600 --> 00:11.820
is over a certain threshold,

5
00:11.820 --> 00:14.130
then we trigger this bunch of code

6
00:14.130 --> 00:16.350
which actually fetches some news

7
00:16.350 --> 00:19.650
from the news API regarding that company.

8
00:19.650 --> 00:22.740
So now that we've got the top three_articles

9
00:22.740 --> 00:26.220
from the news API, now we're on to Step 3,

10
00:26.220 --> 00:30.240
which is to use Twilio to send a message

11
00:30.240 --> 00:32.340
with each article's title and description

12
00:32.340 --> 00:33.870
to our phone number.

13
00:33.870 --> 00:36.152
The first step is to create a list

14
00:36.152 --> 00:40.560
of the first three articles headline and description

15
00:40.560 --> 00:42.840
using list comprehension.

16
00:42.840 --> 00:45.840
So what we're aiming for is a single string

17
00:45.840 --> 00:49.500
that says something like this, Headline:

18
00:49.500 --> 00:54.500
and then it's the actual {article title}.

19
00:54.630 --> 00:57.870
And then we've got our \nBrief:

20
00:57.870 --> 01:00.180
which is going to go on a new line,

21
01:00.180 --> 01:04.203
and then this is going to be the {article description}.

22
01:05.280 --> 01:10.050
You can see those parts in the articles that we printed out.

23
01:10.050 --> 01:12.540
For example, this first article here.

24
01:12.540 --> 01:16.560
You can see the title says Spartan Energy surges

25
01:16.560 --> 01:19.770
after something, something, something, something.

26
01:19.770 --> 01:21.960
You can see this is the first title

27
01:21.960 --> 01:24.510
and this is the first description.

28
01:24.510 --> 01:28.230
So it's basically the title of the article

29
01:28.230 --> 01:32.970
and also a brief description of what the article is about.

30
01:32.970 --> 01:35.700
That's what we want to get messaged to us.

31
01:35.700 --> 01:39.030
Now in order to use list comprehension,

32
01:39.030 --> 01:42.450
we first use the keyword method.

33
01:42.450 --> 01:46.620
So, [new_item for item in list].

34
01:46.620 --> 01:50.910
Now our list in this case is of course our three_articles,

35
01:50.910 --> 01:55.590
and each of those items will be an article in themselves,

36
01:55.590 --> 02:00.120
and for each of those articles, we want to have this format.

37
02:00.120 --> 02:03.960
So I'm just going to cut that and then paste it in here.

38
02:03.960 --> 02:05.760
And then I'm going to use an f-string

39
02:05.760 --> 02:09.210
in order to insert these relevant parts.

40
02:09.210 --> 02:13.050
So the article title is stored under the title key

41
02:13.050 --> 02:16.440
and the description is stored under the description key.

42
02:16.440 --> 02:20.280
So we can simply use that as the key

43
02:20.280 --> 02:22.950
to tap into this article.

44
02:22.950 --> 02:26.370
Notice how we've got a set of double quotes

45
02:26.370 --> 02:28.860
outside to create our string,

46
02:28.860 --> 02:30.690
if we have another set of double quotes

47
02:30.690 --> 02:32.490
to specify the key,

48
02:32.490 --> 02:34.620
this is going to be a bit confusing

49
02:34.620 --> 02:36.210
for the code interpreter.

50
02:36.210 --> 02:39.630
So let's change that to single quotes instead

51
02:39.630 --> 02:43.110
to make it actually work, like this.

52
02:43.110 --> 02:46.380
Now next is the article description.

53
02:46.380 --> 02:49.470
So this is going go inside another set of quotes

54
02:49.470 --> 02:51.268
and also it's going to go inside

55
02:51.268 --> 02:55.263
a set of square brackets, like this.

56
02:57.120 --> 03:00.270
Each of the new items that's going to go into this new list

57
03:00.270 --> 03:02.430
is going to be a string

58
03:02.430 --> 03:04.500
that's comprised of the article['title']

59
03:04.500 --> 03:06.810
and also the article['description'].

60
03:06.810 --> 03:09.030
So now we can save this new list

61
03:09.030 --> 03:14.030
into a formatted_articles list, like this.

62
03:15.690 --> 03:18.270
So that's TODO 8 completed,

63
03:18.270 --> 03:21.120
and now we're going to try and send each article

64
03:21.120 --> 03:23.523
as a separate message via Twilio.

65
03:24.360 --> 03:27.360
Here's our Twilio SMS Python Quickstart,

66
03:27.360 --> 03:30.510
and we're basically going to replicate all of this.

67
03:30.510 --> 03:34.920
So first we're going to import the Client class

68
03:34.920 --> 03:36.393
from the Twilio library.

69
03:39.210 --> 03:41.880
And make sure that we actually have this installed

70
03:41.880 --> 03:43.450
if it's not already installed

71
03:44.550 --> 03:47.340
and get rid of the red underlines.

72
03:47.340 --> 03:50.010
Next we're going to get our ACCOUNT SID

73
03:50.010 --> 03:52.443
and AUTH TOKEN from Twilio.

74
03:53.340 --> 03:58.340
So I'm going to copy my account SID and put that over here,

75
04:03.390 --> 04:06.633
and also get my AUTH_TOKEN from here as well.

76
04:08.280 --> 04:10.890
So now I can set up my client

77
04:10.890 --> 04:13.380
by creating it from the Client class.

78
04:13.380 --> 04:16.500
Down here at the point where I want to send my message

79
04:16.500 --> 04:18.360
I'm going to create a new client

80
04:18.360 --> 04:20.520
from the Twilio Client class.

81
04:20.520 --> 04:22.590
And this is going to be comprised

82
04:22.590 --> 04:27.590
of my TWILIO account SID and also my TWILIO_AUTH_TOKEN.

83
04:28.980 --> 04:31.230
Finally, we can create our message

84
04:31.230 --> 04:34.413
and send it to our own number.

85
04:37.620 --> 04:42.060
The body of the message is going to be each of the articles.

86
04:42.060 --> 04:44.970
So in order to send three messages,

87
04:44.970 --> 04:49.970
we can loop through our three formatted_articles like this,

88
04:50.100 --> 04:52.320
and then we can create a message

89
04:52.320 --> 04:55.410
and send each of those one by one.

90
04:55.410 --> 04:58.440
So the body is going to be each article

91
04:58.440 --> 05:01.350
in the formatted_articles.

92
05:01.350 --> 05:05.700
Now the "from" number is going to be our Twilio virtual number

93
05:05.700 --> 05:09.813
and our "to" number is going to be our actual phone number.

94
05:10.770 --> 05:12.150
Once we've done all of that

95
05:12.150 --> 05:17.040
we can now test this and run it to see if it actually works.

96
05:17.040 --> 05:20.070
Now I'm going to go ahead and hit Run,

97
05:20.070 --> 05:22.683
and hopefully our messages will get sent

98
05:24.780 --> 05:26.850
as you can see right now.

99
05:26.850 --> 05:29.190
So we've got our headline and our brief

100
05:29.190 --> 05:30.720
and each of the three articles

101
05:30.720 --> 05:34.230
are being sent as a separate message.

102
05:34.230 --> 05:37.140
That's pretty much the end of this challenge.

103
05:37.140 --> 05:40.620
Now you can of course, improve this even further.

104
05:40.620 --> 05:42.150
And one of the things I thought about

105
05:42.150 --> 05:44.610
is having a little emoji

106
05:44.610 --> 05:47.400
to show whether if your stock is up or down

107
05:47.400 --> 05:51.210
and also showing a rounded percentage.

108
05:51.210 --> 05:52.800
To do that, we need to figure out

109
05:52.800 --> 05:55.380
whether if the stock was up or down

110
05:55.380 --> 05:59.130
and we can work that out by looking at the difference.

111
05:59.130 --> 06:01.501
If instead of using the abs here,

112
06:01.501 --> 06:05.460
which is going to get rid of our negative and positive,

113
06:05.460 --> 06:08.070
if we had instead use an if-statement

114
06:08.070 --> 06:11.580
to check if the difference is greater than zero,

115
06:11.580 --> 06:12.960
so it's positive,

116
06:12.960 --> 06:15.720
then we can create a variable called up_down,

117
06:15.720 --> 06:18.060
which starts out as None;

118
06:18.060 --> 06:19.260
but if it's positive,

119
06:19.260 --> 06:23.163
then we can turn that into an up emoji ðŸ”º.

120
06:26.280 --> 06:27.930
And remember, emojis are simply

121
06:27.930 --> 06:31.170
just treated as strings in our code.

122
06:31.170 --> 06:33.330
Otherwise, if it's below zero,

123
06:33.330 --> 06:36.243
then we'll change that to a down emoji ðŸ”»,

124
06:42.840 --> 06:43.950
like this.

125
06:43.950 --> 06:45.660
And as always, you can always search

126
06:45.660 --> 06:48.750
for these emojis in Google and copy and paste them in

127
06:48.750 --> 06:52.353
if you don't have the emoji and symbols keyboard.

128
06:53.310 --> 06:57.600
But having taken away that absolute value function here,

129
06:57.600 --> 06:59.970
we're going to have to put it back somewhere else.

130
06:59.970 --> 07:01.860
So we're going to put it right here

131
07:01.860 --> 07:05.163
at the point where we do the if checking.

132
07:07.530 --> 07:11.910
And in addition, I want to change this diff_percent

133
07:11.910 --> 07:15.183
so that we actually around it to the nearest whole number.

134
07:16.283 --> 07:20.520
With the rounded diff_percent and also this up_down,

135
07:20.520 --> 07:22.440
we can now format our message

136
07:22.440 --> 07:26.290
so that it says the name of the stock

137
07:29.280 --> 07:31.870
and then whether if it's up or down

138
07:33.390 --> 07:35.163
and then the diff_percent,

139
07:36.870 --> 07:40.923
and finally a percentage sign and also a new line.

140
07:41.880 --> 07:43.863
So now if I run this again,

141
07:46.320 --> 07:50.460
you can see that this time these messages come complete

142
07:50.460 --> 07:54.150
with the stock name, the movement percentage,

143
07:54.150 --> 07:56.133
the headlines, and the brief.

144
07:57.240 --> 07:58.770
So there you have it.

145
07:58.770 --> 07:59.760
I'm sure you can think

146
07:59.760 --> 08:03.180
of even more improvements to this program,

147
08:03.180 --> 08:05.640
and especially if you're somebody who actually trades stocks,

148
08:05.640 --> 08:07.680
then I'm sure you'll think of even more ways

149
08:07.680 --> 08:09.570
of improving this project.

150
08:09.570 --> 08:11.850
If you come up with anything interesting and fun,

151
08:11.850 --> 08:14.640
be sure to share it with the rest of us in the Q&amp;A

152
08:14.640 --> 08:16.953
so that we can all admire your hard work.