1
00:00:00,180 --> 00:00:02,550
Instructor: So previously we've managed to check

2
00:00:02,550 --> 00:00:04,800
the stock price difference between the day

3
00:00:04,800 --> 00:00:07,020
before yesterday and yesterday.

4
00:00:07,020 --> 00:00:09,600
And then based on when that different

5
00:00:09,600 --> 00:00:11,820
is over a certain threshold,

6
00:00:11,820 --> 00:00:14,130
then we trigger this bunch of code

7
00:00:14,130 --> 00:00:16,350
which actually fetches some news

8
00:00:16,350 --> 00:00:19,650
from the news API regarding that company.

9
00:00:19,650 --> 00:00:22,740
So now that we've got the top three articles

10
00:00:22,740 --> 00:00:26,220
from the news API, now we're on to step three

11
00:00:26,220 --> 00:00:30,240
which is to use Twilio to send a message

12
00:00:30,240 --> 00:00:32,340
with each article's title and description

13
00:00:32,340 --> 00:00:33,870
to our phone number.

14
00:00:33,870 --> 00:00:36,152
The first step is to create a list

15
00:00:36,152 --> 00:00:40,560
of the first three articles headline and description

16
00:00:40,560 --> 00:00:42,840
using list comprehension.

17
00:00:42,840 --> 00:00:45,840
So what we're aiming for is a single string

18
00:00:45,840 --> 00:00:49,500
that says something like this, Headline:,

19
00:00:49,500 --> 00:00:54,500
and then it's the actual article title.

20
00:00:54,630 --> 00:00:57,870
And then we've got our Brief

21
00:00:57,870 --> 00:01:00,180
which is gonna go on a new line,

22
00:01:00,180 --> 00:01:04,203
and then this is going to be the article description.

23
00:01:05,280 --> 00:01:10,050
You can see those parts in the articles that we printed out.

24
00:01:10,050 --> 00:01:12,540
For example, this first article here.

25
00:01:12,540 --> 00:01:16,560
You can see the title says Spartan Energy surges

26
00:01:16,560 --> 00:01:19,770
after something, something, something, something.

27
00:01:19,770 --> 00:01:21,960
You can see this is the first title

28
00:01:21,960 --> 00:01:24,510
and this is the first description.

29
00:01:24,510 --> 00:01:28,230
So it's basically the title of the article

30
00:01:28,230 --> 00:01:32,970
and also a brief description of what the article is about.

31
00:01:32,970 --> 00:01:35,700
That's what we want to get message to us.

32
00:01:35,700 --> 00:01:39,030
Now in order to use list comprehension,

33
00:01:39,030 --> 00:01:42,450
we first use the keyword method.

34
00:01:42,450 --> 00:01:46,620
So new_item for item in list.

35
00:01:46,620 --> 00:01:50,910
Now our list in this case is of course our three articles,

36
00:01:50,910 --> 00:01:55,590
and each of those items will be an article in themselves.

37
00:01:55,590 --> 00:02:00,120
And for each of those articles, we want to have this format.

38
00:02:00,120 --> 00:02:03,960
So I'm just going to cut that and then paste it in here.

39
00:02:03,960 --> 00:02:05,760
And then I'm gonna use an f string

40
00:02:05,760 --> 00:02:09,210
in order to insert these relevant parts.

41
00:02:09,210 --> 00:02:13,050
So the article title is stored under the title key

42
00:02:13,050 --> 00:02:16,440
and the description is stored under the description key.

43
00:02:16,440 --> 00:02:20,280
So we can simply use that as the key

44
00:02:20,280 --> 00:02:22,950
to tap into this article.

45
00:02:22,950 --> 00:02:26,370
Notice how we've got a set of double quotes

46
00:02:26,370 --> 00:02:28,860
outside to create our string.

47
00:02:28,860 --> 00:02:30,690
If we have another set of double quotes

48
00:02:30,690 --> 00:02:32,490
to specify the key,

49
00:02:32,490 --> 00:02:34,620
this is going to be a bit confusing

50
00:02:34,620 --> 00:02:36,210
for the code interpreter.

51
00:02:36,210 --> 00:02:39,630
So let's change that to single quotes instead

52
00:02:39,630 --> 00:02:43,110
to make it actually work, like this.

