82 search_res = resp.json()
93 heading = search_res.get(
'Heading',
'')
100 answer = search_res.get(
'Answer',
'')
102 logger.debug(
'AnswerType="%s" Answer="%s"', search_res.get(
'AnswerType'), answer)
103 if search_res.get(
'AnswerType')
not in [
'calc',
'ip']:
104 results.append({
'answer': html_to_text(answer),
'url': search_res.get(
'AbstractURL',
'')})
107 if 'Definition' in search_res:
108 content = content + search_res.get(
'Definition',
'')
110 if 'Abstract' in search_res:
111 content = content + search_res.get(
'Abstract',
'')
114 image = search_res.get(
'Image')
115 image =
None if image ==
'' else image
116 if image
is not None and urlparse(image).netloc ==
'':
117 image = urljoin(
'https://duckduckgo.com', image)
121 for ddg_result
in search_res.get(
'Results', []):
122 firstURL = ddg_result.get(
'FirstURL')
123 text = ddg_result.get(
'Text')
124 if firstURL
is not None and text
is not None:
125 urls.append({
'title': text,
'url': firstURL})
126 results.append({
'title': heading,
'url': firstURL})
129 for ddg_result
in search_res.get(
'RelatedTopics', []):
130 if 'FirstURL' in ddg_result:
131 firstURL = ddg_result.get(
'FirstURL')
132 text = ddg_result.get(
'Text')
135 if suggestion != heading
and suggestion
is not None:
136 results.append({
'suggestion': suggestion})
137 elif 'Topics' in ddg_result:
139 relatedTopics.append({
'name': ddg_result.get(
'Name',
''),
'suggestions': suggestions})
140 for topic_result
in ddg_result.get(
'Topics', []):
141 suggestion =
result_to_text(topic_result.get(
'Text'), topic_result.get(
'Result'))
142 if suggestion != heading
and suggestion
is not None:
143 suggestions.append(suggestion)
146 abstractURL = search_res.get(
'AbstractURL',
'')
147 if abstractURL !=
'':
149 infobox_id = abstractURL
150 urls.append({
'title': search_res.get(
'AbstractSource'),
'url': abstractURL,
'official':
True})
151 results.append({
'url': abstractURL,
'title': heading})
154 definitionURL = search_res.get(
'DefinitionURL',
'')
155 if definitionURL !=
'':
157 infobox_id = definitionURL
158 urls.append({
'title': search_res.get(
'DefinitionSource'),
'url': definitionURL})
166 if 'Infobox' in search_res:
167 infobox = search_res.get(
'Infobox')
168 if 'content' in infobox:
171 for info
in infobox.get(
'content'):
172 data_type = info.get(
'data_type')
173 data_label = info.get(
'label')
174 data_value = info.get(
'value')
177 if data_value ==
'""':
184 external_url = get_external_url(data_type, data_value)
185 if external_url
is not None:
186 urls.append({
'title': data_label,
'url': external_url})
187 elif data_type
in [
'instance',
'wiki_maps_trigger',
'google_play_artist_id']:
192 elif data_type ==
'string' and data_label ==
'Website':
195 elif data_type ==
'area':
196 attributes.append({
'label': data_label,
'value':
area_to_str(data_value),
'entity':
'P2046'})
197 osm_zoom = area_to_osm_zoom(data_value.get(
'amount'))
198 elif data_type ==
'coordinates':
199 if data_value.get(
'globe') ==
'http://www.wikidata.org/entity/Q2':
205 attributes.append({
'label': data_label,
'value': data_value,
'entity':
'P625'})
206 elif data_type ==
'string':
207 attributes.append({
'label': data_label,
'value': data_value})
210 data_label = coordinates.get(
'label')
211 data_value = coordinates.get(
'value')
212 latitude = data_value.get(
'latitude')
213 longitude = data_value.get(
'longitude')
214 url = get_earth_coordinates_url(latitude, longitude, osm_zoom)
215 urls.append({
'title':
'OpenStreetMap',
'url': url,
'entity':
'P625'})
219 if image
is None and len(attributes) == 0
and len(urls) == 1
and len(relatedTopics) == 0
and len(content) == 0:
220 results.append({
'url': urls[0][
'url'],
'title': heading,
'content': content})
228 'attributes': attributes,
230 'relatedTopics': relatedTopics,