UnderLine — Just the Essentials

News Aggregator

jaseem CK
TechCrush

--

#The_incomplete_projects

News aggregator, online platform or software device that collects news stories and other information as that information is published and organizes the information in a specific manner. This is accomplished in several ways. Some aggregators are curated by people to whom certain types of information is of particular import and others use HTML (hypertext mark-up language) coding on the websites of news-gathering organizations to create RSS (really simple syndication) feeds and other public notifications of instant updates to news content regarding a specific subject.

There are a number of News aggregator available for free and it has been most useful in these recent days. As a matter of fact people are turning in to digital news platforms than the tradition newspapers. This gives them real time news which is available real fast within some hours of the actual event happens. The coming years will see more of these coming.

This is a small attempt to create such a web application which uses an API to fetch top headlines. As the name suggest it just gives the headline and a brief description of the news. Nothing more, nothing less.

Things that can come handy in the project are:

  • newsapi API
  • APIClient in python
  • Flask app
  • Heroku app

newsapi API

News API is a simple HTTP REST API for searching and retrieving live articles from all over the web. It can help you answer questions like:

  • What top stories is the NY Times running right now?
  • What new articles were published about the next iPhone today?
  • Has my company or product been mentioned or reviewed by any blogs recently?

You can search for articles with any combination of the following criteria:

  • Keyword or phrase. Eg: find all articles containing the word ‘Microsoft’.
  • Date published. Eg: find all articles published yesterday.
  • Source name. Eg: find all articles by ‘TechCrunch’.
  • Source domain name. Eg: find all articles published on nytimes.com.
  • Language. Eg: find all articles written in English.

You can sort the results in the following orders:

  • Date published
  • Relevancy to search keyword
  • Popularity of source

The Python client library can be used to integrate News API into your Python application.

To install: $ pip install newsapi-python

from newsapi import NewsApiClient

# Init
newsapi = NewsApiClient(api_key='API_KEY')

# /v2/top-headlines
top_headlines = newsapi.get_top_headlines(q='bitcoin',
sources='bbc-news,the-verge',
category='business',
language='en',
country='us')

# /v2/everything
all_articles = newsapi.get_everything(q='bitcoin',
sources='bbc-news,the-verge',
domains='bbc.co.uk,techcrunch.com',
from_param='2017-12-01',
to='2017-12-12',
language='en',
sort_by='relevancy',
page=2)

# /v2/sources
sources = newsapi.get_sources()

This will fetch the titles, description, image, url etc. from the source. This can be used to display in a webpage we make. This project uses the flask app for this.

Flask APP

Check out these tutorial blogs to learn how to build a flask app:

The main.py of the project initialize the api configuration and fetch the required details.

from flask import Flask, render_template
from newsapi import NewsApiClient
app = Flask(__name__)@app.route("/")
def homepage():
newsapi = NewsApiClient(api_key="APIKEY")
topheadlines = newsapi.get_top_headlines(language='en')
articles = topheadlines['articles']
desc=[]
news=[]
for i in range(len(articles)):
myarticles = articles[i]
news.append(myarticles['title'])
desc.append(myarticles['description'])
mylist = zip(news,desc)
return render_template('index.html',context=mylist)

Then we can use these in the index.html page:

<div class="row">
{% for new, des in context %}
<div class="col">
<h3 class="card-title strong">{{new}}</h3>
<p class="card-text">{{des}}</p>
</div>
</div>
</div>
{% endfor %}
</div>

Play around with API and create different routes for each tags you wish include in the site.

Deploy in Heroku

The flask app can be deployed in heroku. First you would need a heroku account which is free. You can deploy a number of applications of a limited size with heroku for free. Check Heroku and start hosting your applications.

Go through the below blog to deploy a flask app:

News aggregator are getting popular in recent years and it is natural to get curious about how those works. This can give you a brief description of the entire function if not the entire picture.

The source code for the project is available in github. The web application is deployed in heroku and is available in UnderLine-news.

#Keep_Coding!Keep_Innovating

--

--

jaseem CK
TechCrush

Coder, Startup enthusiast, Business aspirant, a maker in heart….