X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=pubmed_search;h=acd327d594710f5d04876de5aba39213a55a379a;hb=a59b2aaed562affa5e86f20ac77b2fee5682edd0;hp=7b3041ed43b4f552bebb1082e9b2c185e8a6910b;hpb=b921b085136311e9c06959396d9cc5c4d57c8e77;p=bin.git diff --git a/pubmed_search b/pubmed_search index 7b3041e..acd327d 100755 --- a/pubmed_search +++ b/pubmed_search @@ -1,9 +1,9 @@ #! /usr/bin/perl -# , and is released +# pubmed_search searches for articles on pubmed, and is released # under the terms of the GPL version 2, or any later version, at your # option. See the file README and COPYING for more information. -# Copyright 2011 by Don Armstrong . -# $Id: perl_script 1825 2011-01-02 01:53:43Z don $ +# Copyright 2011,2017 by Don Armstrong . + use warnings; @@ -59,16 +59,18 @@ Display this manual. use vars qw($DEBUG); my %options = (debug => 0, - help => 0, - man => 0, - color => 1, - org_mode => 0, - ); + help => 0, + man => 0, + color => 1, + org_mode => 0, + abstract => 1, + ); GetOptions(\%options, 'color|c!', 'org_mode|org-mode', 'pmid_only|pmid-only', + 'abstract|abstracts|a!', 'debug|d+','help|h|?','man|m'); pod2usage() if $options{help}; @@ -84,13 +86,26 @@ if (not @ARGV) { pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS; -my $search = Bio::DB::EUtilities->new(-eutil => 'esearch', - -email => 'don@donarmstrong.com', - -db => 'pubmed', - -term => join(' ',@ARGV), - -retmax => 1000, - ); -my @ids = $search->get_ids(); +my $result = + Bio::DB::EUtilities->new(-email => 'don@donarmstrong.com', + -db => 'pubmed', + -term => join(' ',@ARGV), + -retmax => 1000, + -eutil => 'esearch', + ); +my @ids; +eval { + # this warns for everything, so hide the warning. + local $SIG{__WARN__} = sub {}; + @ids = $result->get_ids('pubmed') +}; +if (not @ids) { + print "No results\n"; + exit; +} +if (@ids > 0 and ref($ids[0])) { + @ids = @{$ids[0]}; +} if ($options{org_mode}) { print "* Pubmed search results for ".join(' ',@ARGV)." (".scalar(@ids).")\n"; print " + "; @@ -100,18 +115,21 @@ if ($options{org_mode}) { } print scalar(@ids)." results:\n"; exit 0 unless @ids; -my $esummary = Bio::DB::EUtilities->new(-eutil => 'efetch', - -email => 'don@donarmstrong.com', - -db => 'pubmed', - -id => \@ids - ); +my $raw_xml = Bio::DB::EUtilities->new(-email => 'don@donarmstrong.com', + -db => 'pubmed', + -id => \@ids, + -eutil => 'efetch', + ); use XML::LibXML; -my $xml = XML::LibXML->load_xml(string => $esummary->get_Response()->content()); -for my $article ($xml->findnodes('PubmedArticleSet/PubmedArticle/MedlineCitation')) { - # print $article->toString; - my ($pmid) = $article->findnodes('./PMID'); - my ($title) = $article->findnodes('./Article/ArticleTitle'); - my ($abstract) = $article->findnodes('./Article/Abstract'); +my $xml = XML::LibXML->load_xml(string => $raw_xml->get_Response->content); +print STDERR $xml->toString if $DEBUG; +for my $article ($xml->findnodes(q{//*[local-name()='MedlineCitation']})) { + print STDERR $article->toString if $DEBUG; + my ($pmid) = $article->findnodes(q{./*[local-name()='PMID']}); + my ($title) = $article->findnodes(q{./*[local-name()='Article']}. + q{/*[local-name()='ArticleTitle']}); + my ($abstract) = $article->findnodes(q{./*[local-name()='Article']}. + q{/*[local-name()='Abstract']}); if ($options{org_mode}) { print "** PMID: "; } @@ -122,8 +140,8 @@ for my $article ($xml->findnodes('PubmedArticleSet/PubmedArticle/MedlineCitation print BOLD CYAN if $options{color}; print encode_utf8($title->textContent())."\n"; print RESET if $options{color}; - print BOLD MAGENTA if $options{color}; - if (defined $abstract) { + if (defined $abstract and $options{abstract}) { + print BOLD MAGENTA if $options{color}; if ($options{org_mode}) { print "*** Abstract\n"; }