/[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

Diff of /links/csv2js.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1 by dpavlin, Thu Aug 23 09:46:24 2007 UTC revision 5 by dpavlin, Thu Aug 23 12:00:29 2007 UTC
# Line 7  use Text::CSV_XS; Line 7  use Text::CSV_XS;
7  use Text::CSV::Separator qw(get_separator);  use Text::CSV::Separator qw(get_separator);
8  use Carp qw/confess/;  use Carp qw/confess/;
9  use LWP::Simple;  use LWP::Simple;
10  use Imager;  use Number::Bytes::Human qw/format_bytes/;
11    
12  use JSON::Syck;  use JSON::Syck;
13  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
# Line 52  my $split_stats; Line 52  my $split_stats;
52    
53  my $multiplier = {  my $multiplier = {
54          kb => 1024,          kb => 1024,
55          mb => 1024 * 1204,          mb => 1024 * 1024,
56          gb => 1024 * 1024 * 1024,          gb => 1024 * 1024 * 1024,
57  };  };
58    
# Line 72  sub clean { Line 72  sub clean {
72          return shift @out;          return shift @out;
73  }  }
74    
75    sub human {
76            my $s = shift;
77    
78            if ( $s =~ m/(\d+)\s*($multiplier_regex)/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  while (<$csv_fh>) {  while (<$csv_fh>) {
90      $csv_parser->parse($_);      $csv_parser->parse($_);
91      my @fields = $csv_parser->fields;      my @fields = $csv_parser->fields;
# Line 106  while (<$csv_fh>) { Line 120  while (<$csv_fh>) {
120                                          $h->{ $l . '_short' } = $v;                                          $h->{ $l . '_short' } = $v;
121                                  }                                  }
122    
123                                  if ( $v =~ m/(\d+)\s*($multiplier_regex)/) {                                  if ( my $human = human( $v ) ) {
124                                          my $new = $1 * $multiplier_regex->{$2};                                          $h->{ $l . '_' . $j . '_human' } = $human;
125                                          warn "## $v -> $new\n";                                  } else {
126                                          $v = $new;                                          $h->{ $l . '_' . $j } = $v;
127                                  }                                  }
128    
129                                  $split_stats->{$v}->{$j}++;                                  $split_stats->{$v}->{$j}++;
# Line 134  while (<$csv_fh>) { Line 148  while (<$csv_fh>) {
148          if ( mirror( $url, $img_orig_path ) != RC_NOT_MODIFIED ) {          if ( mirror( $url, $img_orig_path ) != RC_NOT_MODIFIED ) {
149                  warn "$url -> $img_orig_path\n";                  warn "$url -> $img_orig_path\n";
150          }          }
151          system('convert', '-geometry', '320x200', $img_orig_path, $img_thumb_path ) if -e $img_thumb_path;          system('convert', '-geometry', '320x200', $img_orig_path, $img_thumb_path ) if ! -e $img_thumb_path;
152    
153          $h->{'image-url'} = $img_orig_path;          $h->{'image-url'} = $img_orig_path;
154          $h->{'image-thumb-url'} = $img_thumb_path;          $h->{'image-thumb-url'} = $img_thumb_path;

Legend:
Removed from v.1  
changed lines
  Added in v.5

  ViewVC Help
Powered by ViewVC 1.1.26