Creating and Deploying News Fetching Cron Job with Python
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.

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
- 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:
- 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
- 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:
- TrueFoundry: https://truefoundry.com/
- TrueFoundry App: https://app.truefoundry.com/
- TrueFoundry Docs: http://docs.truefoundry.com/
- Code: https://github.com/d4rk-lucif3r/News_Cron_Job