/[simile]/links/csv2js.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /links/csv2js.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 7 - (show annotations)
Thu Aug 23 13:27:56 2007 UTC (12 years, 11 months ago) by dpavlin
File MIME type: text/plain
File size: 4558 byte(s)
human is now more strict about decoding numbers,
strip_prefix strips common prefix from array
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use Text::CSV_XS;
7 use Text::CSV::Separator qw(get_separator);
8 use Carp qw/confess/;
9 use LWP::Simple;
10 use Number::Bytes::Human qw/format_bytes/;
11
12 use JSON::Syck;
13 use Data::Dump qw/dump/;
14
15 $|++;
16
17 my $csv_path = 'links.csv';
18 my $img_path = 'img';
19 my $first_line_labels = 1;
20 my $split_fields = {
21 label => sub { return split(/,\s*/,$_[0]) },
22 };
23
24 my @char_list = get_separator( path => $csv_path );
25
26 my $separator;
27 if (@char_list) {
28 if (@char_list == 1) {
29 $separator = $char_list[0];
30 } else {
31 $separator = $char_list[0];
32 }
33 } else {
34 die "Couldn't detect the field separator.\n";
35 }
36
37 warn "Separator: $separator\n";
38
39 my $csv_parser = Text::CSV_XS->new({
40 sep_char => $separator,
41 # binary => '1',
42 # always_quote => '1'
43 });
44
45 open my $csv_fh, '<', $csv_path;
46
47 my @dump;
48
49 my @labels;
50
51 my $split_stats;
52
53 my $multiplier = {
54 kb => 1024,
55 mb => 1024 * 1024,
56 gb => 1024 * 1024 * 1024,
57 };
58
59 my $multiplier_regex = join('|',keys %$multiplier);
60
61 sub clean {
62 my @out;
63 foreach my $l ( @_ ) {
64 my $o = $l;
65 $l =~ s/^(['"])(.*)\1/$2/;
66 $l =~ s/^\s+//s;
67 $l =~ s/\s+$//s;
68 push @out, $l;
69 warn "clean '$o' -> '$l'\n" if ( $o ne $l );
70 }
71 return @out if wantarray;
72 return shift @out;
73 }
74
75 sub human {
76 my $s = shift;
77
78 if ( $s =~ m/^\s*(\d+)\s*($multiplier_regex)\s*$/i) {
79 my ( $v, $m ) = ( $1, lc($2) );
80 my $factor = $multiplier->{$m};
81 confess "can't find multiplier $m" unless defined $factor;
82 my $new = format_bytes( $v * $factor, bs => 1024 );
83 warn "## [$s] $v * $factor ($m) -> $new\n";
84 return $new;
85 }
86 return;
87 }
88
89 sub strip_prefix {
90 my @data = @_;
91 my $prefix = shift @data;
92
93 my $p;
94
95 foreach my $d ( @data ) {
96 my $chomp = length($prefix);
97 # find end of common string
98 $chomp-- while(
99 lc(substr( $prefix, 0, $chomp )) ne lc(substr( $d, 0, $chomp ))
100 &&
101 $chomp > 0
102 );
103 if ( $chomp == 0 ) {
104 warn "no common prefix in ",dump( @_ );
105 return @_;
106 }
107
108 my $prefix = substr( $prefix, 0, $chomp );
109 $p->{$prefix}++;
110 }
111 warn "prefixes found = ",dump($p);
112 my @sorted = sort { $p->{$b} <=> $p->{$a} } keys %$p;
113 my $strip = shift @sorted || return @_;
114 warn "longest preffix: '$strip'\n";
115 return map { my $v = $_; $v =~ s/^\Q$strip\E//i; $v; } @_;
116 }
117
118 while (<$csv_fh>) {
119 $csv_parser->parse($_);
120 my @fields = $csv_parser->fields;
121
122 if ( $first_line_labels && $. == 1 ) {
123 @labels = @fields;
124 next;
125 }
126
127 my $h;
128 foreach my $i ( 0 .. $#fields ) {
129 my $l = $labels[$i];
130 die "no label for field $i '$fields[$i]'" unless $l;
131
132 my $v = clean( $fields[$i] );
133 # FIXME reject some values?
134
135 $h->{ $l } = $v;
136
137 if ( my $split = $split_fields->{$l} ) {
138 confess "expected CODE for \$split_files->{$l}" unless ref($split) eq 'CODE';
139
140 my @sv = $split->( $v );
141
142 # warn "sv = ",dump( @sv );
143
144 foreach my $j ( 0 .. $#sv ) {
145
146 my $v = clean( $sv[$j] );
147
148 if ( $j == 0 ) {
149 $h->{ $l . '_short' } = $v;
150 }
151
152 if ( my $human = human( $v ) ) {
153 $h->{ $l . '_' . $j . '_human' } = $human;
154 } else {
155 $h->{ $l . '_' . $j } = $v;
156 }
157
158 $split_stats->{$v}->{pos}->{$j}++;
159 $split_stats->{$v}->{sum}++;
160 push @{ $split_stats->{$v}->{rec}->{$#dump + 1} }, $j;
161 }
162 }
163 }
164 warn "\nRecord #$. ",dump($h),"\n";
165
166 my $id = $h->{id};
167
168 if ( ! defined($id) || $id eq '' ) {
169 warn "## skipped: $_";
170 next;
171 }
172
173 my $url = "http://www.links.hr/photo/big/$id.jpg";
174 my $img_thumb_path = "$img_path/t/$id.jpg";
175 my $img_orig_path = "$img_path/$id.jpg";
176
177 if ( mirror( $url, $img_orig_path ) != RC_NOT_MODIFIED ) {
178 warn "$url -> $img_orig_path\n";
179 }
180 system('convert', '-geometry', '320x200', $img_orig_path, $img_thumb_path ) if ! -e $img_thumb_path;
181
182 $h->{'image-url'} = $img_orig_path;
183 $h->{'image-thumb-url'} = $img_thumb_path;
184
185 push @dump, $h;
186 }
187
188 close $csv_fh;
189
190 foreach my $v ( keys %$split_stats ) {
191
192 if ( $split_stats->{$v}->{sum} == 1 ) {
193 delete( $split_stats->{$v} );
194 next;
195 }
196
197 foreach my $i ( keys %{ $split_stats->{$v}->{rec} } ) {
198 push @{ $dump[ $i ]->{feature} }, $v;
199 }
200 }
201
202 warn "split_stats = ", dump( $split_stats ), "\n";
203
204 my @all = map { $_->{label_0} || die "no label_0 for ",dump($_) } @dump;
205 warn "all = ",dump(@all);
206 my @stripped = strip_prefix( @all );
207 $dump[$_]->{label_0} = $stripped[$_] foreach ( 0 .. $#stripped );
208
209 warn "dump = ", dump( @dump ), "\n";
210
211 print "features: .", join(', .', keys %$split_stats), "\n";
212
213 my $js_path = $csv_path;
214 $js_path =~ s/\.csv/.js/gi;
215
216 open my $fh, '>', $js_path || die "can't open $js_path: $!";
217 print $fh JSON::Syck::Dump( { items => \@dump } );
218 close $fh;
219

Properties

Name Value
svn:executable

  ViewVC Help
Powered by ViewVC 1.1.26