2020年12月10日木曜日

WHOのCOVID-19データ(2)

WHOのCOVID-19データ(1)からの続き

 少し改良してみた。あいかわらず,汚いプログラムではある。


#!/usr/local/bin/perl
# 12/10/2020 K. Koshigiri
# usage: json.pl 2020/12/05 5
# extract 5 day data from 2020/12/05
# original data = https://covid19.who.int/page-data/table/page-data.json

use Time::Local 'timelocal';
\$tmp = "tmpx";
\$in = "whox";
\$out = "who-dat.csv";
\$jsn = "https://covid19.who.int/page-data/table/page-data.json";

(\$sec, \$min, \$hour) = (0,0,0);
(\$year, \$month, \$day) = split '/', \$ARGV[0];
\$epochtime = timelocal(\$sec, \$min, \$hour, \$day, \$month-1, \$year-1900)+32400;

system("lynx -source \$jsn | fsp -u > \$tmp");

open(OUT, ">\$in");
open(IN, "<\$tmp");
while() {
  s/],/],\n/g;
  print OUT;
}
close(IN);
close(OUT);

open(OUT,">\$out");
print OUT "Date, Country Code, Unix Time, Region, Deaths, Cumulative Deaths, Deaths Last 7 Days, Deaths Last 7 Days Change, Deaths Per Million, Confirmed, Cumulative Confirmed, Cases Last 7 Days,Cases Last 7 Days Change, Cases Per Million\n";

for (\$i=0; \$i<\$ARGV[1]; \$i++) {
  \$ut=\$epochtime+86400*\$i;
  (\$sec, \$min, \$hour, \$mday, \$mon, \$year) = gmtime(\$ut);
  \$year += 1900;
  \$mon += 1;
  print "\$ut\n";
  open(IN, "<\$in");
  while() {
    s/\]//g;
    s/\[//g;
    if(/Country.*"value":"(.*?)"/) {
      $ct=$1;
      print OUT "$year-$mon-$mday,$ct,";
    }
    if(/$ut/) {
      print OUT;
    }
  }
  close(IN);
}
close(OUT);

0 件のコメント: