Disclaimer: We may earn a commission if you make any purchase by clicking our links. Please see our detailed guide here.

Follow us on:

Google News
Whatsapp

How to Integrate Python with an API That Delivers Media News from Around the World

Join the Opinion Leaders Network

Join the Opinion Leaders Network today and become part of a vibrant community of change-makers. Together, we can create a brighter future by shaping opinions, driving conversations, and transforming ideas into reality.

As the world population and metropolitan nation increase, the climate-related events experienced today are also increasing in impact and frequency. 

We want to learn about every event related to our environment and shared world. 

With the development of technology from the past to the present, people can instantly access events and news thousands of kilometers away from their locations. 

Especially today, web and mobile applications offer this to us very effectively. 

Various news providers keep people informed by sharing instant news on their websites and mobile applications.

What if we want to reach the information of famous news providers with extensive news networks from a single point? 

It is precisely for this need that Mediastack provides instant access to more than 7,500 news sources with the services it offers. How does it do this?  

Let’s look at how and in what situations we could use these services.

Briefly Why Mediastack?

Mediastack services provide a wide variety of news to people with more than 7,500 news sources and nearly 50 country preferences. 

It promises a secure subscription process with the 256-bit HTTPS encryption method it offers. 

It also delivers worldwide news, headlines, and blog posts in real-time, while communicating between services with scalable JSON APIs. 

In addition to these, it is quite possible to access news content of the past from the same service.

Mediastack also offers historical data to users. 

Mediastack also provides very understandable documentation, allowing you to easily integrate it into your projects.

For robust documentation and explanations, see https://mediastack.com/

Integration of Mediastack API to Python Programming Language

Get API key

To use Mediastack’s services with a wide news network, an API key is required. Registration is required to get this API key. 

You can register by choosing one of the flexible and affordable packages offered by Mediastack. After registration, you can see the API key in the Dashboard section. 

Finally, we will perform a code integration using this API key.

Code Integration

We perform an easy integration by creating a Python project and writing the following code.

import http.client, urllib.parse

if __name__ == '__main__':

  conn = http.client.HTTPConnection('api.mediastack.com')

  params = urllib.parse.urlencode({
      'access_key': '57*****05c',
      'categories': '-general,-sports',
      'sort': 'published_desc',
      'limit': 10,
  })

  conn.request('GET', '/v1/news?{}'.format(params))

  res = conn.getresponse()
  data = res.read()

  print(data.decode('utf-8'))

When you place your own API key in the code and run it, you will get a JSON output like the one below.

{
  "pagination": {
    "limit": 10,
    "offset": 0,
    "count": 10,
    "total": 10000
  },
  "data": [
    {
      "author": null,
      "title": "Blackrock closes on Saudi natural gas pipeline",
      "description": "Blackrock closes on Saudi natural gas pipeline",
      "url": "https:\/\/seekingalpha.com\/news\/3804205-blackrock-closes-on-saudi-natural-gas-pipeline?utm_source=feed_news_all\u0026utm_medium=referral",
      "source": "Seeking Alpha",
      "image": null,
      "category": "business",
      "language": "en",
      "country": "us",
      "published_at": "2022-02-23T15:52:02+00:00"
    },
    {
      "author": "Shiri Habib-Valdhorn",
      "title": "monday.com slump gains momentum after Q4 results",
      "description": "The Israeli work operating system company\u0027s share price is sharply lower, beneath its IPO price, and is down nearly 70% since November.",
      "url": "https:\/\/en.globes.co.il\/en\/article-1001403406#utm_source=RSS",
      "source": "Globes English | Israel Business Arena",
      "image": "https:\/\/res.cloudinary.com\/globes\/image\/upload\/t_desktop_article_content_header_800*392\/v1623574058\/direct\/%D7%A4%D7%AA%D7%99%D7%97%D7%AA_%D7%9E%D7%A1%D7%97%D7%A8_%D7%9E%D7%90%D7%A0%D7%93%D7%99%D7%99_%D7%A0%D7%90%D7%A1%D7%93%D7%A7_10.6_ntjkhe.jpg",
      "category": "business",
      "language": "en",
      "country": "il",
      "published_at": "2022-02-23T15:50:32+00:00"
    },..

As seen in the above response, the most up-to-date news came from the categories specified in the request.

Let’s change the query we sent to the Mediastack service a little more by adding parameters.

params = urllib.parse.urlencode({
  'access_key': '57*****05c',
  'categories': 'sports',
  'sort': 'published_desc',
  'limit': 2,
  'source': 'Terra Esportes'
})

By adding the source field, we only added news content from the ‘Terra Esportes’ source to the query. 

At the same time, by setting the limit to 2, we have specified that we will receive the two most recent news from the service. The response is as follows.

{
  "pagination": {
    "limit": 2,
    "offset": 0,
    "count": 2,
    "total": 10000
  },
  "data": [
    {
      "author": "LANCE!",
      "title": "Discuss\u00e3o com Hulk e duelos com Jorge Jesus: conhe\u00e7a V\u00edtor Pereira, novo t\u00e9cnico do Corinthians",
      "description": "Ap\u00f3s 17 anos, o Corinthians volta a ter treinador estrangeiro. Na manh\u00e3 de quarta-feira (23), o ...",
      "url": "https:\/\/www.terra.com.br\/esportes\/corinthians\/discussao-com-hulk-e-duelos-com-jorge-jesus-conheca-vitor-pereira-novo-tecnico-do-corinthians,05da6a51a475f1efc0eb14d049a23111t7rkw6in.html",
      "source": "Terra Esportes",
      "image": null,
      "category": "sports",
      "language": "pt",
      "country": "br",
      "published_at": "2022-02-23T16:24:20+00:00"
    },
    {
      "author": "LANCE!",
      "title": "Apresentador da ESPN diz que Athletico-PR s\u00f3 n\u00e3o \u00e9 mais relevante que tr\u00eas dos grandes clubes brasileiros",
      "description": "\u00c9 de conhecimento p\u00fablico que desde o in\u00edcio deste s\u00e9culo, o Athl\u00e9tico-PR tem frequentado o ...",
      "url": "https:\/\/www.terra.com.br\/esportes\/apresentador-da-espn-diz-que-athletico-pr-so-nao-e-mais-relevante-que-tres-dos-grandes-clubes-brasileiros,8a47439ab6706f723c1ad9c98b0fde0fjf23eubs.html",
      "source": "Terra Esportes",
      "image": null,
      "category": "sports",
      "language": "pt",
      "country": "br",
      "published_at": "2022-02-23T16:24:19+00:00"
    }
  ]
}

As seen in the incoming response, the Mediastack service also supports news in different languages.

At the same time, if we want news in a specific language as a response from the service, a query can be made by adding the Language field to the request.

Usages

Using this service, which contains up-to-date data, works in real-time, and supports more than one language, in your projects will make your work very comfortable.

You can present up-to-date news in the main language of each customer’s location—in multiple locations—from a single application. 

Since the service works in real-time, you can instantly send breaking news to your customers and attract more visitors to your application. 

You can configure the service according to the last-visited categories of your users and suggest news from different sources that may be of interest to you.

Conclusion

Mediastack, which offers instant data from thousands of sources, including historical data, to its customers at an affordable price, will make your work much easier with the powerful documentation that allows it to be easily integrated into applications.

Recomended

Partner With Us

Digital advertising offers a way for your business to reach out and make much-needed connections with your audience in a meaningful way. Advertising on Techgenyz will help you build brand awareness, increase website traffic, generate qualified leads, and grow your business.

Power Your Business

Solutions you need to super charge your business and drive growth

More from this topic