pubmed.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env python
  2. """
  3. PubMed (Scholar publications)
  4. @website https://www.ncbi.nlm.nih.gov/pubmed/
  5. @provide-api yes (https://www.ncbi.nlm.nih.gov/home/develop/api/)
  6. @using-api yes
  7. @results XML
  8. @stable yes
  9. @parse url, title, publishedDate, content
  10. More info on api: https://www.ncbi.nlm.nih.gov/books/NBK25501/
  11. """
  12. from lxml import etree
  13. from datetime import datetime
  14. from searx.url_utils import urlencode, urlopen
  15. categories = ['science']
  16. base_url = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi'\
  17. + '?db=pubmed&{query}&retstart={offset}&retmax={hits}'
  18. # engine dependent config
  19. number_of_results = 10
  20. pubmed_url = 'https://www.ncbi.nlm.nih.gov/pubmed/'
  21. def request(query, params):
  22. # basic search
  23. offset = (params['pageno'] - 1) * number_of_results
  24. string_args = dict(query=urlencode({'term': query}),
  25. offset=offset,
  26. hits=number_of_results)
  27. params['url'] = base_url.format(**string_args)
  28. return params
  29. def response(resp):
  30. results = []
  31. # First retrieve notice of each result
  32. pubmed_retrieve_api_url = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?'\
  33. + 'db=pubmed&retmode=xml&id={pmids_string}'
  34. # handle Python2 vs Python3 management of bytes and strings
  35. try:
  36. pmids_results = etree.XML(resp.text.encode('utf-8'))
  37. except AttributeError:
  38. pmids_results = etree.XML(resp.text)
  39. pmids = pmids_results.xpath('//eSearchResult/IdList/Id')
  40. pmids_string = ''
  41. for item in pmids:
  42. pmids_string += item.text + ','
  43. retrieve_notice_args = dict(pmids_string=pmids_string)
  44. retrieve_url_encoded = pubmed_retrieve_api_url.format(**retrieve_notice_args)
  45. search_results_xml = urlopen(retrieve_url_encoded).read()
  46. search_results = etree.XML(search_results_xml).xpath('//PubmedArticleSet/PubmedArticle/MedlineCitation')
  47. for entry in search_results:
  48. title = entry.xpath('.//Article/ArticleTitle')[0].text
  49. pmid = entry.xpath('.//PMID')[0].text
  50. url = pubmed_url + pmid
  51. try:
  52. content = entry.xpath('.//Abstract/AbstractText')[0].text
  53. except:
  54. content = 'No abstract is available for this publication.'
  55. # If a doi is available, add it to the snipppet
  56. try:
  57. doi = entry.xpath('.//ELocationID[@EIdType="doi"]')[0].text
  58. content = 'DOI: ' + doi + ' Abstract: ' + content
  59. except:
  60. pass
  61. if len(content) > 300:
  62. content = content[0:300] + "..."
  63. # TODO: center snippet on query term
  64. publishedDate = datetime.strptime(entry.xpath('.//DateCreated/Year')[0].text
  65. + '-' + entry.xpath('.//DateCreated/Month')[0].text
  66. + '-' + entry.xpath('.//DateCreated/Day')[0].text, '%Y-%m-%d')
  67. res_dict = {'url': url,
  68. 'title': title,
  69. 'publishedDate': publishedDate,
  70. 'content': content}
  71. results.append(res_dict)
  72. return results