132def response(resp):
133
134 results = []
135 json_data = resp.json()
136
137 if ('topics' or 'posts') not in json_data.keys():
138 return []
139
140 topics = {}
141
142 for item in json_data['topics']:
143 topics[item['id']] = item
144
145 for post in json_data['posts']:
146 result = topics.get(post['topic_id'], {})
147
148 url = f"{base_url}/p/{post['id']}"
149 status = gettext("closed") if result.get('closed', '') else gettext("open")
150 comments = result.get('posts_count', 0)
151 publishedDate = parser.parse(result['created_at'])
152
153 metadata = []
154 metadata.append('@' + post.get('username', ''))
155
156 if int(comments) > 1:
157 metadata.append(f'{gettext("comments")}: {comments}')
158
159 if result.get('has_accepted_answer'):
160 metadata.append(gettext("answered"))
161 elif int(comments) > 1:
162 metadata.append(status)
163
164 result = {
165 'url': url,
166 'title': html.unescape(result['title']),
167 'content': html.unescape(post.get('blurb', '')),
168 'metadata': ' | '.join(metadata),
169 'publishedDate': publishedDate,
170 'upstream': {'topics': result},
171 }
172
173 avatar = post.get('avatar_template', '').replace('{size}', '96')
174 if show_avatar and avatar:
175 result['thumbnail'] = base_url + avatar
176
177 results.append(result)
178
179 results.append({'number_of_results': len(json_data['topics'])})
180
181 return results