.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
pubmed.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""PubMed (Scholar publications)
3
4"""
5
6from datetime import datetime
7from urllib.parse import urlencode
8
9from lxml import etree
10from searx.network import get
11from searx.utils import (
12 eval_xpath_getindex,
13 eval_xpath_list,
14 extract_text,
15)
16
17# about
18about = {
19 "website": 'https://www.ncbi.nlm.nih.gov/pubmed/',
20 "wikidata_id": 'Q1540899',
21 "official_api_documentation": {
22 'url': 'https://www.ncbi.nlm.nih.gov/home/develop/api/',
23 'comment': 'More info on api: https://www.ncbi.nlm.nih.gov/books/NBK25501/',
24 },
25 "use_official_api": True,
26 "require_api_key": False,
27 "results": 'XML',
28}
29
30categories = ['science', 'scientific publications']
31
32base_url = (
33 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi' + '?db=pubmed&{query}&retstart={offset}&retmax={hits}'
34)
35
36# engine dependent config
37number_of_results = 10
38pubmed_url = 'https://www.ncbi.nlm.nih.gov/pubmed/'
39
40
41def request(query, params):
42 # basic search
43 offset = (params['pageno'] - 1) * number_of_results
44
45 string_args = {
46 'query': urlencode({'term': query}),
47 'offset': offset,
48 'hits': number_of_results,
49 }
50
51 params['url'] = base_url.format(**string_args)
52
53 return params
54
55
56def response(resp): # pylint: disable=too-many-locals
57 results = []
58
59 # First retrieve notice of each result
60 pubmed_retrieve_api_url = (
61 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?' + 'db=pubmed&retmode=xml&id={pmids_string}'
62 )
63
64 pmids_results = etree.XML(resp.content)
65 pmids = pmids_results.xpath('//eSearchResult/IdList/Id')
66 pmids_string = ''
67
68 for item in pmids:
69 pmids_string += item.text + ','
70
71 retrieve_notice_args = {'pmids_string': pmids_string}
72
73 retrieve_url_encoded = pubmed_retrieve_api_url.format(**retrieve_notice_args)
74
75 search_results_response = get(retrieve_url_encoded).content
76 search_results = etree.XML(search_results_response)
77 for entry in eval_xpath_list(search_results, '//PubmedArticle'):
78 medline = eval_xpath_getindex(entry, './MedlineCitation', 0)
79
80 title = eval_xpath_getindex(medline, './/Article/ArticleTitle', 0).text
81 pmid = eval_xpath_getindex(medline, './/PMID', 0).text
82 url = pubmed_url + pmid
83 content = extract_text(
84 eval_xpath_getindex(medline, './/Abstract/AbstractText//text()', 0, default=None), allow_none=True
85 )
86 doi = extract_text(
87 eval_xpath_getindex(medline, './/ELocationID[@EIdType="doi"]/text()', 0, default=None), allow_none=True
88 )
89 journal = extract_text(
90 eval_xpath_getindex(medline, './Article/Journal/Title/text()', 0, default=None), allow_none=True
91 )
92 issn = extract_text(
93 eval_xpath_getindex(medline, './Article/Journal/ISSN/text()', 0, default=None), allow_none=True
94 )
95 authors = []
96 for author in eval_xpath_list(medline, './Article/AuthorList/Author'):
97 f = eval_xpath_getindex(author, './ForeName', 0, default=None)
98 l = eval_xpath_getindex(author, './LastName', 0, default=None)
99 f = '' if f is None else f.text
100 l = '' if l is None else l.text
101 authors.append((f + ' ' + l).strip())
102
103 res_dict = {
104 'template': 'paper.html',
105 'url': url,
106 'title': title,
107 'content': content or "",
108 'journal': journal,
109 'issn': [issn],
110 'authors': authors,
111 'doi': doi,
112 }
113
114 accepted_date = eval_xpath_getindex(
115 entry, './PubmedData/History//PubMedPubDate[@PubStatus="accepted"]', 0, default=None
116 )
117 if accepted_date is not None:
118 year = eval_xpath_getindex(accepted_date, './Year', 0)
119 month = eval_xpath_getindex(accepted_date, './Month', 0)
120 day = eval_xpath_getindex(accepted_date, './Day', 0)
121 try:
122 publishedDate = datetime.strptime(
123 year.text + '-' + month.text + '-' + day.text,
124 '%Y-%m-%d',
125 )
126 res_dict['publishedDate'] = publishedDate
127 except Exception as e: # pylint: disable=broad-exception-caught
128 print(e)
129
130 results.append(res_dict)
131
132 return results
request(query, params)
Definition pubmed.py:41