]> git.donarmstrong.com Git - bin.git/blobdiff - get_sgf
add mutt alias which executes neomutt if that exists
[bin.git] / get_sgf
diff --git a/get_sgf b/get_sgf
index 090ba8dcce4f199d7d0b48571db0e6ca2f8a5fe6..f978b81ed5c4a1321158bb88df40a98b58e37ab1 100755 (executable)
--- a/get_sgf
+++ b/get_sgf
@@ -113,6 +113,8 @@ my $base = 'http://gobase.org/online/sgf2misc/?fname=/games/japan/titles/kisei/3
 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},
@@ -121,13 +123,29 @@ for my $url (@ARGV) {
      $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();
-     $m->follow_link(url_regex=>qr/\.ps\.gz/);
-     $m->save_content('temp.ps.gz');
+     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');
 }