chiark / gitweb /
Seems to do correct POST ...
[ypp-sc-tools.db-test.git] / pctb / yppsc-commod-processor
index 5782a5aaa65d515ebeb961f6bd67b1cdf80bbbde..01db528889af367558bb6add9f82f08e7ade862e 100755 (executable)
@@ -219,7 +219,7 @@ sub main__tsv () {
 
 our (%commodmap);
 our ($pctb) = 'http://pctb.ilk.org/';
-our ($ua);
+our ($ua)= LWP::UserAgent->new;
 
 sub load_commodmap() {
     undef %commodmap;
@@ -234,7 +234,6 @@ sub load_commodmap() {
 }
 
 sub refresh_commodmap() {
-    my $ua= LWP::UserAgent->new;
     my $resp= $ua->get("$pctb/commodmap.php?version=2");
     die $resp->status_line unless $resp->is_success;
 
@@ -307,8 +306,8 @@ sub bs_gen_md ($$) {
         $current= $commod{$commod};
        my $l= bs_p($commod,$bs,$sortmul);
        next unless @$l;
-#print STDERR "COMMOD $commod @$l\n";
-
+#print STDERR "COMMOD $commod  has ".scalar(@$l)."\n";
+       $o .= writeint($commodmap{$commod});
        $o .= writeint(scalar @$l);
        foreach my $cs (@$l) {
            $stall= $cs->{Stall};
@@ -316,6 +315,7 @@ sub bs_gen_md ($$) {
            if (!defined $stallix) {
                push @stallmap, $stall;
                $stallmap{$stall}= $stallix= @stallmap;
+#print STDERR "STALL DEF $stallix $stall\n";
            }
            my $qty= $cs->{Qty};
            $qty =~ s/^\>\s*//;
@@ -330,7 +330,7 @@ sub bs_gen_md ($$) {
 
 sub writeint { return pack 'v*', @_; }
 
-sub main__genmarketdata () {
+sub genmarketdata () {
     our $version= '005b';
 
     load_commodmap();
@@ -347,22 +347,37 @@ sub main__genmarketdata () {
            if $missing;
     }    
 
-    my $o='';
-    $o .= bs_gen_md(Buy, -1);
-    $o .= bs_gen_md(Sell,+1);
-
-    printf("$version\n".
-          "%d\n",
-          scalar(@stallmap))
-       or die $!;
-    foreach $stall (@stallmap) { print "$stall\n" or die $!; }
+    my $ob='';
+    $ob .= bs_gen_md(Buy, -1);
+    $ob .= bs_gen_md(Sell,+1);
 
-    print $o or die$!;
+    my $ot= sprintf("$version\n".
+                   "%d\n",
+                   scalar(@stallmap));
+    foreach $stall (@stallmap) { $ot .= "$stall\n"; }
+    return $ot.$ob;
 }
 
+sub main__genmarketdata () {
+    my $o= genmarketdata();
+    print $o or die $!;
+}
 
 sub main__upload () {
-    die "\nUploading not yet implemented, sorry.\n";
+    my $o= genmarketdata();
+    my $url= "$pctb/upload.php";
+    $url= "http://www.chiark.greenend.org.uk/ucgi/~ijackson/check/upload.php";
+    my $content= {
+       'marketdata' => [ undef, "marketdata.gz",
+                         Content_Type => 'application/gzip',
+                         Content => $o
+                         ]
+                     };
+    my $resp= $ua->post("$url", Content => $content,
+                       Content_Type => 'form-data');
+    die $resp->status_line unless $resp->is_success;
+
+    print "[[ ",$resp->content," ]]\n";
 }