74def response(resp):
75 results = []
76 json_data = resp.json()
77
78 for result in json_data.get('results', []):
79
80 if not result.get('title'):
81 continue
82
83
84 url = None
85
86
87 doi = result.get('doi')
88 if doi:
89 url = f'https://doi.org/{doi}'
90
91 if url is None and result.get('doi'):
92
93 url = 'https://doi.org/' + str(result['doi'])
94 elif result.get('id'):
95 url = 'https://core.ac.uk/works/' + str(result['id'])
96 elif result.get('downloadUrl'):
97 url = result['downloadUrl']
98 elif result.get('sourceFulltextUrls'):
99 url = result['sourceFulltextUrls']
100 else:
101 continue
102
103
104 published_date = None
105
106 raw_date = result.get('publishedDate') or result.get('depositedDate')
107 if raw_date:
108 try:
109 published_date = datetime.fromisoformat(result['publishedDate'].replace('Z', '+00:00'))
110 except (ValueError, AttributeError):
111 pass
112
113
114 journals = []
115 if result.get('journals'):
116 journals = [j.get('title') for j in result['journals'] if j.get('title')]
117
118
119 publisher = result.get('publisher', '').strip("'")
120 if publisher:
121 publisher = publisher.strip("'")
122
123
124 authors = set()
125 for i in result.get('authors', []):
126 name = i.get("name")
127 if name:
128 authors.add(name)
129
130 results.append(
131 {
132 'template': 'paper.html',
133 'title': result.get('title'),
134 'url': url,
135 'content': result.get('fullText', '') or '',
136
137 'tags': result.get('fieldOfStudy', []),
138 'publishedDate': published_date,
139 'type': result.get('documentType', '') or '',
140 'authors': authors,
141 'editor': ', '.join(result.get('contributors', [])),
142 'publisher': publisher,
143 'journal': ', '.join(journals),
144 'doi': result.get('doi'),
145
146
147 'pdf_url': result.get('downloadUrl', {}) or result.get("sourceFulltextUrls", {}),
148 }
149 )
150
151 return results