The news API with python

This week’s blog post is a simple tutorial on how to develop a command line application that consumes the News API.

Since working on this task is a complete project, we shall treat it with the respect that is supposed to be accorded to a project. This means that the Pivotal trucker will be an important tool we shall use to keep truck of the progress on this project.

Let’s begin by defining what a web service is. From the many definitions available online, I chose this one: That web services are XML-based information exchange systems that use the Internet for direct application-to-application interaction. These systems can include programs, objects, messages, or documents.

That being said, let’s take a brief look at what command line is. Basically command lines were the only method of interactions between the computer and the humans back in time. Meanwhile the command line interface is the point of interaction between the user and computer program whereby a user keys in the computer console, some commands in text form line by line and gets responses instead of using the mouse. The opposite of command line interface is the graphical user interface (GUI) where a mouse is used.

Building a command line interface in python

There is much to say about command line interface but with my level of understanding of python so far, I will look at only what is necessary for this particular work because am just being pushed to the wall, and that will be basic steps of building the command line application.

To begin with, the tool we are going to use is the click. There are so many tools for building the command line application but I chose click because it has a great documentation filled with examples and also it includes instructions on packaging your app as a Python application so it’s easier to run. But all I Know is that click is a python package for creating beautiful command line interfaces so if we are going to use it, we have to install it into out python library and update using Pip.

This is how we install click; Go to your terminal and type Pip install Click.

Now click has been successfully installed as seen in the screen shot.

Next we have to also install the news API still using pip the same way we installed click.

Next we have to get the news API which is free of charge but I have to sign in to generate the API key. Using this API takes few steps to set up. We must first request a key. Now I have requested the API Key and it has been given to me and below is the screen shot.

Once those steps are complete, we can write the following to connect our new API key:

import click
import requests
apikey= ‘ced42af865c5482aa78652be689b4af0’
# four news sources
def newsFromBBC():
#news from bbc
main_url = “ https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=ced42af865c5482aa78652be689b4af0"
#main_url = “https://newsapi.org/v2/top-headlines?sources=associated-press&apiKey=ced42af865c5482aa78652be689b4af0"
#main_url = “https://newsapi.org/v2/top-headlines?sources=abc-news&apiKey=ced42af865c5482aa78652be689b4af0"
#main_url = “https://newsapi.org/v2/top-headlines?sources=focus&apiKey=ced42af865c5482aa78652be689b4af0"
# fetching data in json format
open_bbc_page = requests.get(main_url).json()
# getting all articles in a string article
article = open_bbc_page[“articles”]
# empty list which will
# contain all trending news
results = []
for ar in article:
results.append(ar[“title”])
for i in range(len(results)):
# printing all trending news
print(i + 1, results[i])
# Driver Code
if __name__ == ‘__main__’:
# function call
newsFromBBC()