From 0bf15a019e9201754f43b7a874b8cc02c96d2130 Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Fri, 10 Dec 2010 01:37:19 +0100 Subject: [PATCH] browser: automatic retrieval of caniuse data Take care to negotiate If-Modified-Since to minimise downloads. --- tools/convert-caniuse.pl | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/tools/convert-caniuse.pl b/tools/convert-caniuse.pl index f013baf..5f619b1 100755 --- a/tools/convert-caniuse.pl +++ b/tools/convert-caniuse.pl @@ -4,9 +4,42 @@ use strict; use warnings; use Data::Dump 'pp'; -use File::Slurp 'read_file'; use JSON; +if (@ARGV) { + require Time::Piece; + require LWP::UserAgent; + require File::stat; + + my $target = 'browser-support.inc.pl'; + my $source = 'http://caniuse.com/data.json'; + + my $ua = LWP::UserAgent->new; + $ua->agent('sheet.shiar.nl/browser'); + $ua->default_header('If-Modified-Since' => scalar gmtime $_->mtime) + for File::stat::stat($target); + + my $res = $ua->get($source) or die "No data from $source\n"; + !$res->is_error or die $res->status_line; + say $res->status_line; + + exit 0 if $res->code == 304; # unmodified + + my $data = decode_json($res->decoded_content) or die "Parse error: $!"; + my $updated = eval { + s/ GMT$//, + return Time::Piece->strptime($_)->ymd for $res->header('Last-Modified') + } || time; + + open my $save, '>', (my $download = "browser-support-$updated.inc.pl"); + print {$save} pp($data); + close $save; + + symlink $download, "$target.new" and rename "$target.new", $target + or die "New data at $download not linked: $!"; + exit 0; +} + local $/; # slurp my $source = readline; @@ -45,6 +78,8 @@ convert-caniuse - Turn Javascript data into an equivalent Perl structure =head1 SYNOPSIS - curl http://caniuse.com/jsonp.php | + curl http://caniuse.com/data.json | tools/convert-caniuse.pl > browser-support.inc.pl + tools/convert-caniuse.pl auto + -- 2.30.0