From 79b7d51e0d3b8481ac4caa3439e0bd59d821887d Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 8 Jun 2009 02:25:41 +0100 Subject: [PATCH] Can fetch and parse the commodmap --- pctb/yppsc-commod-processor | 117 +++++++++++++++++++++++++++++++++++- 1 file changed, 116 insertions(+), 1 deletion(-) diff --git a/pctb/yppsc-commod-processor b/pctb/yppsc-commod-processor index 0e1a7d0..911242c 100755 --- a/pctb/yppsc-commod-processor +++ b/pctb/yppsc-commod-processor @@ -26,7 +26,11 @@ use strict (qw(vars)); -use Data::Dumper; +use HTTP::Request; +use IO::File; +use POSIX; +use LWP::UserAgent; +use XML::Parser; # $commod{'Hemp'}{Buy|Sell}{'stall'}{Stall} # $commod{'Hemp'}{Buy|Sell}{'stall'}{Price} @@ -216,10 +220,121 @@ sub main__tsv () { } } + +our (%commodmap); +our ($pctb) = 'http://pctb.ilk.org/'; +our ($ua); + +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() { + my $ua= LWP::UserAgent->new; + my $resp= $ua->get("$pctb/commodmap.php"); + die $resp->status_line unless $resp->is_success; + + my $cdata=''; + my $incommodmap=0; + my $intag=''; + my %got; + my $o= new IO::File "#commodmap#.tsv.new",'w' or die $!; + + my $xp= new XML::Parser + (Handlers => + { + Start => sub { + $_=$_[1]; +print STDERR "START [$_] intag=$intag icm=$incommodmap\n"; + if (m/^commodmap$/i) { + $incommodmap++; + undef %got; + } elsif (m/^(?:name|index)$/i) { + $cdata=''; + $intag=lc($_) if $incommodmap; + print STDERR "START RECOGNISED $intag icm=$incommodmap\n"; + } else { + print STDERR "START UNRECOGNISED\n"; + } + }, + End => sub { + $_=$_[1]; +print STDERR "END [$_] intag=$intag icm=$incommodmap\n"; + if (m/^commodmap$/i) { + $incommodmap--; + die unless exists $got{'name'}; + die unless exists $got{'index'}; + die unless $got{'index'} =~ m/^\s*([1-9]\d{0,3})\s*$/; + my $index= $1; + $_= $got{'name'}; + s/^\s+//; s/\s+$//; s/\n/ /g; s/\s+/ /; + die if exists $commodmap{$_}; + $commodmap{$_}= $index; + print $o "$_\t$index\n" or die $!; + } elsif (lc $_ eq $intag) { + $got{$intag}= $cdata; + } + }, + Char => sub { +print STDERR "CHAR [$_[1]] intag=$intag icm=$incommodmap\n"; + $cdata .= $_[1]; + } + }) or die; + my $content= $resp->content; + + # hacks to strip off drivel that seems to have been added! + $content =~ s/^.*\n(\<\?xml)/$1/s; + $content =~ s/\<\/body\>.*//s; + print STDERR "[[[$content]]]\n"; + $xp->parse($content); + close $o or die $!; + rename "#commodmap#.tsv.new","#commodmap#.tsv" or die $!; +} + +# +#sub bs_gen_md ($) { +# my ($bs) = @_; + + +sub main__genmarketdata () { + our $version= '005b'; + + load_commodmap(); + my @missing= grep { !exists $commodmap{$_} } keys %commod; + if (@missing) { + refresh_commodmap(); + my $missing=0; + foreach $commod (sort keys %commod) { + next if exists $commodmap{$commod}; + print STDERR "Unknown commodity \`%s'!\n"; + $missing++; + } + die "$missing unknown commodities. OCR failure?\n" + if $missing; + } + + # foreach $commod (sort keys %commod) { +# next if + + +# bs_gen_md(Buy); +# bs_gen_md(Sell); +} + + sub main__upload () { die "\nUploading not yet implemented, sorry.\n"; } + $mode =~ s/\-//; &{"main__$mode"}; close(STDOUT) or die $!; -- 2.30.2