DEV Community

Harsh Vardhan Goswami
Harsh Vardhan Goswami

Posted on

How i built a COVID-19 News Restful API

Hello beautiful people this is me harsh. I created a COVID-19 news Restful API using python flask and beautiful soap. I named it CORONEWS-19 API and it also got its own website and documentation.

Why i built it?

I created a website called gocorona.wtf with my friend Roshan Chokshi and in our website we have a news section where we display the COVID-19 news so we were using newsapi and that api has a limit of hits per user and all that free and paid service shit so i just came up with a idea that why not create our own api for delivering COVID-19 news. So i just opened Atom(Text Edito) i personally use and started building it and now i will explain how.

How i built it?

So this CORONEWS-19 API is built on top of flask(python framework for making web apps) and beautiful soup and now you guys be thinking that from where you getting news so thats where beautiful soup comes in. Building of this API includes few stages.

1.Gathering the data.
2.Structuring the data.
3.Providing the data as response to request.

Lets talk about how i got the news data.

So, for this task i have used beautiful soup to scrap news websites to gather the news it's kind a illegal thing to scrap any website without permission but hey we can give it a shot. From every website i have gathered news article title, feature image, link to the article.

Now it's time to structure the data we have got from beautiful soup.

So, as i have mentioned above that i have scraped news website so its time to arrange the data in a proper manner so for this i took a empty list and then i appended every news article i got from beautiful soup in a dictionary format with title,img,link of the article the output was something like this:

{
    "news":[
        {
            "img":"https://raw.githubusercontent.com/theuitown/COROAPIWEB/master/20200328_183732_0000.png",
            "link":"https://www.newsnation.in/world/news/trump-expands-role-of-us-army-in-fight-against-coronavirus-pandemic-257699.html",
            "title":"President Trump Expands Role Of US Army In Fight Against Coronavirus Pandemic"
        },
        {
            "img":"https://raw.githubusercontent.com/theuitown/COROAPIWEB/master/20200328_183732_0000.png",
            "link":"https://news.abplive.com/videos/news/india-china-observes-national-mourning-day-for-covid-19-victims-1189266",
            "title":"China observes national mourning day for COVID-19 victims"
        }
    ]
}

Now we got the data now its time to ready it for the users of the API.

For this task i choose flask and i created a python function which has all the code inside which at last returns the list of news articles and then i initialized a flask app and set an route '/' to a class which gives a list in form of json using flask function called jsonify. Those who wanna read the code here you go.

from flask import Flask
app = Flask(__name__)

@app.route('/')
def articles():
    r.append({'title':title,'img':img,
    'link':link})
    return news

This is how i made this API and now its time to host this api somewhere so that people can use it in their projects. So i choosed heroku for this task because it supports server based web apps and also its free to some extent.

You can see the response of this API by visiting this link Click Here.
You can read the small documentation of this api here https://coronews.iamharsh.design/

Thanks for reading this post ❀️. I really appreciate it man and you can also read my own blog website Electron Blog. With that being said this is me harsh and i am signing out and i will catch you guys in my next one.

Top comments (2)

Collapse
 
msamgan profile image
Mohammed Samgan Khan

nice efforts bhai..

Collapse
 
electronlab profile image
Harsh Vardhan Goswami

Thanks a lot manπŸ–οΈπŸ–οΈ