News retrieval using Inshorts API in Python

In this tutorial, we are going to retrieve news from all over the globe. To do this we are going to use the Inshorts API. Our script will fetch us all news of the hot and trendy stuff happening worldwide. Our API offers a wide range of topics to choose from like politics, sports, fashion etc.

This is an unofficial API for Inshorts news platform. Using this API you can retrieve news from multiple different sources from around the world as gathered by the inshorts platform.

Let us make our script now.

We will need to import the requests module in python because we will be interacting and sending requests to the API.

import requests

Now lets us start by initialising a list and let us send a request to the API.

In line 4, we convert the response to JSON format for readability.

news = []
url = 'https://inshortsv2.vercel.app/news/top'
response = requests.get(url)
x = response.json()

‘https://inshortsv2.vercel.app/news/top’ is the API request to fetch the top news.

We can also use ‘https://inshortsv2.vercel.app/news/all’ for all the available news and ‘https://inshortsv2.vercel.app/news/trending’ to fetch the trending news.

Fetching news by topics

To fetch the news of a certain topic we can use ‘https://inshortsv2.vercel.app/topics/{topic_name}’

Just replace ‘{topic_name}’ with a specific topic, the list of different topics that our API offers are:

  1. national
  2. business
  3. politics
  4. sports
  5. technology
  6. startups
  7. entertainment
  8. hatke
  9. education
  10. world
  11. automobile
  12. science
  13. travel
  14. miscellaneous
  15. fashion

You can find the documentation of the APIĀ here.

This is how the JSON format looks like.

{'total': 9, 'articles': [{'title': 'Flight operations to remain suspended on revised lockdown days: Kolkata airport', 'description': 'Flight operations at Kolkata airport will remain suspended on the revised lockdown days, the airport authorities have announced. West Bengal government has announced that the state will be on lockdown on August 5, 8, 20, 21, 27, 28 and 31. Earlier, the suspension of flights to Kolkata from Delhi, Mumbai, Pune, Nagpur, Chennai and Ahmedabad was extended till August 15.', 'author': 'Arshiya Chopra', 'source_name': 'India.com', 'source_url': 'https://www.india.com/news/india/kolkata-lockdown-flight-operations-to-remain-suspended-on-these-days-in-august-read-details-4101981/amp/?utm_campaign=fullarticle&utm_medium=referral&utm_source=inshorts', 'image_url': 'https://static.inshorts.com/inshorts/images/v1/variants/jpg/m/2020/08_aug/4_tue/img_1596534544423_427.jpg?', 'created_at': 1596536051000, 'inshorts_url': 'https://shrts.in/Jr9X'}, {'title': 'Delhi bans hookahs in bars, hotels, other public places to curb COVID-19 spread', 'description': 'Delhi government has banned the use of hookahs, with or without tobacco, in all public places, including hotels, restaurants and bars, with immediate effect to control the spread of COVID-19. The government said smoking products such as water pipes often involve the sharing of mouthpieces and hoses, which could facilitate the transmission of coronavirus in communal and social settings.', 'author': 'Arshiya Chopra', 'source_name': 'Zee News', 'source_url': 'https://zeenews.india.com/india/delhi-records-805-new-covid-19-cases-with-17-deaths-hookah-in-hotels-restaurants-banned-2300363.html/amp?utm_campaign=fullarticle&utm_medium=referral&utm_source=inshorts', 'image_url': 'https://static.inshorts.com/inshorts/images/v1/variants/jpg/m/2020/08_aug/4_tue/img_1596530711037_267.jpg?', 'created_at': 1596531782000, 'inshorts_url': 'https://shrts.in/5fp7'}, {'title': 'Pradeep Singh tops UPSC Civil Services Exam 2019', 'description': 'Union Public Service Commission (UPSC) has announced the result of Civil Services Exam 2019 on its official website. While, Pradeep Singh has topped the exam, Pratibha Verma is the topper among women candidates. As many as 829 candidates have been selected and the result of 11 candidates has been withheld.', 'author': 'Ankush Verma', 'source_name': 'ANI', 'source_url': 'https://twitter.com/ANI/status/1290533188528291840?utm_campaign=fullarticle&utm_medium=referral&utm_source=inshorts', 'image_url': 'https://static.inshorts.com/inshorts/images/v1/variants/jpg/m/2020/08_aug/4_tue/img_1596524305050_247.jpg?', 'created_at': 1596525594000, 'inshorts_url': 'https://shrts.in/2q6Q'}, {'title': 'India records highest COVID-19 tests in a single day with 6,61,715 tests', 'description': 'India recorded the highest number of COVID-19 sample tests in a single day, with 6,61,715 tests conducted in the last 24 hours, said Ministry of Health. The total cases have reached 18,55,745 after a one-day jump of 52,050. Meanwhile, recoveries have increased to 12,30,509. Death toll has surged to 38,938 and 5,86,298 cases are active.', 'author': 'Daisy Mowke', 'source_name': 'MoHFW', 'source_url': 'https://www.mohfw.gov.in/?utm_campaign=fullarticle&utm_medium=referral&utm_source=inshorts', 'image_url': 'https://static.inshorts.com/inshorts/images/v1/variants/jpg/m/2020/08_aug/4_tue/img_1596511206831_600.jpg?', 'created_at': 1596515740000, 'inshorts_url': 'https://shrts.in/Ym9W'}, {'title': 'BMC appeals offices in Mumbai to remain shut today amid heavy rain forecast', 'description': 'The Brihanmumbai Municipal Corporation (BMC) has appealed to all offices and other establishments in Mumbai, except emergency services, to remain shut on Tuesday amid heavy rainfall forecast. A red alert has also been issued for two days (Tuesday and Wednesday) for extremely heavy rain in Mumbai. According to BMC, Mumbai received 140.5 mm rain from August 3 to August 4.', 'author': 'Jigyasu', 'source_name': 'Hindustan Times', 'source_url': 'https://m.hindustantimes.com/mumbai-news/mumbai-rains-local-train-services-stop-bmc-appeals-to-offices-to-remain-shut/story-gY8k6rEggkKGKH9A9AIDEI_amp.html?utm_campaign=fullarticle&utm_medium=referral&utm_source=inshorts', 'image_url': 'https://static.inshorts.com/inshorts/images/v1/variants/jpg/m/2020/08_aug/4_tue/img_1596510564665_970.jpg?', 'created_at': 1596512689000, 'inshorts_url': 'https://shrts.in/ekq1'}, {'title': 'Former Karnataka CM Siddaramaiah tests positive for COVID-19', 'description': 'Congress leader and former Karnataka CM Siddaramaiah has tested positive for coronavirus. "I have been tested positive for COVID-19 and also been admitted to the hospital on the advice of doctors as a precaution," the Congress leader wrote on Twitter. "I request all who had come in contact with me to check out for symptoms and quarantine themselves," Siddaramaiah added.', 'author': 'Jigyasu', 'source_name': 'The Quint', 'source_url': 'https://www.thequint.com/news/india/siddaramaiah-ex-karnataka-cm-congress-leader-tests-covid-19-coronavirus-positive?utm_campaign=fullarticle&utm_medium=referral&utm_source=inshorts', 'image_url': 'https://static.inshorts.com/inshorts/images/v1/variants/jpg/m/2020/08_aug/4_tue/img_1596509469407_688.jpg?', 'created_at': 1596511181000, 'inshorts_url': 'https://shrts.in/gaay'}, {'title': 'TikTok must sell US business by Sept 15 or it will be shut in US: Trump', 'description': 'US President Donald Trump has said that Chinese app TikTok must sell its US operations by September 15 or it\'ll be "out of business" in the US. Trump, who earlier opposed Microsoft buying TikTok\'s US business, said, "If somebody, whether it\'s Microsoft, buys it, that will be interesting." "TikTok can\'t be controlled for security reasons by China," Trump added.', 'author': 'Pragya Swastik', 'source_name': 'Hindustan Times', 'source_url': 'https://m.hindustantimes.com/world-news/donald-trump-says-tiktok-must-sell-us-operations-by-september-15-or-close/story-wu95VrlxrOT3cdahAJ2EJM_amp.html?utm_campaign=fullarticle&utm_medium=referral&utm_source=inshorts', 'image_url': 'https://static.inshorts.com/inshorts/images/v1/variants/jpg/m/2020/08_aug/3_mon/img_1596477790978_573.jpg?', 'created_at': 1596478534000, 'inshorts_url': 'https://shrts.in/fAK9'}, {'title': "Employees' workday increased by 48 minutes during COVID-19: Study", 'description': "Employees' workday increased by 48.5 minutes due to increased meetings and work e-mails amid coronavirus pandemic, according to a study by the National Bureau of Economic Research. The study covered 3.1 million people at over 21,000 companies across North America, Europe and the Middle East. Longer days persisted in New York, San Jose and most of Europe well into May.", 'author': 'Pragya Swastik', 'source_name': 'BloombergQuint', 'source_url': 'https://www.bloombergquint.com/business/the-pandemic-workday-is-48-minutes-longer-and-has-more-meetings?utm_campaign=fullarticle&utm_medium=referral&utm_source=inshorts', 'image_url': 'https://static.inshorts.com/inshorts/images/v1/variants/jpg/m/2020/08_aug/4_tue/img_1596545759381_900.jpg?', 'created_at': 1596547402000, 'inshorts_url': 'https://shrts.in/oJrr'}, {'title': "Sushant's ex Ankita shares quote that says 'I can't be bought and I can't be sold'", 'description': 'Late actor Sushant Singh Rajput\'s ex-girlfriend, actress Ankita Lokhande has shared a quote that says, "I follow the journey of my heart and the singing of my soul; I cannot be bought, and I cannot be sold." She wrote "#listeningtomyhigherself" in the caption. Earlier, speaking about Sushant\'s death, Ankita had said, "I want people to remember him as a hero."', 'author': 'Daisy Mowke', 'source_name': 'Twitter', 'source_url': 'https://twitter.com/anky1912/status/1290545546822729729?utm_campaign=fullarticle&utm_medium=referral&utm_source=inshorts', 'image_url': 'https://static.inshorts.com/inshorts/images/v1/variants/jpg/m/2020/08_aug/4_tue/img_1596548187890_546.jpg?', 'created_at': 1596547180000, 'inshorts_url': 'https://shrts.in/aJ5i'}]}

