added očuvan dio posude split into array
[keramika-neolitik] / csv2js.pl
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5 use autodie;
6
7 use Text::CSV qw( csv );
8 use Data::Dump qw( dump );
9 use JSON;
10 use Clone qw( clone );
11 use autodie;
12
13 use utf8;
14
15 my $file = 'keramika.csv';
16 my $aoh = csv (in => $file, headers => "auto");
17
18 open(my $fh, '>', 'keramika.js');
19 my $json;
20
21 foreach my $i ( @$aoh ) {
22         my $source = sprintf( "../ZBIRKA-NEOLITIK/%d.png", $i->{ID} );
23         my $image = sprintf( "image/%d.jpg" , $i->{ID} );
24         my $thumb = sprintf( "thumb/%d.jpg" , $i->{ID} );
25
26         if ( ! -e $source ) {
27                 warn "ERROR: no image $source";
28                 next;
29         }
30
31         #system "convert -trim -geometry 400x $source $image";
32         system qq{convert -fill white -draw "color 1,1 floodfill" -trim -geometry 400x $source $image};
33         #system "convert $image -scale 100x100 -gravity center -background white -extent 100x100 $thumb";
34         system qq{convert $source -fill white -draw "color 1,1 floodfill" -scale 100x100 -gravity center -gravity south -background white -extent 100x100 $thumb};
35         $i->{image} = $image;
36         $i->{thumbnail} = $thumb;
37 #       $i->{label} = $i->{opis_predmeta};
38         $i->{label} = $i->{ID};
39
40         foreach my $name ( keys %$i ) {
41                 if ( length($i->{$name}) == 0 || $i->{$name} =~ m{^(nema|n/a)$} ) {
42                         delete $i->{$name};
43                 }
44         }
45
46         if ( defined($i->{ukras}) ) {
47                 my @ukrasi = split(/\s*,\s+|\s+i\s+/, $i->{ukras});
48                 $i->{ukrasi_array} = [ @ukrasi ];
49         }
50
51         if ( defined($i->{očuvan_dio_posude} ) ) {
52                 my @ocuvan = split(/\s*,\s+|\s+i\s+/, $i->{očuvan_dio_posude});
53                 $i->{ocuvan_array} = [ @ocuvan ];
54         }
55
56         warn "# i = ",dump($i);
57         push @{ $json->{items} }, $i;
58 #       foreach my $n ( 1 .. 1000 ) {
59 #               my $i2 = clone $i;
60 #               $i2->{label} .= "_" . $n;
61 #               push @{ $json->{items} }, $i2;
62 #       }
63 }
64
65 print $fh encode_json( $json );
66 close($fh);
67