WEBVTT

0
00:00.420 --> 00:02.310
In the first part of the solution,

1
00:02.340 --> 00:07.340
we managed to get our code to pull in the live stock data for the selected

2
00:08.180 --> 00:09.020
stock name.

3
00:09.740 --> 00:14.270
And then we looked at the difference in price between yesterday and the day

4
00:14.270 --> 00:18.140
before yesterday and then we worked out the percentage difference.

5
00:18.920 --> 00:22.760
Now we have this if statement which will print get news

6
00:23.090 --> 00:27.560
if the percentage difference is greater than a specified amount.

7
00:28.130 --> 00:31.760
Now, I've put right five here, just because ideally,

8
00:31.820 --> 00:33.980
if you were monitoring a particular stock,

9
00:34.220 --> 00:37.880
you'd probably only be interested if it changed by a large amount,

10
00:37.880 --> 00:42.800
like 5 or 10 or 50% set by you. But at the moment,

11
00:42.800 --> 00:47.780
because we're testing these things and the Tesla stock is only moved by one

12
00:47.780 --> 00:51.710
point something percent, then this is actually never going to be true.

13
00:51.800 --> 00:56.030
So I'm going to manually adjust this down so that we can continue working with

14
00:56.030 --> 01:00.770
the rest of the challenge. So now onto step two of the challenge,

15
01:00.920 --> 01:04.460
the first part is instead of printing get news,

16
01:04.670 --> 01:08.960
we're going to use the news API to get articles related to

17
01:08.960 --> 01:13.820
the company name. The news API is newsapi.org.

18
01:14.540 --> 01:17.780
And we can start by getting hold of an API key.

19
01:18.590 --> 01:21.740
Once you signed up, you should get a free API key

20
01:21.860 --> 01:25.580
and we're going to paste that into our project.

21
01:27.500 --> 01:31.550
Now we can start looking through the documentation and seeing how we can use

22
01:31.550 --> 01:32.990
this news API.

23
01:34.010 --> 01:39.010
There's a number of end points as we can see. There's top headlines and also

24
01:39.170 --> 01:41.810
everything. These are the two main end points.

25
01:42.470 --> 01:46.250
Now everything is better for article discovery

26
01:46.700 --> 01:50.990
whereas the headlines tend to only have a limited number of headlines.

27
01:51.530 --> 01:53.300
I've tested both of these

28
01:53.360 --> 01:58.360
and I found that the everything one is more likely to give us a news piece for

29
01:58.670 --> 02:03.260
stocks that we're looking for. Because very often a lot of these companies and

30
02:03.260 --> 02:06.530
their stocks don't actually make it to the day's headlines.

31
02:07.190 --> 02:12.170
So this is what the query looks like, this is the end point,

32
02:12.230 --> 02:16.730
and these are the parameters. Now, if we scroll down,

33
02:16.760 --> 02:20.330
we can see a number of request parameters; q

34
02:20.330 --> 02:25.330
which is the keyword or phrase the same in the article title and body, qIn

35
02:25.760 --> 02:29.210
Title is to search for in the title only,

36
02:29.720 --> 02:32.420
and then you can limit the sources or the domains

37
02:32.420 --> 02:35.570
which you want to search or exclude certain websites

38
02:35.930 --> 02:39.200
and you can also specify the date, time, language,

39
02:39.530 --> 02:43.550
and a number of other things. Now, the only required parameter is

40
02:43.880 --> 02:47.780
actually this API key. Now, in addition,

41
02:47.810 --> 02:51.500
we're probably gonna want to use one of these requests parameters.

42
02:52.250 --> 02:57.050
There's a lot of stocks that get lumped in into these analysis articles.

43
02:57.410 --> 03:01.420
So I think it will be more specific if we can actually check the title of the

44
03:01.420 --> 03:03.730
article for our company name.

45
03:04.600 --> 03:08.680
So let's get started putting this API to use. Up here

46
03:08.680 --> 03:10.840
we've really got the news end point,

47
03:11.770 --> 03:15.010
and we're going to delete this print statement,

48
03:15.400 --> 03:20.400
and we're going to use the requests library to get hold of some data from that

49
03:21.550 --> 03:22.383
endpoint.

50
03:23.920 --> 03:28.920
And then we're going to add our params. Our new params is firstly going to

51
03:29.950 --> 03:34.950
contain our API key and notice how it's spelled. When you are using parameters,

52
03:36.400 --> 03:40.840
you have to make sure that any capitalization or spelling must match their

53
03:40.840 --> 03:42.430
requirements exactly.

54
03:43.510 --> 03:48.040
So the API key is just going to be our news API key that we added previously,

55
03:48.580 --> 03:52.690
and then we're going to provide a queryInTitle as well.

56
03:53.020 --> 03:57.130
And I'm actually just going to straight up copy it because I'm not sure if that

57
03:57.130 --> 04:01.390
was an I or an L. That way we don't make any typos. Now,

58
04:01.420 --> 04:04.810
the query we're going to search for is actually our company name,

59
04:04.990 --> 04:07.420
because while we can search for the name of the stock,

60
04:07.660 --> 04:11.860
it's actually better to search for the company name because most articles tend

61
04:11.860 --> 04:14.650
to include the company name rather than the stock name.

62
04:15.730 --> 04:19.120
This is going to be the params that's going to go in here.

63
04:19.720 --> 04:22.300
Now we're going to get hold of our response

64
04:24.340 --> 04:28.420
and I'm going to print out the response as a JSON.

65
04:31.170 --> 04:31.410
<v 1>All right.</v>

66
04:31.410 --> 04:33.030
<v 0>If you take a look at the documentation,</v>

67
04:33.030 --> 04:37.080
they also tell you what an example output would look like.

68
04:37.500 --> 04:42.060
Firstly, we have a dictionary with status,

69
04:42.090 --> 04:45.150
total results, but most importantly articles,

70
04:45.330 --> 04:50.130
which is a list. In that list there's a bunch of dictionaries

71
04:50.160 --> 04:54.480
which contains things like the source, the author, the title, description,

72
04:54.870 --> 04:59.370
and each of these articles are an item in this list of articles.

73
05:00.060 --> 05:02.730
We can actually get hold of all the articles

74
05:03.210 --> 05:08.210
just by tapping into the news_response.json and then passing in the key

75
05:09.420 --> 05:14.400
articles. And now when I print that out,

76
05:14.400 --> 05:16.830
you can see its a list first of all,

77
05:17.280 --> 05:21.150
and then each article is a dictionary inside that list.

78
05:22.710 --> 05:27.630
Now we've managed to complete to-do number six and we can move on to

79
05:27.630 --> 05:28.800
to-do number seven,

80
05:29.280 --> 05:34.280
which is to use the Python slice operator to create a list that contains the

81
05:35.220 --> 05:38.130
first three articles. And as a hint,

82
05:38.220 --> 05:41.160
I've linked to the Stack Overflow page

83
05:41.190 --> 05:46.170
where they go through the slice operator in quite a bit of detail

84
05:46.170 --> 05:47.003
actually.

85
05:48.240 --> 05:51.900
If we want to only get the first three articles,

86
05:51.960 --> 05:55.950
then our start is probably going to be from the beginning and then we're going

87
05:55.950 --> 05:58.100
to loop to stop minus one.

88
05:58.490 --> 06:03.490
So that's going to be :3 because let's start counting from zero. Like

89
06:05.000 --> 06:08.300
that. And now if I print these three articles,

90
06:08.380 --> 06:09.213
<v 2>right,</v>

91
06:11.380 --> 06:12.010
<v 0>to see them,</v>

92
06:12.010 --> 06:17.010
if you click on this soft wrap and you can see the three items,

93
06:17.980 --> 06:22.540
one, two, and three.

94
06:24.310 --> 06:25.143
<v 2>That's</v>

95
06:25.630 --> 06:26.463
<v 0>step two.</v>

96
06:26.500 --> 06:31.500
Now we've managed to use the news API to get hold of the first three articles

97
06:32.440 --> 06:37.240
that talk about this particular company name. In the next lesson

98
06:37.270 --> 06:41.350
I'm going to go through the final part of the solution which is step three,

99
06:41.500 --> 06:46.500
and to figure out how to send the relevant parts of the articles to our mobile

100
06:47.140 --> 06:47.560
number.