Fallback to posting HTTP URLs if XML fetch fails in entrez.py.
authorW. Trevor King <wking@drexel.edu>
Sun, 17 Apr 2011 01:53:11 +0000 (21:53 -0400)
committerW. Trevor King <wking@drexel.edu>
Sun, 17 Apr 2011 01:53:11 +0000 (21:53 -0400)
posts/entrez/entrez.py

index 81ffdb4214ee2b25d0f3f5c9fb82a71e6d8a7f4c..11356aa188c43c051b8b88fde7dcfb1b258d866e 100755 (executable)
@@ -84,6 +84,7 @@ __version__ = '0.2'
 EUTILS_WSDL_URL = 'http://eutils.ncbi.nlm.nih.gov/soap/v2.0/eutils.wsdl'
 EFETCH_WSDL_URL = 'http://eutils.ncbi.nlm.nih.gov/soap/v2.0/efetch_%s.wsdl'
 EFETCH_PLAIN_URL = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi'
+NCBI_PLAIN_URL = 'http://www.ncbi.nlm.nih.gov/%s/%s'
 
 EUTILS_CLIENT = _Client(EUTILS_WSDL_URL)
 
@@ -620,9 +621,16 @@ if __name__ == '__main__':
                     xml = f.read()
                     f.close()
                     # Remove wrapping HTML and unescape XML
+                    #LOG.debug('raw data:\n%s' % xml)
                     xml = xml.split('<pre>', 1)[-1]
                     xml = xml.split('</pre>', 1)[0]
                     xml = _unescape(xml, {'&quot;': '"'})
+                    #LOG.debug('xml data:\n%s' % xml)
+                    if not xml.strip(): # 
+                        urls = [NCBI_PLAIN_URL % (options.database, id)
+                                for id in q.IdList.Id]
+                        LOG.warn(
+                            'no meaningful output; try:\n%s' % '\n'.join(urls))
                 else:  # Use SOAP eFetch
                     LOG.info('run eFetch on %s' % options.database)
                     f = efetch_client.service.run_eFetch(
@@ -631,7 +639,9 @@ if __name__ == '__main__':
                         raise Exception(f.ERROR)
                     xml = efetch_client.last_received()
 
-            if output == 'medline':
+            if output is None:
+                pass  # we're bailing
+            elif output == 'medline':
                 outfile.write(str(xml).rstrip()+'\n')
             elif output == 'bibtex':
                 outfile.write(medline_xml_to_bibtex(str(xml)))