Skip to content

Commit

Permalink
Update foodbanks
Browse files Browse the repository at this point in the history
  • Loading branch information
slowe committed Jul 24, 2024
1 parent 5577552 commit ec902c5
Show file tree
Hide file tree
Showing 7 changed files with 683 additions and 666 deletions.
16 changes: 16 additions & 0 deletions pipelines/lib/lib.pl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use strict;
use utf8;
use JSON::XS;
use POSIX qw(strftime);
use open qw(:std :encoding(UTF-8));
binmode(STDOUT, ":utf8");
binmode(STDIN, ":encoding(UTF-8)");
Expand Down Expand Up @@ -60,6 +61,21 @@ sub makeDir {
}
}

sub updateCreationTimestamp {
my $file = shift;
my(@lines,$fh,$i,$dt);
open($fh,$file);
@lines = <$fh>;
close($fh);
$dt = strftime("%FT%H:%M", localtime);
for($i = 0; $i < @lines ; $i++){
$lines[$i] =~ s/^(updated: )(.*)/$1$dt/;
}
msg("Updating timestamp in <cyan>$file<none>\n");
open($fh,">",$file);
print $fh @lines;
close($fh);
}

# Version 1.3
sub ParseCSV {
Expand Down
19 changes: 11 additions & 8 deletions pipelines/society/foodbanks.pl
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,28 @@ BEGIN
# Get the real base directory for this script
($basedir, $path) = abs_path($0) =~ m{(.*/)?([^/]+)$};
}
require $basedir."../util.pl";
use lib $basedir."../lib/"; # Custom functions
require "lib.pl";

my $hexfile = $basedir."../../src/_data/hexjson/constituencies.hexjson";
my $hexfile = $basedir."../../src/_data/hexjson/uk-constituencies-2023.hexjson";
my $fbkfile = $basedir."../../raw-data/society/foodbanks.json";
my $outfile = $basedir."../../src/_data/sources/society/foodbanks.csv";
my $outfile = $basedir."../../src/themes/society/food-banks/_data/foodbanks.csv";

# Get a new copy of the food bank data
SaveFromURL("https://www.givefood.org.uk/api/2/foodbanks/",$fbkfile);

my (@items,$i,$lookup,$pcon,$totals,$name);

# Read in the HexJSON and build the lookup
my $hexjson = loadJSON($hexfile);
my $hexjson = LoadJSON($hexfile);
@items = keys(%{$hexjson->{'hexes'}});
$lookup = {};
for($i = 0; $i < @items; $i++){
$lookup->{$hexjson->{'hexes'}{$items[$i]}{'n'}} = {'id'=>$items[$i],'region'=>$hexjson->{'hexes'}{$items[$i]}{'a'},'total'=>0};
}

# Read in the Food Banks data
my $json = loadJSON($fbkfile);
my $json = LoadJSON($fbkfile);
@items = @{$json};
$totals = {};
for($i = 0; $i < @items; $i++){
Expand All @@ -48,12 +51,12 @@ BEGIN
}


open(my $fh,">",$outfile) || error("Unable to save to <cyan>$outfile<none>\n");
open(my $fh,">:utf8",$outfile) || error("Unable to save to <cyan>$outfile<none>\n");
print $fh "Code,Constituency,Region,Food banks\n";
foreach $name (sort{ $lookup->{$a}{'id'} cmp $lookup->{$b}{'id'} }(keys(%{$lookup}))){
print $fh $lookup->{$name}{'id'}.",".($name =~ /\,/ ? "\"$name\"" : $name).",".($lookup->{$name}{'region'}).",".($lookup->{$name}{'total'})."\n";
print $fh ($lookup->{$name}{'id'}||"").",".($name =~ /\,/ ? "\"$name\"" : $name).",".($lookup->{$name}{'region'}||"").",".($lookup->{$name}{'total'}||"0")."\n";
}
close($fh);
msg("Saved to <cyan>$outfile<none>\n");


updateCreationTimestamp($basedir."../../src/themes/society/food-banks/index.njk");
Loading

0 comments on commit ec902c5

Please sign in to comment.