Creating and Deploying News Fetching Cron Job with Python

Arsh Anwar
3 min readSep 23, 2022

--

News is a part of the information that keeps us up-to-date with the world. It is a crucial thing in a Human’s life because it informs us about the changing events and issues in the world outside. Therefore, checking the news daily is important.

Image Source: https://www.shortstack.com/blog/social-media-in-the-news-2016-2017-infographic/

In Today’s blog, we are going to create a Cron Job that will fetch News every morning for you from NewsAPI and will put it into MySQL.

Let’s get started

Introduction

We are going to create a Python Script that will fetch news data from NewsAPI upon running. Then we will deploy it as a cron job which will run every day at 0900 hrs.

Installing Libraries

All the requirements are present in requirements.txt

pip install -r requirements.txt

Getting API Key from NewsAPI

  1. Create an account at NewsAPI

2. Get API Key from here

Setting Up MySQL

We will use PlanetScale which is a platform offering free MySQL database instances.
Create a new Database here.

Get the connection credentials from the dashboard.

Writing Code

Let’s write code for our fetching:

  1. News fetching Script

2. Notification Script

Integrating Everything

Final Checks

Now we will run the main.py and will check the output

python main.py

Now everything is working we will deploy the job using TrueFoundry.

Prerequisite

Before deployment, we will upload the code on our GitHub and connect our GitHub to TrueFoundry from this page

Deployment

This time we will deploy the job using TrueFoundry’s WebApp.

Head over to this page so that we all are aligned.

Creating Job

  1. Click on new Deployment

2. Select the Job option and Workspace name

3. Fill out the properties and submit

If you need assistance in finding Cron Format use https://crontab.guru/

5) Wait for the build to be successful

6) Triggers

Now the job is deployed and will get triggered every day at 9:00 am.

Here, you can also manually trigger the job, edit and/or delete it.

Results

After the job is done you will results like this in MySQL

That’s it. Thanks for staying and deploying a script that will fetch news and write it to MySQL

Video:

Code:

The above code is also present in my Repository

References:

  1. TrueFoundry: https://truefoundry.com/
  2. TrueFoundry App: https://app.truefoundry.com/
  3. TrueFoundry Docs: http://docs.truefoundry.com/
  4. Code: https://github.com/d4rk-lucif3r/News_Cron_Job

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Arsh Anwar
Arsh Anwar

Written by Arsh Anwar

AI/ML expert. Built LuciferML (120k+ downloads). Co-founder @Revca, building smart solutions for a sustainable future.

No responses yet

Write a response