]> git.donarmstrong.com Git - bin.git/blob - get_pdf
add get_pdf function
[bin.git] / get_pdf
1 #! /usr/bin/perl
2 # get_pdf tries to get pdfs, 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 2008 by Don Armstrong <don@donarmstrong.com>.
6 # $Id: perl_script 1352 2009-01-25 02:04:38Z don $
7
8
9 use warnings;
10 use strict;
11
12 use Getopt::Long;
13 use Pod::Usage;
14
15 =head1 NAME
16
17 get_pdf - try to get a pdf
18
19 =head1 SYNOPSIS
20
21 get_pdf [options] reference [references]
22
23  Options:
24   --debug, -d debugging level (Default 0)
25   --help, -h display this help
26   --man, -m display manual
27
28 =head1 OPTIONS
29
30 =over
31
32 =item B<--pmid, -p>
33
34 The reference is a pmid
35
36 =item B<--cgi-proxy, -C>
37
38 Use this cgi proxy style proxy
39
40 =item B<--debug, -d>
41
42 Debug verbosity. (Default 0)
43
44 =item B<--help, -h>
45
46 Display brief usage information.
47
48 =item B<--man, -m>
49
50 Display this manual.
51
52 =back
53
54 =head1 EXAMPLES
55
56
57 =cut
58
59
60 use vars qw($DEBUG);
61
62 use Cwd;
63 use WWW::Mechanize;
64
65 my %options = (debug           => 0,
66                help            => 0,
67                man             => 0,
68                );
69
70 my %REFERENCE_TYPES = (pmid => 'pmid|p');
71
72 GetOptions(\%options,
73            values %REFERENCE_TYPES,
74            'cgi_proxy|cgi-proxy|C=s',
75            'http_proxy|http-proxy|H=s',
76            'debug|d+','help|h|?','man|m');
77
78 pod2usage() if $options{help};
79 pod2usage({verbose=>2}) if $options{man};
80
81 $DEBUG = $options{debug};
82
83
84
85 if (not grep {exists $options{$_} and
86                   defined $options{$_} and
87                       $options{$_}} keys %REFERENCE_TYPES) {
88     $options{pmid} = 1;
89 }
90 my @USAGE_ERRORS;
91 if (grep {exists $options{$_}
92               and defined $options{$_}
93                   and $options{$_}} keys %REFERENCE_TYPES > 1) {
94     push @USAGE_ERRORS,"You can only specify exactly one of the ".(map { "--$_"} keys %REFERENCE_TYPES)." options";
95 }
96
97 if (not @ARGV) {
98     push @USAGE_ERRORS,"You must specify at least one reference";
99 }
100
101 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
102
103
104 if (exists $options{http_proxy}) {
105     $ENV{http_proxy} = $options{http_proxy};
106     $ENV{HTTP_PROXY} = $options{http_proxy};
107     $ENV{CGI_HTTP_PROXY} = $options{http_proxy};
108 }
109
110 if ($options{pmid}) {
111     my $m = WWW::Mechanize->new();
112     for my $pmid (@ARGV) {
113         $pmid =~ s/\D//g;
114         next unless length $pmid;
115         my $url = "www.ncbi.nlm.nih.gov/entrez/query.fcgi?cmd=Retrieve&db=PubMed&list_uids=${pmid}&dopt=Abstract";
116         if (exists $options{cgi_proxy}) {
117             $url = $options{cgi_proxy}.$url;
118         }
119         $url = "http://${url}";
120         eval {
121             $m->get($url) or die "Unable to get $url";
122             $m->follow_link(text_regex => qr/to\s*read/i) or
123                 die "Unable to follow link";
124             # try to find pdf link
125             my $pdf_m = find_pdf_link($m) or
126                 die "Unable to find pdf";
127             my $fh = IO::File->new($pmid.'.pdf','w') or
128                 die "Unable to open ${pmid}.pdf for writing: $!";
129             print {$fh} $pdf_m->content or
130                 die "Unable to write to ${pmid}.pdf: $!";
131             close $fh or
132                 die "Unable to close ${pmid}.pdf filehandle: $!";
133         };
134         if ($@) {
135             print STDERR "$@\n" if $DEBUG;
136             ## system('links',
137             ##     exists $options{http_proxy}?('-http-proxy',$options{http_proxy}):(),
138             ##     $url
139             ##    ) == 0 or next;
140             ## rename('temp.pdf',"${pmid}.pdf") if -e 'temp.pdf';
141         }
142     }
143 }
144
145
146 sub find_pdf_link {
147     my ($mech,$guess,$call) = @_;
148     $guess = 1 unless defined $guess;
149     $call = 0 unless defined $call;
150     # avoid looping endlessly
151     return undef if $call > 5;
152     my $m = $mech->clone();
153     if ($m->content =~ /select\s*a\s*website\s*below/i) {
154         print STDERR $m->uri() if $DEBUG;
155         print STDERR $m->content() if $DEBUG;
156         my @inputs = $m->find_all_inputs(type => 'hidden',
157                                          name => q(urls['sd']),
158                                         );
159         return unless @inputs;
160         $m->get($inputs[0]->value);
161         print STDERR $m->content() if $DEBUG;
162     }
163     my @possible_links = $m->find_all_links(text_regex => qr/pdf/i);
164     push @possible_links,$m->find_all_links(text_regex => qr/manual\s*download/i);
165     print STDERR map{$_->url,qq(\n)} @possible_links if $DEBUG;
166     if (not @possible_links and $DEBUG) {
167         print STDERR $m->content();
168     }
169     my $best_guess = $possible_links[0] if @possible_links;
170     for my $link (@possible_links) {
171         my $r = $m->get($link->url());
172         if ($r->header('Content-Type') =~ /pdf/) {
173             return $m;
174         }
175     }
176     my @sub_frames = $m->find_all_links(tag_regex=>qr/^i?frame$/);
177     for my $frame (@sub_frames) {
178         $m->get($frame->url());
179         my $pdf_m = find_pdf_link($m,
180                                   0,
181                                   $call+1,
182                                  );
183         if (defined $pdf_m) {
184             return $pdf_m;
185         }
186     }
187     if ($guess and defined $best_guess) {
188         $m->get($best_guess->url());
189         return $m;
190     }
191     return undef;
192 }
193
194
195 __END__