2 # get_sgf downloads sgf from gobase, and is released
3 # under the terms of the GPL version 2, or any later version, at your
4 # option. See the file README and COPYING for more information.
5 # Copyright 2006 by Don Armstrong <don@donarmstrong.com>.
6 # $Id: perl_script 495 2006-08-10 08:02:01Z don $
17 get_sgf - downloads sgf and ps.gz from gobase
24 --debug, -d debugging level (Default 0)
25 --help, -h display this help
26 --man, -m display manual
34 Figures to make, defaults to '%100'
38 Mode to use, defaults to 'ps'
42 Other parameters; defaults to 'paper=letter;size=420'
46 Debug verbosity. (Default 0)
50 Display brief useage information.
68 use File::Temp qw(tempdir);
72 my %options = (debug => 0,
82 'figures=s','mode=s','extra=s',
83 'username=s','password=s',
85 'debug|d+','help|h|?','man|m');
87 pod2usage() if $options{help};
88 pod2usage({verbose=>2}) if $options{man};
90 $DEBUG = $options{debug};
93 if (-r "$ENV{HOME}/.get_sgf") {
94 my $sgf_fh = IO::File->new("$ENV{HOME}/.get_sgf") or
95 die "Unable to open .get_sgf for reading";
97 my $sgf_file = <$sgf_fh>;
98 ($options{username},$options{password}) = $sgf_file =~ /^(\w+)\:(\w+)$/;
102 my $olddir = getcwd();
104 #my $tmpdir = tempdir(CLEANUP=>1);
107 # /games/japan/titles/kisei/31/game-01.sgf
110 my $m = WWW::Mechanize->new();
111 $m->credentials($options{username},$options{password});
112 my $base = 'http://gobase.org/online/sgf2misc/?fname=/games/japan/titles/kisei/31/game-01.sgf';
113 for my $url (@ARGV) {
114 # strip of leading stuff
116 $url =~ s{(?:http://)?(?:gobase\.org)?}{};
117 $url = qq(/$url) unless $url =~ m{^/};
119 my $uri = URI->new($base);
120 $uri->query_form(mode => $options{mode},
123 $m->get("http://gobase.org$fname");
124 $m->save_content('temp.sgf');
125 $m->get($uri->as_string);
127 $m->select('scale',$options{scale});
128 $m->field('fig',$options{figures});
129 $m->select('paper',$options{paper});
137 $m->follow_link(url_regex=>qr/\.ps\.gz/);
138 if ($m->content =~ /^\%\!PS/){
139 $m->save_content('temp.ps');
142 $sleep = 240+int(rand()*120);
143 print STDERR "Failure to download content, waiting $sleep seconds\n";
145 } while (sleep $sleep);
147 system('gzip','temp.ps');
148 system("$ENV{HOME}/bin/sgf_rename",'temp.sgf');