Statistics-Covid

 view release on metacpan or  search on metacpan

lib/Statistics/Covid/DataProvider/UK/BBC.pm  view on Meta::CPAN

package Statistics::Covid::DataProvider::UK::BBC;

use 5.006;
use strict;
use warnings;

our $VERSION = '0.23';

use parent 'Statistics::Covid::DataProvider::Base';

use Statistics::Covid::Datum;

use DateTime;
use DateTime::Format::Strptime;
use File::Spec;
use File::Path;
use Data::Dump;

use Statistics::Covid::Utils;

# new method inherited but here we will create one
# to be used as a factory
sub new {
	my ($class, $params) = @_;
	$params = {} unless defined $params;
	$params->{'urls'} = [
		[
			# start a url
			'https://www.bbc.co.uk/indepthtoolkit/data-sets/coronavirus_lookup/json',
			# and its headers if any
			[]
		]
	];
	# initialise our parent class
	my $self = $class->SUPER::new($params);
	if( ! defined $self ){ warn "error, call to $class->new() has failed."; return undef }

	# and do set parameters specific to this particular data provider
	$self->name('BBC'); # <<<< Make sure this is unique over all providers
	$self->datafilesdir(File::Spec->catfile(
		$self->datafilesdir(), # use this as prefix
		'UK', $self->name() # and append a dir hierarchy relevant to this provider
	));
	# initialise this particular data provider
	if( ! $self->init() ){ warn "error, call to init() has failed."; return undef }
	# this will now be BBC obj (not generic)
	return $self
}
# overwriting this from parent
# returns undef on failure or a data id unique on timepoint
# which can be used for saving data to a file or labelling this data
sub create_data_id {
	my $self = $_[0];
	my $datas = $_[1]; # this is an arrayref of [url, data_received_string, data_as_perlvar]

	# this json is idiotic because it's just arrays,
	# 0: location id
	# 1: location name
	# 2: cases
	# 3: population
	# unless [0] is 'UpdatedOn', in which case [1] is 09:00 GMT, 15 March
	# thankfully this update info is last
	my $date = undef;
	my $aurl = $datas->[0]->[0];
	for my $apv (reverse @{$datas->[0]->[2]}){ # we only have 1 triplet and we get the perl-json-var
		if( $apv->[0] eq 'UpdatedOn' ){
			$date = Statistics::Covid::Utils::epoch_stupid_date_format_from_the_BBC_to_DateTime($apv->[1]);
			if( ! defined $date ){
				warn "error, failed to parse date '".$apv->[1]."' from input json data just transfered from url '$aurl'.";
				return undef;
			}
			last;
		}
	}
	if( ! defined $date ){
		warn "error, did not find any date information in input json data just transfered from url '$aurl'.";
		return undef;
	}
	my $dataid = $date->strftime('2020-%m-%dT%H.%M.%S')
		     . '_'
		     . $date->epoch()
	;
	return $dataid
}
# reads from the specified file the data that was
# fetched exactly from the remote provider.
# the input base name (and not an exact filename)
# will be used to create all the necessary filenames
# for data and metadata if exists.



( run in 1.083 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )