]> git.donarmstrong.com Git - bin.git/blobdiff - get_pdf
add postfix grep
[bin.git] / get_pdf
diff --git a/get_pdf b/get_pdf
index 74f62dc6998e1f6ec494b4411eac95f9495320d8..2aa84169aaa0afe672fd28b00ea69a6fae6597f3 100755 (executable)
--- a/get_pdf
+++ b/get_pdf
@@ -65,12 +65,14 @@ use WWW::Mechanize;
 my %options = (debug           => 0,
               help            => 0,
               man             => 0,
+               use_links       => 1,
               );
 
 my %REFERENCE_TYPES = (pmid => 'pmid|p');
 
 GetOptions(\%options,
           values %REFERENCE_TYPES,
+           'use_links|use-links!',
           'cgi_proxy|cgi-proxy|C=s',
           'http_proxy|http-proxy|H=s',
           'debug|d+','help|h|?','man|m');
@@ -108,7 +110,7 @@ if (exists $options{http_proxy}) {
 }
 
 if ($options{pmid}) {
-    my $m = WWW::Mechanize->new();
+    my $m = WWW::Mechanize->new(agent => 'Mozilla',cookie_jar => {});
     for my $pmid (@ARGV) {
        $pmid =~ s/\D//g;
        next unless length $pmid;
@@ -152,12 +154,15 @@ if ($options{pmid}) {
        };
        if ($@) {
            print STDERR "$@\n" if $DEBUG;
-           system('links2',
-                  exists $options{http_proxy}?('-http-proxy',$options{http_proxy}):(),
-                  $url
-                 ) == 0 or next;
-           rename('temp.pdf',"${pmid}.pdf") if -e 'temp.pdf';
-       }
+            if ($options{use_links}) {
+                system('links2',
+                       # links2 doesn't like the leading http:// of proxies for some reason
+                       exists $options{http_proxy}?('-http-proxy',(map {s{http://}{}; $_} $options{http_proxy})):(),
+                       $url
+                      ) == 0 or next;
+                rename('temp.pdf',"${pmid}.pdf") if -e 'temp.pdf';
+            }
+        }
     }
 }
 
@@ -205,6 +210,7 @@ sub find_pdf_link {
            return $m;
        }
        print STDERR "failure; content type ".$r->header('Content-Type')."\n" if $DEBUG;
+       print STDERR $m->content() if $DEBUG;
     }
     my @sub_frames = $m->find_all_links(tag_regex=>qr/^i?frame$/);
     for my $frame (@sub_frames) {