1 00:00:00,420 --> 00:00:02,310 In the first part of the solution, 2 00:00:02,340 --> 00:00:07,340 we managed to get our code to pull in the live stock data for the selected 3 00:00:08,180 --> 00:00:09,020 stock name. 4 00:00:09,740 --> 00:00:14,270 And then we looked at the difference in price between yesterday and the day 5 00:00:14,270 --> 00:00:18,140 before yesterday and then we worked out the percentage difference. 6 00:00:18,920 --> 00:00:22,760 Now we have this if statement which will print get news 7 00:00:23,090 --> 00:00:27,560 if the percentage difference is greater than a specified amount. 8 00:00:28,130 --> 00:00:31,760 Now, I've put right five here, just because ideally, 9 00:00:31,820 --> 00:00:33,980 if you were monitoring a particular stock, 10 00:00:34,220 --> 00:00:37,880 you'd probably only be interested if it changed by a large amount, 11 00:00:37,880 --> 00:00:42,800 like 5 or 10 or 50% set by you. But at the moment, 12 00:00:42,800 --> 00:00:47,780 because we're testing these things and the Tesla stock is only moved by one 13 00:00:47,780 --> 00:00:51,710 point something percent, then this is actually never going to be true. 14 00:00:51,800 --> 00:00:56,030 So I'm going to manually adjust this down so that we can continue working with 15 00:00:56,030 --> 00:01:00,770 the rest of the challenge. So now onto step two of the challenge, 16 00:01:00,920 --> 00:01:04,459 the first part is instead of printing get news, 17 00:01:04,670 --> 00:01:08,960 we're going to use the news API to get articles related to 18 00:01:08,960 --> 00:01:13,820 the company name. The news API is newsapi.org. 19 00:01:14,540 --> 00:01:17,780 And we can start by getting hold of an API key. 20 00:01:18,590 --> 00:01:21,740 Once you signed up, you should get a free API key 21 00:01:21,860 --> 00:01:25,580 and we're going to paste that into our project. 22 00:01:27,500 --> 00:01:31,550 Now we can start looking through the documentation and seeing how we can use 23 00:01:31,550 --> 00:01:32,990 this news API. 24 00:01:34,010 --> 00:01:39,010 There's a number of end points as we can see. There's top headlines and also 25 00:01:39,170 --> 00:01:41,810 everything. These are the two main end points. 26 00:01:42,470 --> 00:01:46,250 Now everything is better for article discovery 27 00:01:46,700 --> 00:01:50,990 whereas the headlines tend to only have a limited number of headlines. 28 00:01:51,530 --> 00:01:53,300 I've tested both of these 29 00:01:53,360 --> 00:01:58,360 and I found that the everything one is more likely to give us a news piece for 30 00:01:58,670 --> 00:02:03,260 stocks that we're looking for. Because very often a lot of these companies and 31 00:02:03,260 --> 00:02:06,530 their stocks don't actually make it to the day's headlines. 32 00:02:07,190 --> 00:02:12,170 So this is what the query looks like, this is the end point, 33 00:02:12,230 --> 00:02:16,730 and these are the parameters. Now, if we scroll down, 34 00:02:16,760 --> 00:02:20,330 we can see a number of request parameters; q 35 00:02:20,330 --> 00:02:25,330 which is the keyword or phrase the same in the article title and body, qIn 36 00:02:25,760 --> 00:02:29,210 Title is to search for in the title only, 37 00:02:29,720 --> 00:02:32,420 and then you can limit the sources or the domains 38 00:02:32,420 --> 00:02:35,570 which you want to search or exclude certain websites 39 00:02:35,930 --> 00:02:39,200 and you can also specify the date, time, language, 40 00:02:39,530 --> 00:02:43,550 and a number of other things. Now, the only required parameter is 41 00:02:43,880 --> 00:02:47,780 actually this API key. Now, in addition, 42 00:02:47,810 --> 00:02:51,500 we're probably gonna want to use one of these requests parameters. 43 00:02:52,250 --> 00:02:57,050 There's a lot of stocks that get lumped in into these analysis articles. 44 00:02:57,410 --> 00:03:01,420 So I think it will be more specific if we can actually check the title of the 45 00:03:01,420 --> 00:03:03,730 article for our company name. 46 00:03:04,600 --> 00:03:08,680 So let's get started putting this API to use. Up here 47 00:03:08,680 --> 00:03:10,840 we've really got the news end point, 48 00:03:11,770 --> 00:03:15,010 and we're going to delete this print statement, 49 00:03:15,400 --> 00:03:20,400 and we're going to use the requests library to get hold of some data from that 50 00:03:21,550 --> 00:03:22,383 endpoint. 51 00:03:23,920 --> 00:03:28,920 And then we're going to add our params. Our new params is firstly going to 52 00:03:29,950 --> 00:03:34,950 contain our API key and notice how it's spelled. When you are using parameters, 53 00:03:36,400 --> 00:03:40,840 you have to make sure that any capitalization or spelling must match their 54 00:03:40,840 --> 00:03:42,430 requirements exactly. 55 00:03:43,510 --> 00:03:48,040 So the API key is just going to be our news API key that we added previously, 56 00:03:48,580 --> 00:03:52,690 and then we're going to provide a queryInTitle as well. 57 00:03:53,020 --> 00:03:57,130 And I'm actually just going to straight up copy it because I'm not sure if that 58 00:03:57,130 --> 00:04:01,390 was an I or an L. That way we don't make any typos. Now, 59 00:04:01,420 --> 00:04:04,810 the query we're going to search for is actually our company name, 60 00:04:04,990 --> 00:04:07,420 because while we can search for the name of the stock, 61 00:04:07,660 --> 00:04:11,860 it's actually better to search for the company name because most articles tend 62 00:04:11,860 --> 00:04:14,650 to include the company name rather than the stock name. 63 00:04:15,730 --> 00:04:19,120 This is going to be the params that's going to go in here. 64 00:04:19,720 --> 00:04:22,300 Now we're going to get hold of our response 65 00:04:24,340 --> 00:04:28,420 and I'm going to print out the response as a JSON. 66 00:04:31,170 --> 00:04:31,410 All right. 67 00:04:31,410 --> 00:04:33,030 If you take a look at the documentation, 68 00:04:33,030 --> 00:04:37,080 they also tell you what an example output would look like. 69 00:04:37,500 --> 00:04:42,060 Firstly, we have a dictionary with status, 70 00:04:42,090 --> 00:04:45,150 total results, but most importantly articles, 71 00:04:45,330 --> 00:04:50,130 which is a list. In that list there's a bunch of dictionaries 72 00:04:50,160 --> 00:04:54,480 which contains things like the source, the author, the title, description, 73 00:04:54,870 --> 00:04:59,370 and each of these articles are an item in this list of articles. 74 00:05:00,060 --> 00:05:02,730 We can actually get hold of all the articles 75 00:05:03,210 --> 00:05:08,210 just by tapping into the news_response.json and then passing in the key 76 00:05:09,420 --> 00:05:14,400 articles. And now when I print that out, 77 00:05:14,400 --> 00:05:16,830 you can see its a list first of all, 78 00:05:17,280 --> 00:05:21,150 and then each article is a dictionary inside that list. 79 00:05:22,710 --> 00:05:27,630 Now we've managed to complete to-do number six and we can move on to 80 00:05:27,630 --> 00:05:28,800 to-do number seven, 81 00:05:29,280 --> 00:05:34,280 which is to use the Python slice operator to create a list that contains the 82 00:05:35,220 --> 00:05:38,130 first three articles. And as a hint, 83 00:05:38,220 --> 00:05:41,160 I've linked to the Stack Overflow page 84 00:05:41,190 --> 00:05:46,170 where they go through the slice operator in quite a bit of detail 85 00:05:46,170 --> 00:05:47,003 actually. 86 00:05:48,240 --> 00:05:51,900 If we want to only get the first three articles, 87 00:05:51,960 --> 00:05:55,950 then our start is probably going to be from the beginning and then we're going 88 00:05:55,950 --> 00:05:58,100 to loop to stop minus one. 89 00:05:58,490 --> 00:06:03,490 So that's going to be :3 because let's start counting from zero. Like 90 00:06:05,000 --> 00:06:08,300 that. And now if I print these three articles, 91 00:06:08,380 --> 00:06:09,213 right, 92 00:06:11,380 --> 00:06:12,010 to see them, 93 00:06:12,010 --> 00:06:17,010 if you click on this soft wrap and you can see the three items, 94 00:06:17,980 --> 00:06:22,540 one, two, and three. 95 00:06:24,310 --> 00:06:25,143 That's 96 00:06:25,630 --> 00:06:26,463 step two. 97 00:06:26,500 --> 00:06:31,500 Now we've managed to use the news API to get hold of the first three articles 98 00:06:32,440 --> 00:06:37,240 that talk about this particular company name. In the next lesson 99 00:06:37,270 --> 00:06:41,350 I'm going to go through the final part of the solution which is step three, 100 00:06:41,500 --> 00:06:46,500 and to figure out how to send the relevant parts of the articles to our mobile 101 00:06:47,140 --> 00:06:47,560 number.