53
00:02:43,110 --> 00:02:46,380
Now next is the article description.

54
00:02:46,380 --> 00:02:49,470
So this is gonna go inside another set of quotes

55
00:02:49,470 --> 00:02:51,268
and also it's gonna go inside

56
00:02:51,268 --> 00:02:55,263
a set of square brackets, like this.

57
00:02:57,120 --> 00:03:00,270
Each of the new items that's gonna go into this new list

58
00:03:00,270 --> 00:03:02,430
is going to be a string

59
00:03:02,430 --> 00:03:04,500
that's comprised of the article title

60
00:03:04,500 --> 00:03:06,810
and also the article description.

61
00:03:06,810 --> 00:03:09,030
Now we can save this new list

62
00:03:09,030 --> 00:03:14,030
into a formatted_articles list, like this.

63
00:03:15,690 --> 00:03:18,270
So that's TODO eight completed.

64
00:03:18,270 --> 00:03:21,120
And now we're gonna try and send each article

65
00:03:21,120 --> 00:03:23,523
as a separate message via Twilio.

66
00:03:24,360 --> 00:03:27,360
Here's our Twilio SMS Python Quickstart,

67
00:03:27,360 --> 00:03:30,510
and we're basically going to replicate all of this.

68
00:03:30,510 --> 00:03:34,920
So first we're gonna import the Client class

69
00:03:34,920 --> 00:03:36,393
from the Twilio library.

70
00:03:39,210 --> 00:03:41,880
And make sure that we actually have this installed

71
00:03:41,880 --> 00:03:43,450
if it's not already installed

72
00:03:44,550 --> 00:03:47,340
and get rid of the red underlines.

73
00:03:47,340 --> 00:03:50,010
Next we're going to get our account SID

74
00:03:50,010 --> 00:03:52,443
and auth token from Twilio.

75
00:03:53,340 --> 00:03:58,340
So I'm gonna copy my account SID and put that over here,

76
00:04:03,390 --> 00:04:06,633
and also get my auth token from here as well.

77
00:04:08,280 --> 00:04:10,890
So now I can set up my client

78
00:04:10,890 --> 00:04:13,380
by creating it from the Client class.

79
00:04:13,380 --> 00:04:16,500
Down here at the point where I want to send my message

80
00:04:16,500 --> 00:04:18,360
I'm going to create a new client

81
00:04:18,360 --> 00:04:20,519
from the Twilio Client class.

82
00:04:20,519 --> 00:04:22,590
And this is going to be comprised

83
00:04:22,590 --> 00:04:27,590
of my Twilio account SID and also my Twilio auth token.

84
00:04:28,980 --> 00:04:31,230
Finally, we can create our message

85
00:04:31,230 --> 00:04:34,413
and send it to our own number.

86
00:04:37,620 --> 00:04:42,060
The body of the message is going to be each of the articles.

87
00:04:42,060 --> 00:04:44,970
So in order to send three messages,

88
00:04:44,970 --> 00:04:49,970
we can loop through our three formatted_articles like this

89
00:04:50,100 --> 00:04:52,320
and then we can create a message

90
00:04:52,320 --> 00:04:55,410
and send each of those one by one.

91
00:04:55,410 --> 00:04:58,440
So the body is going to be each article

92
00:04:58,440 --> 00:05:01,350
in the formatted_articles.

93
00:05:01,350 --> 00:05:05,700
Now the from number is going to be our Twilio virtual number

94
00:05:05,700 --> 00:05:09,813
and our to number is going to be our actual phone number.

95
00:05:10,770 --> 00:05:12,150
Once we've done all of that

96
00:05:12,150 --> 00:05:17,040
we can now test this and run it to see if it actually works.

97
00:05:17,040 --> 00:05:20,070
Now I'm gonna go ahead and hit run,

98
00:05:20,070 --> 00:05:22,683
and hopefully our messages will get sent.

99
00:05:24,780 --> 00:05:26,850
As you can see right now.

100
00:05:26,850 --> 00:05:29,190
So we've got our headline and our brief

101
00:05:29,190 --> 00:05:30,720
and each of the three articles

102
00:05:30,720 --> 00:05:34,230
are being sent as a separate message.

103
00:05:34,230 --> 00:05:37,140
That's pretty much the end of this challenge.

