X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?a=blobdiff_plain;f=pctb%2Fcommod-results-processor;h=0286ab3fd99a2203584dac2bc665f48748d37a1f;hb=b958771fa67513ba09630953ec91b9d21b3f42f9;hp=fe26f8847348aea3dbaf7e076f05b2ef52269744;hpb=a94d9664c1ba82ed1f0f2b9c22de9ee4138bf3af;p=ypp-sc-tools.web-live.git diff --git a/pctb/commod-results-processor b/pctb/commod-results-processor index fe26f88..0286ab3 100755 --- a/pctb/commod-results-processor +++ b/pctb/commod-results-processor @@ -32,6 +32,8 @@ use POSIX; use LWP::UserAgent; use XML::Parser; +use Commods; + # $commod{'Hemp'}{Buy|Sell}{'stall'}{Stall} # $commod{'Hemp'}{Buy|Sell}{'stall'}{Price} # $commod{'Hemp'}{Buy|Sell}{'stall'}{Qty} @@ -191,49 +193,43 @@ sub main__bestprices () { } } -sub bs_p_tsv ($) { - my ($bs) = @_; +sub bs_p_tsv ($$) { + my ($f, $bs) = @_; if (exists $current->{$bs}{$stall}) { my $si= $current->{$bs}{$stall}; - printf("\t%d\t%s", $si->{Price}, $si->{Qty}) or die $!; + printf($f "\t%d\t%s", $si->{Price}, $si->{Qty}) or die $!; } else { - printf("\t\t") or die $!; + printf($f "\t\t") or die $!; } } -sub main__tsv () { +sub write_tsv ($) { + my ($f) = @_; foreach $commod (sort keys %commod) { $current= $commod{$commod}; my %stalls; map { $stalls{$_}=1; } keys %{ $current->{Buy} }; map { $stalls{$_}=1; } keys %{ $current->{Sell} }; foreach $stall (sort keys %stalls) { - printf("%s\t%s", $commod, $stall) or die $!; - bs_p_tsv(Buy); - bs_p_tsv(Sell); - print("\n") or die $!; + printf($f "%s\t%s", $commod, $stall) or die $!; + bs_p_tsv($f, Buy); + bs_p_tsv($f, Sell); + print($f "\n") or die $!; } } + $f->error and die $!; + $f->flush or die $!; +} + +sub main__tsv () { + write_tsv(\*STDOUT); } -our (%commodmap); our ($pctb) = $ENV{'YPPSC_PCTB_PCTB'}; our ($ua)= LWP::UserAgent->new; -sub load_commodmap() { - undef %commodmap; - my $c= new IO::File "_commodmap.tsv"; - if (!$c) { $!==&ENOENT or die $!; return; } - while (<$c>) { - m/^(\S.*\S)\t(\d+)\n$/ or die "$_"; - $commodmap{$1}= $2; - } - $c->error and die $!; - close $c; -} - sub refresh_commodmap() { die unless $pctb; $pctb =~ s,/*$,,; @@ -245,7 +241,7 @@ sub refresh_commodmap() { my $intag=''; my %got; my $o= new IO::File "_commodmap.tsv.tmp",'w' or die $!; - undef %commodmap; + undef %pctb_commodmap; my $xp= new XML::Parser (Handlers => @@ -275,8 +271,8 @@ sub refresh_commodmap() { my $index= $1; $_= $got{'name'}; s/^\s+//; s/\s+$//; s/\n/ /g; s/\s+/ /; - die "$_ ?" if exists $commodmap{$_}; - $commodmap{$_}= $index; + die "$_ ?" if exists $pctb_commodmap{$_}; + $pctb_commodmap{$_}= $index; print $o "$_\t$index\n" or die $!; } elsif (lc $_ eq $intag) { $got{$intag}= $cdata; @@ -336,8 +332,8 @@ sub bs_gen_md ($$) { my $o= ''; foreach $commod ( - sort { $commodmap{$a} <=> $commodmap{$b} } - grep { exists $commodmap{$_} } + sort { $pctb_commodmap{$a} <=> $pctb_commodmap{$b} } + grep { exists $pctb_commodmap{$_} } keys %commod ) { #print STDERR "COMMOD $commod\n"; @@ -345,7 +341,7 @@ sub bs_gen_md ($$) { my $l= bs_p($commod,$bs,$sortmul); next unless @$l; #print STDERR "COMMOD $commod has ".scalar(@$l)."\n"; - $o .= writeint($commodmap{$commod}); + $o .= writeint($pctb_commodmap{$commod}); $o .= writeint(scalar @$l); foreach my $cs (@$l) { $stall= $cs->{Stall}; @@ -381,14 +377,14 @@ our (%stalltypetoabbrevmap)= qw( sub genmarketdata () { our $version= '005b'; - load_commodmap(); - my @missing= grep { !exists $commodmap{$_} } keys %commod; + parse_pctb_commodmap(); + my @missing= grep { !exists $pctb_commodmap{$_} } keys %commod; if (@missing) { refresh_commodmap(); refresh_newcommods(); my $missing=0; foreach $commod (sort keys %commod) { - next if exists $commodmap{$commod}; + next if exists $pctb_commodmap{$commod}; if (exists $newcommods{$commod}) { printf STDERR "Ignoring new commodity \`%s'!\n", $commod; } else { @@ -437,23 +433,33 @@ sub save_upload_html ($$) { sub gzip ($) { my ($raw) = @_; - my $tf= IO::File::new_tmpfile() or die $!; + my $tf= pipethrough_prep(); print $tf $raw or die $!; - $tf->flush or die $!; - $tf->seek(0,0) or die $!; - my $child= open GZ, "-|"; defined $child or die $!; - if (!$child) { - open STDIN, "<&", $tf; - exec 'gzip'; die $!; - } - my $r; - { undef $/; $!=0; $r= ; } - defined $r or die $!; - close GZ or die "$! $?"; die $? if $?; - return $r; + return pipethrough_run($tf,undef,'gzip','gzip'); +} + +sub main__uploadyaarg () { + my %o; + $o{'ocean'}= $ENV{'YPPSC_OCEAN'} or die; + $o{'island'}= $ENV{'YPPSC_ISLAND'} or die; + $o{'timestamp'}= 10; + get_our_version(\%o, 'client'); + + my $tf= pipethrough_prep(); + write_tsv($tf); + my $oz= pipethrough_run_gzip($tf); + $o{'data'}= [ undef, 'deduped.tsv.gz', + Content_Type => 'application/octet-stream', + Content => $oz ]; + + my $dest= $ENV{'YPPSC_YAARG_SUBMIT'}; + + my $respcontent= cgipostform($ua, "$dest/commod-update-receiver", \%o); + + print $respcontent(); } -sub main__upload () { +sub main__uploadpctb () { my $ocean= $ENV{'YPPSC_OCEAN'}; die unless $ocean; my $island= $ENV{'YPPSC_ISLAND'}; die unless $island; die unless $pctb;