#! /usr/bin/perl # get_sgf downloads sgf from gobase, and is released # under the terms of the GPL version 2, or any later version, at your # option. See the file README and COPYING for more information. # Copyright 2006 by Don Armstrong . # $Id: perl_script 495 2006-08-10 08:02:01Z don $ use warnings; use strict; use Getopt::Long; use Pod::Usage; =head1 NAME get_sgf - downloads sgf and ps.gz from gobase =head1 SYNOPSIS [options] Options: --debug, -d debugging level (Default 0) --help, -h display this help --man, -m display manual =head1 OPTIONS =over =item B<--figures> Figures to make, defaults to '%100' =item B<--mode> Mode to use, defaults to 'ps' =item B<--extra> Other parameters; defaults to 'paper=letter;size=420' =item B<--debug, -d> Debug verbosity. (Default 0) =item B<--help, -h> Display brief useage information. =item B<--man, -m> Display this manual. =back =head1 EXAMPLES =cut 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, 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}; $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/[^=]+=//; $url =~ s{(?:http://)?(?:gobase\.org)?}{}; $url = qq(/$url) unless $url =~ m{^/}; 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->follow_link(text_regex=>qr/PostScript/); $m->form_number(2); $m->select('scale',$options{scale}); $m->field('fig',$options{figures}); $m->select('paper',$options{paper}); $m->submit(); my $content; my $state; my $sleep; VALID_LOOP: { do { $state = $m->clone; $m->follow_link(url_regex=>qr/\.ps\.gz/); if ($m->content =~ /^\%\!PS/){ $m->save_content('temp.ps'); last VALID_LOOP; } $sleep = 240+int(rand()*120); print STDERR "Failure to download content, waiting $sleep seconds\n"; $m = $state; } while (sleep $sleep); } system('gzip','temp.ps'); system("$ENV{HOME}/bin/sgf_rename",'temp.sgf'); } __END__