From bd355978c5f8e326de6a4d32b7e8304a8192fd94 Mon Sep 17 00:00:00 2001 From: Don Armstrong Date: Sat, 25 Aug 2007 10:02:17 +0000 Subject: [PATCH] write get_sgf to deal with gobase; rename ps.gz when they exist too --- get_sgf | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++---- sgf_rename | 7 +++++++ 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/get_sgf b/get_sgf index a33eddf..090ba8d 100755 --- a/get_sgf +++ b/get_sgf @@ -63,12 +63,26 @@ Display this manual. use vars qw($DEBUG); +use IO::File; +use WWW::Mechanize; +use File::Temp qw(tempdir); +use Cwd; +use URI; + my %options = (debug => 0, help => 0, man => 0, - ); - -GetOptions(\%options,'debug|d+','help|h|?','man|m'); + figures => '%100', + mode => 'ps', + paper => 'letter', + scale => '420', + ); + +GetOptions(\%options, + 'figures=s','mode=s','extra=s', + 'username=s','password=s', + 'paper=s','scale=s', + 'debug|d+','help|h|?','man|m'); pod2usage() if $options{help}; pod2usage({verbose=>2}) if $options{man}; @@ -76,6 +90,45 @@ pod2usage({verbose=>2}) if $options{man}; $DEBUG = $options{debug}; - +if (-r "$ENV{HOME}/.get_sgf") { + my $sgf_fh = IO::File->new("$ENV{HOME}/.get_sgf") or + die "Unable to open .get_sgf for reading"; + local $/; + my $sgf_file = <$sgf_fh>; + ($options{username},$options{password}) = $sgf_file =~ /^(\w+)\:(\w+)$/; + close $sgf_fh; +} + +my $olddir = getcwd(); + +#my $tmpdir = tempdir(CLEANUP=>1); +#chdir($tmpdir); + +# /games/japan/titles/kisei/31/game-01.sgf + + +my $m = WWW::Mechanize->new(); +$m->credentials($options{username},$options{password}); +my $base = 'http://gobase.org/online/sgf2misc/?fname=/games/japan/titles/kisei/31/game-01.sgf'; +for my $url (@ARGV) { + # strip of leading stuff + $url =~ s/[^=]+=//; + my $fname = $url; + my $uri = URI->new($base); + $uri->query_form(mode => $options{mode}, + fname => $fname, + ); + $m->get("http://gobase.org$fname"); + $m->save_content('temp.sgf'); + $m->get($uri->as_string); + $m->form_number(2); + $m->select('scale',$options{scale}); + $m->field('fig',$options{figures}); + $m->select('paper',$options{paper}); + $m->submit(); + $m->follow_link(url_regex=>qr/\.ps\.gz/); + $m->save_content('temp.ps.gz'); + system("$ENV{HOME}/bin/sgf_rename",'temp.sgf'); +} __END__ diff --git a/sgf_rename b/sgf_rename index 759380c..b6c9ff5 100755 --- a/sgf_rename +++ b/sgf_rename @@ -39,4 +39,11 @@ for my $sgf (@ARGV) { next if $sgf eq $new_file; warn "$new_file already exists" and next if -e $new_file; rename($sgf,$new_file); + my $psgz = $sgf; + $psgz =~ s/\.sgf/.ps.gz/; + if (-e $psgz) { + my $new_psgz = $new_file; + $new_psgz =~ s/\.sgf/.ps.gz/; + rename($psgz,$new_psgz); + } } -- 2.39.5