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 $
17 get_pdf - try to get a pdf
21 get_pdf [options] reference [references]
24 --debug, -d debugging level (Default 0)
25 --help, -h display this help
26 --man, -m display manual
34 The reference is a pmid
36 =item B<--cgi-proxy, -C>
38 Use this cgi proxy style proxy
42 Debug verbosity. (Default 0)
46 Display brief usage information.
65 my %options = (debug => 0,
70 my %REFERENCE_TYPES = (pmid => 'pmid|p');
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');
78 pod2usage() if $options{help};
79 pod2usage({verbose=>2}) if $options{man};
81 $DEBUG = $options{debug};
85 if (not grep {exists $options{$_} and
86 defined $options{$_} and
87 $options{$_}} keys %REFERENCE_TYPES) {
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";
98 push @USAGE_ERRORS,"You must specify at least one reference";
101 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
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};
110 if ($options{pmid}) {
111 my $m = WWW::Mechanize->new(agent => 'Mozilla',cookie_jar => {});
112 for my $pmid (@ARGV) {
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;
119 $url = "http://${url}";
121 $m->get($url) or die "Unable to get $url";
122 my $orig_mech = $m->clone();
124 my @possible_links = $m->find_all_links(text_regex => qr/to\s*read/i);
125 # try to find the other links
126 push @possible_links,
127 grep {my $attr = $_->attrs(); exists $attr->{title} and $attr->{title} =~ qr/Full\s*Text/i}
129 print STDERR map {"article link: ".$_->url_abs()."\n"} @possible_links if $DEBUG;
130 die "No links" unless @possible_links;
134 print "trying ".$possible_links[0]->url()."\n" if $DEBUG;
135 $m->get($possible_links[0]->url()) or
136 die "Unable to follow link";
137 # try to find pdf link
138 my $pdf_m = find_pdf_link($m) or
139 die "Unable to find pdf";
140 my $fh = IO::File->new($pmid.'.pdf','w') or
141 die "Unable to open ${pmid}.pdf for writing: $!";
142 print {$fh} $pdf_m->content or
143 die "Unable to write to ${pmid}.pdf: $!";
145 die "Unable to close ${pmid}.pdf filehandle: $!";
147 shift @possible_links;
148 } while ($@ and @possible_links);
154 print STDERR "$@\n" if $DEBUG;
156 # links2 doesn't like the leading http:// of proxies for some reason
157 exists $options{http_proxy}?('-http-proxy',(map {s{http://}{}; $_} $options{http_proxy})):(),
160 rename('temp.pdf',"${pmid}.pdf") if -e 'temp.pdf';
167 my ($mech,$guess,$call) = @_;
168 $guess = 1 unless defined $guess;
169 $call = 0 unless defined $call;
170 # avoid looping endlessly
171 return undef if $call > 5;
172 my $m = $mech->clone();
173 if ($m->content =~ /select\s*a\s*website\s*below/i) {
174 print STDERR $m->uri() if $DEBUG;
175 print STDERR $m->content() if $DEBUG;
176 my @inputs = $m->find_all_inputs(type => 'hidden',
177 name => q(urls['sd']),
179 return unless @inputs;
180 $m->get($inputs[0]->value);
181 print STDERR $m->content() if $DEBUG;
184 # this is to prioritize the real link at science direct
185 push @possible_links, grep {my $temp = $_->attrs();
186 exists $temp->{title} and $temp->{title} =~ qr/Download\s*PDF/i}
187 $m->find_all_links(text_regex => qr/PDF/i);
188 push @possible_links, grep { $_->url_abs() !~ /_orig(?:in)?=article/} $m->find_all_links(text_regex => qr/PDF/i);
189 push @possible_links, $m->find_all_links(tag_regex => qr/meta/,
190 url_regex => qr/(reprint|\.pdf)/i,
192 push @possible_links, $m->find_all_links(text_regex => qr/pdf/i);
193 push @possible_links,$m->find_all_links(text_regex => qr/manual\s*download/i);
194 print STDERR $m->uri() if $DEBUG;
195 print STDERR $m->content() if $DEBUG;
196 print STDERR map{"possible pdf link: ".$_->url_abs().qq(\n)} @possible_links if $DEBUG;
197 if (not @possible_links and $DEBUG) {
198 print STDERR $m->content();
200 my $best_guess = $possible_links[0] if @possible_links;
201 for my $link (@possible_links) {
202 print STDERR "trying ".$link->url_abs()."..." if $DEBUG;
203 my $r = $m->get($link->url_abs());
204 if ($r->header('Content-Type') =~ /pdf/) {
205 print STDERR "success\n" if $DEBUG;
208 print STDERR "failure; content type ".$r->header('Content-Type')."\n" if $DEBUG;
209 print STDERR $m->content() if $DEBUG;
211 my @sub_frames = $m->find_all_links(tag_regex=>qr/^i?frame$/);
212 for my $frame (@sub_frames) {
213 my $r = $m->get($frame->url_abs());
214 if ($r->header('Content-Type') =~ /pdf/) {
217 my $pdf_m = find_pdf_link($m,
221 if (defined $pdf_m) {
225 # if ($guess and defined $best_guess) {
226 # $m->get($best_guess->url_abs());