How To Create NEWS API?

In this commercial world, there are a lot of Newspaper publishers and websites available. There are a myriad number of articles about a wide variety of topics. What if you can search all the articles yourself just by entering a keyword and can design them too. Doesn’t it sound interesting!! In this article, let’s discuss how to create a NEWS search based on keywords using open-source NEWS API and python.

What is an open-source API??

Open-source API/public API (Application Programming Interface) is an open public accessible API, which provides the interface between the program and the application.

NEWS API:

An open-source NEWS API is a URL which on requested gives required data in JSON format containing the following-

  • Status of request
  • Total results available
  • Array of articles

{

“status”: “ok”,

“totalResults”: 38,

“articles”: []

}

And each element of the array(articles) is again a JSON format which contains

  • Source (JSON format with contents ID and NULL)
  • Title
  • Author
  • Description
  • URL
  • URL to image
  • Published At
  • Content

{

“source”: {

“Id”: “ID,

“name”: “NAME”

}

“author”: “xxxxxxxxxx”,

“title”: “How to use NEWS API?”,

“description”: “NEWS API is an open-source API service that can be used by anybody”,

“url”: “https://newsapi.org/”,

“urlToImage”: “https://newsapi.org/”,

“publishedAt”: “2020-11-20T09:20:19Z”,

“content”: “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”

}

This open-source API is developed and maintained by the NEWS API

How to generate NEWS API?

  1. Firstly, open the  NEWS API.
  2. Then click on the GET API KEY.
  3. Now provide the mentioned details for creating an account with the NEWS API.
    • First name
    • E-MAIL
    • Password
    • Choose whether you are an individual or a group
    • Confirm you are not a robot
    • Agree to their terms
  1. After Signing in you will have 5 types of API available.
    • All articles based on key-word of recent Month sorted by recently published.
    • Top Business Headlines in the U.S right now.
    • All articles mentioning a key-word sorted by popular publisher.
    • Top articles from TechCrunch at present.
    • All articles published by Wall street journal in the last 6 months.
  1. After signing in, you will have the link shown in the image above with the API. Access your API key in the profile section.
  2. So the API is generated successfully.
  3. In this article, we will discuss the first type of API, and how to get all the URLs of NEWS related to the keyword provided.

SOURCE CODE:

The code is written using Python and a detailed explanation of the same is given below.

  1. First, make sure the ‘requests’ and ‘JSON’ libraries are installed.
  2. Then, import requests and JSON libraries.
  3. Now take the input key-word using input() method.
  4. Copy the API from NEWS API

http://newsapi.org/v2/everything?q=bitcoin&from=2020-10-20&sortBy=publishedAt&apiKey=API_KEY

(If you are signed in, then the API_KEY will be automatically replaced)

  1. Replace the topic name with your input in API using the Concatenation operator(‘+’).
  2. Make a web request using requests class.
    1. request(“GET” , URL) – Method to make a GET from mentioned URL.
    2. ,text – attribute to the above method to get a text  as string from the URL.
  1. Convert the string into a JSON format
  • Json.loads(‘str’) – method to convert string to json. The string should be properly formatted as per the JSON format beforehand.
  1. Iterate over ‘articles’ array to get the URL  of each article and print the required URL in each loop(refer JSON structure at the beginning of the article for a better understanding).
    • P [‘articles’][i][‘url’]

  1. The whole code looks like this-

The coding is done. Now let’s run it. While executing the code, you must ensure that the system is connected to the internet.

The sample output looks as shown:


CONCLUSION:

Open-source API is very useful in the IoT industry. These APIs are changing the way applications interact with each other and are going to be used widely in the days to come. In this article, we’ve learnt about the open-source API, NEWS API, step by step manner to generate the API and Detailed explanation of the source code used.

so, Try this simple program and you can play around with the code using other open-source APIs like weather API, Youtube API, Instagram API e.t.c. Have fun creating some exciting tasks.

Written by: Batta Pruthvi

If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs

Leave a Comment

Your email address will not be published. Required fields are marked *