chiark / gitweb /
wip commod-update-receiver and new uploader
[ypp-sc-tools.main.git] / pctb / commod-results-processor
index 3ea53be79f49b73a36b9be2f592a307a05b2833e..4beb6d066a59af51b46591b70d7158941f26a9e4 100755 (executable)
@@ -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}
@@ -217,23 +219,10 @@ sub main__tsv () {
 }
 
 
-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,/*$,,;
@@ -244,8 +233,8 @@ sub refresh_commodmap() {
     my $incommodmap=0;
     my $intag='';
     my %got;
-    my $o= new IO::File "#commodmap#.tsv.tmp",'w' or die $!;
-    undef %commodmap;
+    my $o= new IO::File "_commodmap.tsv.tmp",'w' or die $!;
+    undef %pctb_commodmap;
 
     my $xp= new XML::Parser
        (Handlers =>
@@ -275,8 +264,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;
@@ -292,7 +281,7 @@ sub refresh_commodmap() {
 #    print STDERR "[[[$content]]]\n";
     $xp->parse($content);
     close $o or die $!;
-    rename "#commodmap#.tsv.tmp","#commodmap#.tsv" or die $!;
+    rename "_commodmap.tsv.tmp","_commodmap.tsv" or die $!;
 }
 
 our %newcommods;
@@ -317,7 +306,7 @@ sub refresh_newcommods() {
     my $rsync= $ENV{'YPPSC_PCTB_RSYNC'};
     $rsync= 'rsync' if !defined $rsync;
 
-    my $local= "#master-newcommods#.txt";
+    my $local= "_master-newcommods.txt";
     my $src= $ENV{'YPPSC_PCTB_DICT_UPDATE'};
     if ($src) {
        my $remote= "$src/master-newcommods.txt";
@@ -325,7 +314,7 @@ sub refresh_newcommods() {
        die "$? $!" if $! or $?;
     }
     read_newcommods($local);
-    read_newcommods('#local-newcommods#.txt');
+    read_newcommods('_local-newcommods.txt');
 }
 
 our (%stallmap, @stallmap);
@@ -336,8 +325,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 +334,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 +370,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 {
@@ -430,12 +419,22 @@ sub main__genmarketdata () {
 
 sub save_upload_html ($$) {
     my ($which, $resptxt) = @_;
-    open R, ">./#upload-$which#.html" or die $!;
+    open R, ">./_upload-$which.html" or die $!;
     print R $resptxt or die $!;
     close R or die $!;
 }
 
-sub main__upload () {
+sub main__upload_yaarg () {
+    my $ocean= $ENV{'YPPSC_OCEAN'};  die unless $ocean;
+    my $island= $ENV{'YPPSC_ISLAND'};  die unless $island;
+    my $content= {
+       'data' => [ undef, 'deduped.tsv.gz',
+                   Content_Type => 'application/octet-stream',
+                   Content => '' 
+]};
+}
+
+sub main__upload_pctb () {
     my $ocean= $ENV{'YPPSC_OCEAN'};  die unless $ocean;
     my $island= $ENV{'YPPSC_ISLAND'};  die unless $island;
     die unless $pctb;
@@ -449,7 +448,7 @@ sub main__upload () {
                          ]
                      };
 
-    print STDERR "Uploading data to server...\n";
+    print STDERR "Uploading data to $pctb...\n";
 
     my $resp= $ua->post("$url", Content => $content,
                        Content_Type => 'form-data');
@@ -458,7 +457,7 @@ sub main__upload () {
     my $resptxt= $resp->content();
     save_upload_html('1', $resptxt);
 
-    open R, ">./#upload-1#.html" or die $!;
+    open R, ">./_upload-1.html" or die $!;
     print R $resptxt or die $!;
     close R or die $!;
 
@@ -514,7 +513,7 @@ sub main__upload () {
     save_upload_html('3', $resptxt);
 
     print "\n" or die $!;
-    system('w3m -T text/html -dump < \#upload-3#.html');
+    system('w3m -T text/html -dump < _upload-3.html');
     
     print "\n" or die $!;
 }