The information that API provides is the title, the description, author, source name, source URL, image URL and Inshorts URL.

Now we just need to use dictionary operations to extract the data from the JSON response.

n = x['total']
y = x['articles']

for i in range (n):
    news = y[i]
    title = news['title']
    des = news['description']
    source = news['source_name']
    source_url = news['source_url']
    print('\n\n\n\n\n\n\n\n\n')
    print("TITLE: " + title)
    print("\nDESCRIPTION: "+des)
    print("\nSOURCE: "+source)
    print('\nSOURCE URL: '+source_url)

Output:

TITLE: Flight operations to remain suspended on revised lockdown days: Kolkata airport

DESCRIPTION: Flight operations at Kolkata airport will remain suspended on the revised lockdown days, the airport authorities have announced. West Bengal government has announced that the state will be on lockdown on August 5, 8, 20, 21, 27, 28 and 31. Earlier, the suspension of flights to Kolkata from Delhi, Mumbai, Pune, Nagpur, Chennai and Ahmedabad was extended till August 15.

SOURCE: India.com

SOURCE URL: https://www.india.com/news/india/kolkata-lockdown-flight-operations-to-remain-suspended-on-these-days-in-august-read-details-4101981/amp/?utm_campaign=fullarticle&utm_medium=referral&utm_source=inshorts


TITLE: Delhi bans hookahs in bars, hotels, other public places to curb COVID-19 spread

DESCRIPTION: Delhi government has banned the use of hookahs, with or without tobacco, in all public places, including hotels, restaurants and bars, with immediate effect to control the spread of COVID-19. The government said smoking products such as water pipes often involve the sharing of mouthpieces and hoses, which could facilitate the transmission of coronavirus in communal and social settings.

SOURCE: Zee News

SOURCE URL: https://zeenews.india.com/india/delhi-records-805-new-covid-19-cases-with-17-deaths-hookah-in-hotels-restaurants-banned-2300363.html/amp?utm_campaign=fullarticle&utm_medium=referral&utm_source=inshorts


TITLE: Pradeep Singh tops UPSC Civil Services Exam 2019

DESCRIPTION: Union Public Service Commission (UPSC) has announced the result of Civil Services Exam 2019 on its official website. While, Pradeep Singh has topped the exam, Pratibha Verma is the topper among women candidates. As many as 829 candidates have been selected and the result of 11 candidates has been withheld.

SOURCE: ANI

