covid19 in italy – a new plugin

Everybody knows what Covid19 is and everybody has seen their life and habits change with this pandemic, the quarantine and all the bad things that happened in the world during this year.

I’ve found myself checking the daily report from the Italian Health Ministry to stay informed on how the pandemic is evolving and how we are managing the situation as a Country, but I wanted to have that data available on this site, both as a way to spread awareness on the matter and also as an exercise for me to write a new plugin for WordPress.

I know there are a few plugins already on the plugins directory on WordPress.org, but as usual, I wanted to do things my way, so I sat down and wrote a widget plugin. You can see it at the bottom of this website.

I pull the data directly from the github repository made by the “Presidenza del Consiglio dei Ministri – Dipartimento della Protezione Civile” which is the civil protection department and is in charge of monitoring the situation on behalf of the Prime Minister.

The data is published to github every day at 6pm and I store it for no more than 24 hours, so that every day it gets updated with the latest news.

To do so I use a transient stored inside the wp_options table which expires every 24hours, then I fetch again the json from github and store it again.

/**
 * The heart of the plugin, this function retrieves the data from github and
 * stores it in a transient for up to 24h.
 *
 * @link    https://danix.xyz
 * @since   0.0.1
 * @package Dnxcovita
 */
function dnxcovita_retrieve_data() {
	// full history since 24/02/2020
	$all_data_link = 'https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-json/dpc-covid19-ita-andamento-nazionale.json';
	if ( false === ( $dnxcovita_data = get_transient( 'dnxcovita_data' ) ) ) {
		$json_data = file_get_contents( $all_data_link );
		$raw_data = json_decode( $json_data, true );
		$processed = array();
		foreach ( $raw_data as $item => $daily_data ) {
			// $item is the entry number and $daily_data is the array that we want to filter
			$processed[ $item ] = dnxcovita_walker( $daily_data );
		}

		set_transient( 'dnxcovita_data', $processed, DAY_IN_SECONDS );
	}
}

Here you can see the heart of the plugin, the function that retrieves the data from the official json stored on github. I then process it through a walker function and store it inside the database.

I’ve decided not to share the plugin on the WordPress directory, I think it’s redundant and doesn’t add any value to the situation, but it was something I wanted to do and I’m happy of the way it turned out.

If anyone is interested, you can check the code for the plugin on my personal repository.

Stay safe everyone, wear your mask and keep the distance from the others.

Danilo M.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  

This site uses Akismet to reduce spam. Learn how your comment data is processed.