| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							| 
									
										
										
										
											2018-08-18 19:24:02 +02:00
										 |  |  | """
 | 
					
						
							|  |  |  |  Duden | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import re | 
					
						
							| 
									
										
										
										
											2020-08-06 17:42:46 +02:00
										 |  |  | from urllib.parse import quote, urljoin | 
					
						
							| 
									
										
										
										
											2020-12-07 10:31:11 +01:00
										 |  |  | from lxml import html | 
					
						
							|  |  |  | from searx.utils import extract_text, eval_xpath, eval_xpath_list, eval_xpath_getindex | 
					
						
							| 
									
										
										
										
											2022-08-19 17:58:37 +02:00
										 |  |  | from searx.network import raise_for_httperror | 
					
						
							| 
									
										
										
										
											2018-08-18 19:24:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  | # about | 
					
						
							|  |  |  | about = { | 
					
						
							|  |  |  |     "website": 'https://www.duden.de', | 
					
						
							|  |  |  |     "wikidata_id": 'Q73624591', | 
					
						
							|  |  |  |     "official_api_documentation": None, | 
					
						
							|  |  |  |     "use_official_api": False, | 
					
						
							|  |  |  |     "require_api_key": False, | 
					
						
							|  |  |  |     "results": 'HTML', | 
					
						
							| 
									
										
										
										
											2021-12-21 09:39:03 +01:00
										 |  |  |     "language": 'de', | 
					
						
							| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-28 16:26:38 +01:00
										 |  |  | categories = ['dictionaries'] | 
					
						
							| 
									
										
										
										
											2018-08-18 19:24:02 +02:00
										 |  |  | paging = True | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # search-url | 
					
						
							|  |  |  | base_url = 'https://www.duden.de/' | 
					
						
							| 
									
										
										
										
											2019-07-25 08:17:45 +02:00
										 |  |  | search_url = base_url + 'suchen/dudenonline/{query}?search_api_fulltext=&page={offset}' | 
					
						
							| 
									
										
										
										
											2018-08-18 19:24:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def request(query, params): | 
					
						
							|  |  |  |     '''pre-request callback
 | 
					
						
							|  |  |  |     params<dict>: | 
					
						
							|  |  |  |       method  : POST/GET | 
					
						
							|  |  |  |       headers : {} | 
					
						
							|  |  |  |       data    : {} # if method == POST | 
					
						
							|  |  |  |       url     : '' | 
					
						
							|  |  |  |       category: 'search category' | 
					
						
							|  |  |  |       pageno  : 1 # number of the requested page | 
					
						
							|  |  |  |     '''
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |     offset = params['pageno'] - 1 | 
					
						
							| 
									
										
										
										
											2019-07-25 08:17:45 +02:00
										 |  |  |     if offset == 0: | 
					
						
							|  |  |  |         search_url_fmt = base_url + 'suchen/dudenonline/{query}' | 
					
						
							|  |  |  |         params['url'] = search_url_fmt.format(query=quote(query)) | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         params['url'] = search_url.format(offset=offset, query=quote(query)) | 
					
						
							| 
									
										
										
										
											2020-12-07 10:31:11 +01:00
										 |  |  |     # after the last page of results, spelling corrections are returned after a HTTP redirect | 
					
						
							|  |  |  |     # whatever the page number is | 
					
						
							|  |  |  |     params['soft_max_redirects'] = 1 | 
					
						
							| 
									
										
										
										
											2022-08-19 17:58:37 +02:00
										 |  |  |     params['raise_for_httperror'] = False | 
					
						
							| 
									
										
										
										
											2018-08-18 19:24:02 +02:00
										 |  |  |     return params | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def response(resp): | 
					
						
							|  |  |  |     '''post-response callback
 | 
					
						
							|  |  |  |     resp: requests response object | 
					
						
							|  |  |  |     '''
 | 
					
						
							|  |  |  |     results = [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-19 17:58:37 +02:00
										 |  |  |     if resp.status_code == 404: | 
					
						
							|  |  |  |         return results | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     raise_for_httperror(resp) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-18 19:24:02 +02:00
										 |  |  |     dom = html.fromstring(resp.text) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |     number_of_results_element = eval_xpath_getindex( | 
					
						
							|  |  |  |         dom, '//a[@class="active" and contains(@href,"/suchen/dudenonline")]/span/text()', 0, default=None | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2020-12-07 10:31:11 +01:00
										 |  |  |     if number_of_results_element is not None: | 
					
						
							|  |  |  |         number_of_results_string = re.sub('[^0-9]', '', number_of_results_element) | 
					
						
							| 
									
										
										
										
											2018-08-18 19:24:02 +02:00
										 |  |  |         results.append({'number_of_results': int(number_of_results_string)}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-07 10:31:11 +01:00
										 |  |  |     for result in eval_xpath_list(dom, '//section[not(contains(@class, "essay"))]'): | 
					
						
							|  |  |  |         url = eval_xpath_getindex(result, './/h2/a', 0).get('href') | 
					
						
							|  |  |  |         url = urljoin(base_url, url) | 
					
						
							|  |  |  |         title = eval_xpath(result, 'string(.//h2/a)').strip() | 
					
						
							|  |  |  |         content = extract_text(eval_xpath(result, './/p')) | 
					
						
							|  |  |  |         # append result | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         results.append({'url': url, 'title': title, 'content': content}) | 
					
						
							| 
									
										
										
										
											2018-08-18 19:24:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return results |