chiark / gitweb /
Actually compress marketdata.gz (!)
[ypp-sc-tools.db-test.git] / pctb / commod-results-processor
index cea64e5190318e437c5ad55ff6368cfe0f8a68ed..fe26f8847348aea3dbaf7e076f05b2ef52269744 100755 (executable)
@@ -224,7 +224,7 @@ our ($ua)= LWP::UserAgent->new;
 
 sub load_commodmap() {
     undef %commodmap;
-    my $c= new IO::File "#commodmap#.tsv";
+    my $c= new IO::File "_commodmap.tsv";
     if (!$c) { $!==&ENOENT or die $!; return; }
     while (<$c>) {
        m/^(\S.*\S)\t(\d+)\n$/ or die "$_";
@@ -244,7 +244,7 @@ sub refresh_commodmap() {
     my $incommodmap=0;
     my $intag='';
     my %got;
-    my $o= new IO::File "#commodmap#.tsv.new",'w' or die $!;
+    my $o= new IO::File "_commodmap.tsv.tmp",'w' or die $!;
     undef %commodmap;
 
     my $xp= new XML::Parser
@@ -292,7 +292,7 @@ sub refresh_commodmap() {
 #    print STDERR "[[[$content]]]\n";
     $xp->parse($content);
     close $o or die $!;
-    rename "#commodmap#.tsv.new","#commodmap#.tsv" or die $!;
+    rename "_commodmap.tsv.tmp","_commodmap.tsv" or die $!;
 }
 
 our %newcommods;
@@ -317,7 +317,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 +325,7 @@ sub refresh_newcommods() {
        die "$? $!" if $! or $?;
     }
     read_newcommods($local);
-    read_newcommods('#local-newcommods#.txt');
+    read_newcommods('_local-newcommods.txt');
 }
 
 our (%stallmap, @stallmap);
@@ -430,11 +430,29 @@ 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 gzip ($) {
+    my ($raw) = @_;
+    my $tf= IO::File::new_tmpfile() or die $!;
+    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= <GZ>; }
+    defined $r or die $!;
+    close GZ or die "$! $?";  die $? if $?;
+    return $r;
+}
+
 sub main__upload () {
     my $ocean= $ENV{'YPPSC_OCEAN'};  die unless $ocean;
     my $island= $ENV{'YPPSC_ISLAND'};  die unless $island;
@@ -445,11 +463,11 @@ sub main__upload () {
     my $content= {
        'marketdata' => [ undef, "marketdata.gz",
                          Content_Type => 'application/gzip',
-                         Content => $o
+                         Content => gzip($o),
                          ]
                      };
 
-    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 +476,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 +532,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 $!;
 }