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($_)->ymd for $res->header('Last-Modified')
34 open my $save, '>', (my $download = "browser-support-$updated.inc.pl");
35 print {$save} pp($data);
38 symlink $download, "$target.new" and rename "$target.new", $target
39 or die "New data at $download not linked: $!";
44 my $source = readline;
46 for ($source) { # cleanup
47 s/\A\(// and s/\);?\s*\Z//; # empty callback
48 next if /^\{/; # valid json
50 # convert seperate variables to hash keys
52 s/^caniuse\.(\w+) *= */"$1":/gm;
55 # fractions not supported by barekey
56 s/(?<=[,{\n]) (\d*\.\d) (?=:['"])/"$1"/gx;
57 # escapes not supported in singlequote
58 s{: *\K'((?:[^\\']+|\\.)*)'}{
66 my $data = from_json($source, {
67 allow_singlequote => 1,
77 convert-caniuse - Turn Javascript data into an equivalent Perl structure
81 curl http://caniuse.com/data.json |
82 tools/convert-caniuse.pl > browser-support.inc.pl
84 tools/convert-caniuse.pl auto