11 require LWP::UserAgent;
14 my $target = 'browser-support.inc.pl';
15 my $source = 'http://caniuse.com/data.json';
17 my $ua = LWP::UserAgent->new;
18 $ua->agent('sheet.shiar.nl/browser');
19 $ua->default_header('If-Modified-Since' => scalar gmtime $_->mtime)
20 for File::stat::stat($target);
22 my $res = $ua->get($source) or die "No data from $source\n";
23 !$res->is_error or die $res->status_line;
24 say $res->status_line;
26 exit 0 if $res->code == 304; # unmodified
28 my $data = decode_json($res->decoded_content) or die "Parse error: $!";
31 return Time::Piece->strptime($_) for $res->header('Last-Modified')
33 $data->{-date} = $_->datetime for $updated || ();
35 my $suffix = $updated && $updated->ymd || time;
36 open my $save, '>', (my $download = "browser-support-$suffix.inc.pl");
37 print {$save} pp($data);
40 symlink $download, "$target.new" and rename "$target.new", $target
41 or die "New data at $download not linked: $!";
46 my $source = readline;
48 for ($source) { # cleanup
49 s/\A\(// and s/\);?\s*\Z//; # empty callback
50 next if /^\{/; # valid json
52 # convert seperate variables to hash keys
54 s/^caniuse\.(\w+) *= */"$1":/gm;
57 # fractions not supported by barekey
58 s/(?<=[,{\n]) (\d*\.\d) (?=:['"])/"$1"/gx;
59 # escapes not supported in singlequote
60 s{: *\K'((?:[^\\']+|\\.)*)'}{
68 my $data = from_json($source, {
69 allow_singlequote => 1,
79 convert-caniuse - Turn Javascript data into an equivalent Perl structure
83 curl http://caniuse.com/data.json |
84 tools/convert-caniuse.pl > browser-support.inc.pl
86 tools/convert-caniuse.pl auto