In the first part of the solution,
we managed to get our code to pull in the live stock data for the selected
stock name.
And then we looked at the difference in price between yesterday and the day
before yesterday and then we worked out the percentage difference.
Now we have this if statement which will print get news
if the percentage difference is greater than a specified amount.
Now, I've put right five here, just because ideally,
if you were monitoring a particular stock,
you'd probably only be interested if it changed by a large amount,
like 5 or 10 or 50% set by you. But at the moment,
because we're testing these things and the Tesla stock is only moved by one
point something percent, then this is actually never going to be true.
So I'm going to manually adjust this down so that we can continue working with
the rest of the challenge. So now onto step two of the challenge,
the first part is instead of printing get news,
we're going to use the news API to get articles related to
the company name. The news API is newsapi.org.
And we can start by getting hold of an API key.
Once you signed up, you should get a free API key
and we're going to paste that into our project.
Now we can start looking through the documentation and seeing how we can use
this news API.
There's a number of end points as we can see. There's top headlines and also
everything. These are the two main end points.
Now everything is better for article discovery
whereas the headlines tend to only have a limited number of headlines.
I've tested both of these
and I found that the everything one is more likely to give us a news piece for
stocks that we're looking for. Because very often a lot of these companies and
their stocks don't actually make it to the day's headlines.
So this is what the query looks like, this is the end point,
and these are the parameters. Now, if we scroll down,
we can see a number of request parameters; q
which is the keyword or phrase the same in the article title and body, qIn
Title is to search for in the title only,
and then you can limit the sources or the domains
which you want to search or exclude certain websites
and you can also specify the date, time, language,
and a number of other things. Now, the only required parameter is
actually this API key. Now, in addition,
we're probably gonna want to use one of these requests parameters.
There's a lot of stocks that get lumped in into these analysis articles.
So I think it will be more specific if we can actually check the title of the
article for our company name.
So let's get started putting this API to use. Up here
we've really got the news end point,
and we're going to delete this print statement,
and we're going to use the requests library to get hold of some data from that
endpoint.
And then we're going to add our params. Our new params is firstly going to
contain our API key and notice how it's spelled. When you are using parameters,
you have to make sure that any capitalization or spelling must match their
requirements exactly.
So the API key is just going to be our news API key that we added previously,
and then we're going to provide a queryInTitle as well.
And I'm actually just going to straight up copy it because I'm not sure if that
was an I or an L. That way we don't make any typos. Now,
the query we're going to search for is actually our company name,
because while we can search for the name of the stock,
it's actually better to search for the company name because most articles tend
to include the company name rather than the stock name.
This is going to be the params that's going to go in here.
Now we're going to get hold of our response
and I'm going to print out the response as a JSON.
1: All right.
0: If you take a look at the documentation,
they also tell you what an example output would look like.
Firstly, we have a dictionary with status,
total results, but most importantly articles,
which is a list. In that list there's a bunch of dictionaries
which contains things like the source, the author, the title, description,
and each of these articles are an item in this list of articles.
We can actually get hold of all the articles
just by tapping into the news_response.json and then passing in the key
articles. And now when I print that out,
you can see its a list first of all,
and then each article is a dictionary inside that list.
Now we've managed to complete to-do number six and we can move on to
to-do number seven,
which is to use the Python slice operator to create a list that contains the
first three articles. And as a hint,
I've linked to the Stack Overflow page
where they go through the slice operator in quite a bit of detail
actually.
If we want to only get the first three articles,
then our start is probably going to be from the beginning and then we're going
to loop to stop minus one.
So that's going to be :3 because let's start counting from zero. Like
that. And now if I print these three articles,
2: right,
0: to see them,
if you click on this soft wrap and you can see the three items,
one, two, and three.
2: That's
0: step two.
Now we've managed to use the news API to get hold of the first three articles
that talk about this particular company name. In the next lesson
I'm going to go through the final part of the solution which is step three,
and to figure out how to send the relevant parts of the articles to our mobile
number.