use
5.006;
our
$VERSION
=
'0.13'
;
sub
new {
my
(
$class
,
$params
) =
@_
;
$params
= {}
unless
defined
$params
;
$params
->{
'urls'
} = [
[
[]
]
];
my
$self
=
$class
->SUPER::new(
$params
);
if
( !
defined
$self
){
warn
"error, call to $class->new() has failed."
;
return
undef
}
$self
->name(
'BBC'
);
$self
->datafilesdir(File::Spec->catfile(
$self
->datafilesdir(),
'UK'
,
$self
->name()
));
if
( !
$self
->init() ){
warn
"error, call to init() has failed."
;
return
undef
}
return
$self
}
sub
create_data_id {
my
$self
=
$_
[0];
my
$datas
=
$_
[1];
my
$date
=
undef
;
my
$aurl
=
$datas
->[0]->[0];
for
my
$apv
(
reverse
@{
$datas
->[0]->[2]}){
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
}
sub
load_fetched_data_from_localfile {
my
$self
=
$_
[0];
my
$inbasename
=
$_
[1];
my
$infile
=
$inbasename
.
'.data.json'
;
my
$infh
;
if
( !
open
(
$infh
,
'<:encoding(UTF-8)'
,
$infile
) ){
warn
"error, failed to open file '$infile' for reading, $!"
;
return
undef
}
my
$json_contents
; {
local
$/=
undef
;
$json_contents
= <
$infh
> }
close
$infh
;
my
$pv
= Statistics::Covid::Utils::json2perl(
$json_contents
);
if
( !
defined
$pv
){
warn
"error, call to "
.
'Statistics::Covid::Utils::json2perl()'
.
" has failed (for data, file '$infile')."
;
return
undef
}
return
[[
'file://'
.
$infile
,
$json_contents
,
$pv
]];
}
sub
create_Datums_from_fetched_data {
my
$self
=
$_
[0];
my
$datas
=
$_
[1];
my
$data
=
$datas
->[0]->[2];
my
@ret
= ();
my
$dateobj
=
undef
;
for
my
$aUKlocation
(
@$data
){
if
(
$aUKlocation
->[0] eq
'UpdatedOn'
){
$dateobj
= Statistics::Covid::Utils::epoch_stupid_date_format_from_the_BBC_to_DateTime(
$aUKlocation
->[1]);
if
( !
defined
$dateobj
){
warn
"error, call to "
.
'Statistics::Covid::Utils::epoch_stupid_date_format_from_the_BBC_to_DateTime()'
.
" has failed for this date-spec '"
.
$aUKlocation
->[1].
"'."
;
return
undef
}
last
}
}
if
( !
defined
$dateobj
){
warn
"error, did not find any date (searched for 'UpdatedOn') in the perl-var data."
;
return
undef
}
my
$ds
=
$self
->name();
for
my
$aUKlocation
(
@$data
){
if
(
$aUKlocation
->[0] eq
'UpdatedOn'
){
next
}
my
$datumobj
= Statistics::Covid::Datum->new({
'id'
=>
$aUKlocation
->[0],
'name'
=>
$aUKlocation
->[1],
'belongsto'
=>
'UK'
,
'confirmed'
=>
$aUKlocation
->[2],
'population'
=>
$aUKlocation
->[3],
'date'
=>
$dateobj
,
'type'
=>
'UK Higher Local Authority'
,
'datasource'
=>
$ds
,
});
if
( !
defined
$datumobj
){
warn
"error, call to "
.
'Statistics::Covid::Datum->new()'
.
" has failed for this data: "
.
join
(
","
,
@$aUKlocation
);
return
undef
}
push
@ret
,
$datumobj
}
return
\
@ret
}
sub
save_fetched_data_to_localfile {
my
$self
=
$_
[0];
my
$datas
=
$_
[1];
my
$outbase
=
$_
[2];
if
( !
defined
$outbase
){
my
$dataid
=
$self
->create_data_id(
$datas
);
if
( !
defined
$dataid
){
warn
"error, call to "
.
'create_data_id()'
.
" has failed."
;
return
0;
}
$outbase
= File::Spec->catfile(
$self
->datafilesdir(),
$dataid
);
}
my
$outfile
=
$outbase
.
'.data.json'
;
if
( ! Statistics::Covid::Utils::save_text_to_localfile(
$datas
->[0]->[1],
$outfile
) ){
warn
"error, call to "
.
'save_text_to_localfile()'
.
" has failed."
;
return
0 }
$outfile
=
$outbase
.
'.data.pl'
;
if
( ! Statistics::Covid::Utils::save_perl_var_to_localfile(
$datas
->[0]->[2],
$outfile
) ){
warn
"error, call to "
.
'save_perl_var_to_localfile()'
.
" has failed."
;
return
0 }
print
"save_fetched_data_to_localfile() : saved data to base '$outbase'.\n"
;
return
1;
}
1;