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