92def response(resp: "SXNG_Response") -> EngineResults:
93
94
95
96
97 efetch_xml = etree.XML(resp.content)
98 res = EngineResults()
99
100 def _field_txt(xml: ElementType, xpath_str: str) -> str:
101 elem = eval_xpath_getindex(xml, xpath_str, 0, default="")
102 return extract_text(elem, allow_none=True) or ""
103
104 for pubmed_article in eval_xpath_list(efetch_xml, "//PubmedArticle"):
105
106 medline_citation: ElementType = eval_xpath_getindex(pubmed_article, "./MedlineCitation", 0)
107 pubmed_data: ElementType = eval_xpath_getindex(pubmed_article, "./PubmedData", 0)
108
109 title: str = eval_xpath_getindex(medline_citation, ".//Article/ArticleTitle", 0).text
110 pmid: str = eval_xpath_getindex(medline_citation, ".//PMID", 0).text
111 url: str = pubmed_url + pmid
112 content = _field_txt(medline_citation, ".//Abstract/AbstractText//text()")
113 doi = _field_txt(medline_citation, ".//ELocationID[@EIdType='doi']/text()")
114 journal = _field_txt(medline_citation, "./Article/Journal/Title/text()")
115 issn = _field_txt(medline_citation, "./Article/Journal/ISSN/text()")
116
117 authors: list[str] = []
118
119 for author in eval_xpath_list(medline_citation, "./Article/AuthorList/Author"):
120 f = eval_xpath_getindex(author, "./ForeName", 0, default=None)
121 l = eval_xpath_getindex(author, "./LastName", 0, default=None)
122 author_name = f"{f.text if f is not None else ''} {l.text if l is not None else ''}".strip()
123 if author_name:
124 authors.append(author_name)
125
126 accepted_date = eval_xpath_getindex(
127 pubmed_data, "./History//PubMedPubDate[@PubStatus='accepted']", 0, default=None
128 )
129 pub_date = None
130 if accepted_date is not None:
131 year = eval_xpath_getindex(accepted_date, "./Year", 0)
132 month = eval_xpath_getindex(accepted_date, "./Month", 0)
133 day = eval_xpath_getindex(accepted_date, "./Day", 0)
134 try:
135 pub_date = datetime(year=int(year.text), month=int(month.text), day=int(day.text))
136 except ValueError:
137 pass
138
139 res.add(
140 res.types.Paper(
141 url=url,
142 title=title,
143 content=content,
144 journal=journal,
145 issn=[issn],
146 authors=authors,
147 doi=doi,
148 publishedDate=pub_date,
149 )
150 )
151 return res