10 use Digest::MD5 qw(md5_hex);
13 our $max_rate = 2; # per second
15 # todo: allow to read size details out of svg (maybe in daemon-otter?)
16 # todo: allow scraper method none to handle item vs filename mismatch
18 #print Dumper($libinfo);
20 open DEBUG, ">&STDERR" or die $!;
24 my ($datalog, $output, $url, @xopts) = @_;
25 my @curl = (qw(curl -Ssf -L --proto-redir -all), @xopts);
26 push @curl, '-o', "$output.tmp", $url;
29 my $now = Time::HiRes::time;
30 my $delay = 1./$max_rate - ($now - $last_curl);
31 Time::HiRes::sleep $delay if $delay > 0;
33 # print DEBUG "+ @curl\n";
34 $!=$?=0; my $r = system @curl; die "curl failed ($? $!): @curl" if $r;
35 my $logtime = strftime "%F %T UTC", gmtime time;
36 print $datalog "$logtime: downloaded into $output from $url\n"
38 rename "$output.tmp", "$output" or die "install $output: $!";
41 sub cfg_lookup_1 ($@) {
42 my ($dict, @keys) = @_;
43 #print DEBUG "cfg_lookup_1 ".Dumper($dict, \@keys);
44 foreach my $k (@keys) {
45 confess unless ref $dict eq 'HASH';
47 return undef if !defined $dict;
49 #print DEBUG "cfg_lookup_1 (@keys) => $dict\n";
53 sub mk_cfg_lookup ($$@) {
54 my ($groups, $thisgroup, @keysprefix) = @_;
55 #print DEBUG "mk_cfg_lookup >@keysprefix< ".Dumper($thisgroup);
57 #print DEBUG "from mk_cfg_lookup >@keysprefix< >@_<\n";
58 my $cgroup = $thisgroup;
60 my $got = cfg_lookup_1($cgroup, @keysprefix, @_);
61 return $got if defined $got;
62 my $inherit = cfg_lookup_1($cgroup, qw(inherit));
63 return undef unless $inherit;
64 $cgroup = $groups->{$inherit};
65 confess "$inherit".Dumper($groups,$inherit)."?" unless $cgroup;
70 sub cfg_affixes ($$$) {
71 my ($cfg, $keybase, $middle) = @_;
73 ($cfg->("${keybase}_prefix") // '')
75 .($cfg->("${keybase}_suffix") // '');
78 sub method_none { return sub { } }
79 sub methodlic_none { undef }
81 sub methodlic_wikimedia ($) {
84 These files were all obtained from
85 ${ \ $scraper->('site-title') }
87 They are all available under at least, and distributed here under,
88 ${ \ $scraper->('spdx') }
89 as well as possibly other licences. There is NO WARRANTY.
91 See <file>.download-log for the original URL and download timestamp.
92 The wikitext of the File: page on the wiki is in <file>.wikitext, and
93 contains the authorship and derivation information, as well as
94 information about any alternative licence terms.
96 [ This LICENCE file was generated by media-scraper and should not
101 sub method_wikimedia ($$$) {
102 my ($scraper, $methname) = @_;
103 #print DEBUG "METHOD $methname...\n";
105 my ($lbase, $ldest, $rstem) = @_;
106 my $rfilename = cfg_affixes $scraper, 'filename', $rstem;
107 my $url = cfg_affixes $scraper, 'url', $rfilename;
108 my $wt = "$lbase.wikitext";
109 #print DEBUG "rfilename=$rfilename url=$url .\n";
110 my $datalog = new IO::File "$lbase.download-log", '>>' or die $!;
111 print $datalog "\n" or die $!;
112 run_curl $datalog, $wt, $url;
113 open WT, "$wt" or die $!;
114 my (@lics) = @{ $scraper->('licences') };
115 s/\W/\\$&/g foreach @lics;
116 my $lic1_re = '(?:'.(join '|', @lics).')';
120 if (m{^ \{\{ ($lic1_re) \}\} $}xi ||
121 m{^ \{\{ self\| (?:[^{}]*\|)? ($lic1_re) (?:\|[^{}]*)? \}\} $}xi) {
128 die "\nfile $wt from $url no appropriate licence $lic1_re";
130 my $hash_prefix = '';
131 if ($scraper->('data_url_hashprefix')) {
132 # https://www.mediawiki.org/wiki/Special:MyLanguage/Manual:$wgHashedUploadDirectory
133 md5_hex($rfilename) =~ m{^((.).)} or die;
134 $hash_prefix .= "$2/$1/";
136 my $data_url = cfg_affixes $scraper, 'data_url', $hash_prefix.$rfilename;
137 run_curl $datalog, $ldest, $data_url;
138 close $datalog or die $!;
144 while (@ARGV && $ARGV[0] =~ m/^-/) {
147 if (m/^--offline$/) {
150 die "bad option: \`$_'";
154 my $input = $ARGV[0] // die;
155 $input =~ m/\.toml$/ or die "$input ?";
157 mkdir $basename or $!==EEXIST or die "mkdir $basename: $!";
159 my $parser = TOML::Parser->new();
160 my $libinfo = $parser->parse_file($input);
161 my $groups = $libinfo->{group};
162 my $scraper = sub { $libinfo->{scraper}{$_[0]} };
163 my $method = $scraper->('method');
164 my $method_fn = ${*::}{"method_$method"};
166 my $methodlic_fn = ${*::}{"methodlic_$method"};
167 my $licpath = "$basename/LICENCE";
169 my $method_lictext = $methodlic_fn->($scraper);
170 if (defined $method_lictext) {
171 my $licfile = new IO::File "$licpath.tmp", '>' or die $!;
173 print $licfile <<END, $method_lictext, <<END or die $!;
174 SPDX-License-Identifier: ${ \ $scraper->('spdx') }
175 (applies to the contents of this directory unless otherwise stated)
179 The download was done by media-scraper, controlled by $input.
181 close $licfile or die $!;
184 my $makepath = "$basename/files.make";
185 my $makefile = new IO::File "$makepath.tmp", '>' or die $!;
187 foreach my $groupname (sort keys %$groups) {
188 my $group_dict = $groups->{$groupname};
189 my $gcfg = mk_cfg_lookup($groups, $group_dict);
190 my $method_impl = $method_fn->($scraper, $method);
191 foreach (split(/\n/, $gcfg->('files'))) {
193 next if m/^\#/ || m/^$/;
194 m/^(\S+)\s+(\S+)/ or die "bad line in files: \`$_'";
198 my $lstem = cfg_affixes $gcfg, 'item', $lministem;
199 my $rstem = cfg_affixes $gcfg, 'stem', $rministem;
200 my $lbase = "$basename/$lstem";
201 my $lupstream = "$lbase.svg";
202 my $lprocessed = "$lbase.usvg";
204 print DEBUG "file $lstem ";
206 my $process_colour = sub ($$) {
207 my ($linput, $lprocessed) = @_;
208 print $makefile <<END or die $!;
209 LIBRARY_FILES += $lprocessed
210 $lprocessed: $linput $licpath $input
211 \$(LIBRARY_PROCESS_SVG)
215 my $process_colours = sub {
216 my $colours = $gcfg->('colours');
217 if (!keys %$colours) {
218 $process_colour->($lupstream, $lprocessed, "");
221 foreach my $colour (sort keys %$colours) {
222 my $cspec = $colours->{$colour};
223 my $abbrev = $cspec->{abbrev} or confess;
224 my $ncoloured = $lupstream;
225 $ncoloured =~ s/\.svg$/.coloured$&/;
226 my $outfile = $lprocessed;
227 $ncoloured =~ s/_c/$abbrev/ or confess "$outfile ?";
228 $outfile =~ s/_c/$abbrev/ or confess "$outfile ?";
229 my $coloured = $lupstream;
234 my ($from, $to) = @_;
235 confess if $from =~ m/\W/ || $to =~ m/\W/;
237 $coloured = $ncoloured;
240 print $makefile <<END or die $!;
241 LIBRARY_CLEAN += $ncoloured
242 $ncoloured: $lupstream Makefile $makepath
243 \$(USVG_CMD) -c - <\$< >$cfp
247 my $nfp = "\$@.$ci.tmp";
248 print $makefile <<END or die $!;
249 \$(RECOLOUR_SVG) -f '$from' -t '$to' $cfp >$nfp
254 my %map = %{ $cspec->{map} // { } };
256 my $from = (sort keys %map)[0];
257 my @maybe_cycle = ();
260 push @maybe_cycle, $from;
261 my $to = $map{$from};
262 if (!exists $map{$to}) {
266 if (grep { $_ eq $to } @maybe_cycle) {
271 my $emit_most = sub ($) {
273 $end //= $#maybe_cycle;
274 foreach my $i (@maybe_cycle[0..$end]) {
275 $emitmap->($i, $map{$i});
278 if (defined $cycle) {
279 my $temp = 'abcbfb'; # chosen at random
280 my $aside = $maybe_cycle[-1];
281 $emitmap->($aside, $temp);
282 $emit_most->($#maybe_cycle-1);
283 $emitmap->($temp, $map{$aside});
284 print $makefile <<END or die $!;
285 # cycle: @maybe_cycle
290 delete $map{$_} foreach @maybe_cycle;
293 # inkscape extensions have a tendency to write an empty
294 # file when they don't like their input or arguments
295 print $makefile <<END or die $!;
301 $process_colour->($coloured, $outfile);
305 $process_colours->();
307 if (stat $lupstream) {
308 print DEBUG "already.\n";
311 die "$lupstream $!" unless $!==ENOENT;
314 print DEBUG "missing.\n";
315 warn "offline but $lupstream missing\n";
319 $method_impl->($lbase, $lupstream, $rstem);
321 print DEBUG "done.\n";
325 close $makefile or die $!;
327 if (defined($method_lictext)) {
328 my $cmp = compare("$licpath.tmp", $licpath);
331 rename "$licpath.tmp", $licpath or die $!;
333 remove "$licpath.tmp";
337 rename "$makepath.tmp", $makepath or die $!;