SOURCE URL: https://twitter.com/ANI/status/1290533188528291840?utm_campaign=fullarticle&utm_medium=referral&utm_source=inshorts


TITLE: India records highest COVID-19 tests in a single day with 6,61,715 tests

DESCRIPTION: India recorded the highest number of COVID-19 sample tests in a single day, with 6,61,715 tests conducted in the last 24 hours, said Ministry of Health. The total cases have reached 18,55,745 after a one-day jump of 52,050. Meanwhile, recoveries have increased to 12,30,509. Death toll has surged to 38,938 and 5,86,298 cases are active.

SOURCE: MoHFW

SOURCE URL: https://www.mohfw.gov.in/?utm_campaign=fullarticle&utm_medium=referral&utm_source=inshorts


TITLE: BMC appeals offices in Mumbai to remain shut today amid heavy rain forecast

DESCRIPTION: The Brihanmumbai Municipal Corporation (BMC) has appealed to all offices and other establishments in Mumbai, except emergency services, to remain shut on Tuesday amid heavy rainfall forecast. A red alert has also been issued for two days (Tuesday and Wednesday) for extremely heavy rain in Mumbai. According to BMC, Mumbai received 140.5 mm rain from August 3 to August 4.

SOURCE: Hindustan Times

SOURCE URL: https://m.hindustantimes.com/mumbai-news/mumbai-rains-local-train-services-stop-bmc-appeals-to-offices-to-remain-shut/story-gY8k6rEggkKGKH9A9AIDEI_amp.html?utm_campaign=fullarticle&utm_medium=referral&utm_source=inshorts


TITLE: Former Karnataka CM Siddaramaiah tests positive for COVID-19

DESCRIPTION: Congress leader and former Karnataka CM Siddaramaiah has tested positive for coronavirus. "I have been tested positive for COVID-19 and also been admitted to the hospital on the advice of doctors as a precaution," the Congress leader wrote on Twitter. "I request all who had come in contact with me to check out for symptoms and quarantine themselves," Siddaramaiah added.

SOURCE: The Quint

SOURCE URL: https://www.thequint.com/news/india/siddaramaiah-ex-karnataka-cm-congress-leader-tests-covid-19-coronavirus-positive?utm_campaign=fullarticle&utm_medium=referral&utm_source=inshorts


TITLE: TikTok must sell US business by Sept 15 or it will be shut in US: Trump

DESCRIPTION: US President Donald Trump has said that Chinese app TikTok must sell its US operations by September 15 or it'll be "out of business" in the US. Trump, who earlier opposed Microsoft buying TikTok's US business, said, "If somebody, whether it's Microsoft, buys it, that will be interesting." "TikTok can't be controlled for security reasons by China," Trump added.

SOURCE: Hindustan Times

SOURCE URL: https://m.hindustantimes.com/world-news/donald-trump-says-tiktok-must-sell-us-operations-by-september-15-or-close/story-wu95VrlxrOT3cdahAJ2EJM_amp.html?utm_campaign=fullarticle&utm_medium=referral&utm_source=inshorts


TITLE: Employees' workday increased by 48 minutes during COVID-19: Study

DESCRIPTION: Employees' workday increased by 48.5 minutes due to increased meetings and work e-mails amid coronavirus pandemic, according to a study by the National Bureau of Economic Research. The study covered 3.1 million people at over 21,000 companies across North America, Europe and the Middle East. Longer days persisted in New York, San Jose and most of Europe well into May.

SOURCE: BloombergQuint

SOURCE URL: https://www.bloombergquint.com/business/the-pandemic-workday-is-48-minutes-longer-and-has-more-meetings?utm_campaign=fullarticle&utm_medium=referral&utm_source=inshorts


TITLE: Sushant's ex Ankita shares quote that says 'I can't be bought and I can't be sold'

DESCRIPTION: Late actor Sushant Singh Rajput's ex-girlfriend, actress Ankita Lokhande has shared a quote that says, "I follow the journey of my heart and the singing of my soul; I cannot be bought, and I cannot be sold." She wrote "#listeningtomyhigherself" in the caption. Earlier, speaking about Sushant's death, Ankita had said, "I want people to remember him as a hero."

SOURCE: Twitter

SOURCE URL: https://twitter.com/anky1912/status/1290545546822729729?utm_campaign=fullarticle&utm_medium=referral&utm_source=inshorts

 

Leave a Reply