104
00:05:37,140 --> 00:05:40,620
Now you can of course, improve this even further.

105
00:05:40,620 --> 00:05:42,150
And one of the things I thought about

106
00:05:42,150 --> 00:05:44,610
is having a little emoji

107
00:05:44,610 --> 00:05:47,400
to show whether if your stock is up or down

108
00:05:47,400 --> 00:05:51,210
and also showing a rounded percentage.

109
00:05:51,210 --> 00:05:52,800
To do that, we need to figure out

110
00:05:52,800 --> 00:05:55,380
whether if the stock was up or down

111
00:05:55,380 --> 00:05:59,130
and we can work that out by looking at the difference.

112
00:05:59,130 --> 00:06:01,501
If instead of using the abs here,

113
00:06:01,501 --> 00:06:05,460
which is gonna get rid of our negative and positive,

114
00:06:05,460 --> 00:06:08,070
if we had instead use an if statement

115
00:06:08,070 --> 00:06:11,580
to check if the difference is greater than zero,

116
00:06:11,580 --> 00:06:12,960
so it's positive,

117
00:06:12,960 --> 00:06:15,720
then we can create a variable called up_down,

118
00:06:15,720 --> 00:06:18,060
which starts out as none;

119
00:06:18,060 --> 00:06:19,260
but if it's positive,

120
00:06:19,260 --> 00:06:23,163
then we can turn that into a up emoji.

121
00:06:26,280 --> 00:06:27,930
And remember, emojis are simply

122
00:06:27,930 --> 00:06:31,170
just treated as strings in our code.

123
00:06:31,170 --> 00:06:33,330
Otherwise, if it's below zero,

124
00:06:33,330 --> 00:06:36,243
then we'll change that to a down emoji,

125
00:06:42,840 --> 00:06:43,950
like this.

126
00:06:43,950 --> 00:06:45,660
And as always, you can always search

127
00:06:45,660 --> 00:06:48,750
for these emojis in Google and copy and paste them in

128
00:06:48,750 --> 00:06:52,353
if you don't have the emoji and symbols keyboard.

129
00:06:53,310 --> 00:06:57,600
But having taken away that absolute value function here,

130
00:06:57,600 --> 00:06:59,970
we're going to have to put it back somewhere else.

131
00:06:59,970 --> 00:07:01,860
So we're gonna put it right here

132
00:07:01,860 --> 00:07:05,163
at the point where we do the if checking.

133
00:07:07,530 --> 00:07:11,910
And in addition, I want to change this diff_percent

134
00:07:11,910 --> 00:07:15,183
so that we actually around it to the nearest whole number.

135
00:07:16,283 --> 00:07:20,520
With the rounded diff_percent and also this up_down,

136
00:07:20,520 --> 00:07:22,440
we can now format our message

137
00:07:22,440 --> 00:07:26,290
so that it says the name of the stock

138
00:07:29,280 --> 00:07:31,870
and then whether it fits up or down

139
00:07:33,390 --> 00:07:35,163
and then the diff_percent,

140
00:07:36,870 --> 00:07:40,923
and finally a percentage sign and also a new line.

141
00:07:41,880 --> 00:07:43,863
So now if I run this again,

142
00:07:46,320 --> 00:07:50,460
you can see that this time these messages come complete

143
00:07:50,460 --> 00:07:54,150
with the stock name, the movement percentage,

144
00:07:54,150 --> 00:07:56,133
the headlines, and the brief.

145
00:07:57,240 --> 00:07:58,770
So there you have it.

146
00:07:58,770 --> 00:07:59,760
I'm sure you can think

147
00:07:59,760 --> 00:08:03,180
of even more improvements to this program.

148
00:08:03,180 --> 00:08:05,640
And especially if you're somebody who actually trade stocks,

149
00:08:05,640 --> 00:08:07,680
then I'm sure you'll think of even more ways

150
00:08:07,680 --> 00:08:09,570
of improving this project.

151
00:08:09,570 --> 00:08:11,850
If you come up with anything interesting and fun,

152
00:08:11,850 --> 00:08:14,640
be sure to share it with the rest of us in the Q&A

153
00:08:14,640 --> 00:08:16,953
so that we can all admire your hard work.

