Skip to content

RESTful API for Malaysia Covid-19 data based on MoH and CITF's public dataset

Notifications You must be signed in to change notification settings

izzudinhafiz/mycovidapi

Repository files navigation

MYCovidAPI

Introduction

JSON API Site for Malaysian Covid statistics hosted at https://mycovidapi.izzudinhafiz.com.

The project builds upon the Covid and MySejahtera dataset by MoH Malaysia as well as the vaccination and registration data by CITF.

The intent is to allow any developer to easily develop dashboards using the comprehensive data supplied with a JSON friendly interface.

Documentation

Documentation is hosted on Postman and can be found here. There are examples both on Postman and further down this document.

Overview

The current V1 API presents all the raw data from MoH as is with no normalization or bucketing. This is to allow developers full access to the underlying data.

The API uses ISO 8601 format dates YYYY-MM-DD.

The API renames some of the fields that was given by MoH to be more consistent and self-descriptive. You can generally base the field name with those in MoH dataset.

Generally for each endpoint:

  • For most endpoints, there are /state/ and /country/ sub-endpoints
  • For endpoints having state data, you need to include state_id as below.
  • Omitting both start_date and end_date will return all the data.
  • Omitting start_date will return data from the start of the dataset.
  • Omitting end_date will return data up to the current date.

The API uses standardized abbreviation for Malaysian state that is based on this Wikipedia table.

State Name State Code
Johor JHR
Kedah KDH
Kelantan KTN
Melaka MLK
Negeri Sembilan NSN
Pahang PHG
Pulau Pinang PNG
Perak PRK
Perlis PLS
Sabah SBH
Sarawak SWK
Selangor SGR
Terengganu TRG
W.P. Kuala Lumpur KUL
W.P. Labuan LBN
W.P. Putrajaya PJY

Authentication

No authentication is required. Everyone is free to use this API.

Rate Limit

There is no rate limit currently, however, we expect users to use the API respectfully. We reserve the right to limit API calls if we detect excessive call rate.

Developer should also cache the data as the data is updated only once a day.

Examples

  1. How do I get a state's new Covid cases for a particular date?

    You can use the same start and end date in the API query.

    E.g. /api/v1/cases/state?state_id=KUL&start_date=2021-01-01&end_date=2021-01-01

  2. How do I get the nationwide new Covid cases for a particular date range?

    E.g. /api/v1/cases/country?start_date=2021-01-01&end_date=2021-01-01

  3. How do I get all the data for new Covid cases since the start?

    You can omit the start_date and end_date in the queries

    E.g. (state) /api/v1/cases/state?state_id=KUL

    E.g. (nation) /api/v1/cases/country

Local Setup for Development

Clone the repo locally

git clone https://github.com/izzudinhafiz/mycovidapi.git

Set up a local Python environment

cd ~/mycovidapi

python3 -m venv venv

source venv/bin/activate

pip install -r requirements.txt

Seed the database

mkdir localdata

python db_sync.py

Start the application

python app.py

Updating the local data

To manually update local data

python db_sync.py

Deployment Setup

Clone the repo locally

git clone https://github.com/izzudinhafiz/mycovidapi.git

Set up a local Python environment

cd ~/mycovidapi

python3 -m venv venv

source venv/bin/activate

pip install -r requirements.txt

Seed the database

mkdir localdata

python db_sync.py

Setup a gunicorn service to serve the app

Use your preferred text editor to create a service file

sudo nano /etc/systemd/system/mycovidapi.service

Create this systemd Unit file

[Unit]
Description=MY Covid API
After=network.target

[Service]
Type=simple
ExecStart= /home/user/mycovidapi/.venv/bin/gunicorn --workers=3 --chdir  /home/user/mycovidapi/ app:app

[Install]
WantedBy=multi-user.target

Setup nginx

Use your preferred text editor to create an nginx site file

sudo nano /etc/nginx/sites-available/mycovidapi.izzudinhafiz.com

Create this file:

server {
	server_name mycovidapi.izzudinhafiz.com;
	root /home/user/mycovidapi/;

	location / {
		proxy_pass http://127.0.0.1:8000;
		proxy_set_header Host $host;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	}
}

Link it to sites-enabled and allow nginx firewall access

sudo ln -s /etc/nginx/sites-available/mycovidapi.izzudinhafiz.com /etc/nginx/sites-enabled

sudo ufw allow 'Nginx Full'

Start gunicorn and nginx

sudo systemctl restart nginx
sudo systemctl start mycovidapi.service

Cronjob for daily database update

Create a daily.sh script to safely stop the service and perform update and restart service

#!/bin/bash
systemctl stop mycovidapi.service
/home/user/mycovidapi/.venv/bin/python /home/user/mycovidapi/db_sync.py
systemctl restart mycovidapi.service

Run sudo crontab -e to create a sudo cronjob to run the script. Example below runs every 6 hours

0 */6 * * * bash /home/user/mycovidapi/daily.sh >> /home/user/mycovidapi/daily_sync.log 2>&1

According to MoH Github page, the data is updated daily by 2359. So if you'd prefer to update the database at midnight instead, modify the cronjob to

0 0 * * * bash /home/user/mycovidapi/daily.sh >> /home/user/mycovidapi/daily_sync.log 2>&1

Sources

Open data on COVID-19 in Malaysia - MoH Malaysia

Open data on Malaysia's National Covid-​19 Immunisation Programme - CITF-Malaysia

JSON time-series of coronavirus cases - pomber

Roadmap

  • Integrate CITF data
  • Add consolidated and bucketed data

About

RESTful API for Malaysia Covid-19 data based on MoH and CITF's public dataset

Topics

Resources

Stars

Watchers

Forks