56def response(resp):
57 results = []
58
59
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:
128 print(e)
129
130 results.append(res_dict)
131
132 return results