| 
									
										
										
										
											2015-05-02 15:45:17 +02:00
										 |  |  | """
 | 
					
						
							|  |  |  |  general mediawiki-engine (Web) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  @website     websites built on mediawiki (https://www.mediawiki.org) | 
					
						
							|  |  |  |  @provide-api yes (http://www.mediawiki.org/wiki/API:Search) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  @using-api   yes | 
					
						
							|  |  |  |  @results     JSON | 
					
						
							|  |  |  |  @stable      yes | 
					
						
							|  |  |  |  @parse       url, title | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  @todo        content | 
					
						
							|  |  |  | """
 | 
					
						
							| 
									
										
										
										
											2014-09-03 11:40:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-23 23:53:27 +02:00
										 |  |  | from json import loads | 
					
						
							| 
									
										
										
										
											2014-09-04 21:19:11 +02:00
										 |  |  | from string import Formatter | 
					
						
							| 
									
										
										
										
											2013-10-23 23:53:27 +02:00
										 |  |  | from urllib import urlencode, quote | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-03 11:40:29 +02:00
										 |  |  | # engine dependent config | 
					
						
							|  |  |  | categories = ['general'] | 
					
						
							|  |  |  | language_support = True | 
					
						
							|  |  |  | paging = True | 
					
						
							|  |  |  | number_of_results = 1 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:19:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-03 11:40:29 +02:00
										 |  |  | # search-url | 
					
						
							|  |  |  | base_url = 'https://{language}.wikipedia.org/' | 
					
						
							| 
									
										
										
										
											2014-09-04 23:53:13 +02:00
										 |  |  | search_url = base_url + 'w/api.php?action=query'\ | 
					
						
							|  |  |  |                                  '&list=search'\ | 
					
						
							|  |  |  |                                  '&{query}'\ | 
					
						
							|  |  |  |                                  '&srprop=timestamp'\ | 
					
						
							|  |  |  |                                  '&format=json'\ | 
					
						
							|  |  |  |                                  '&sroffset={offset}'\ | 
					
						
							| 
									
										
										
										
											2014-12-16 17:10:20 +01:00
										 |  |  |                                  '&srlimit={limit}'     # noqa | 
					
						
							| 
									
										
										
										
											2013-10-23 23:53:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-20 02:31:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-03 11:40:29 +02:00
										 |  |  | # do search-request | 
					
						
							|  |  |  | def request(query, params): | 
					
						
							|  |  |  |     offset = (params['pageno'] - 1) * number_of_results | 
					
						
							| 
									
										
										
										
											2014-12-16 17:10:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:19:11 +02:00
										 |  |  |     string_args = dict(query=urlencode({'srsearch': query}), | 
					
						
							| 
									
										
										
										
											2014-12-16 17:10:20 +01:00
										 |  |  |                        offset=offset, | 
					
						
							|  |  |  |                        limit=number_of_results) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 23:53:13 +02:00
										 |  |  |     format_strings = list(Formatter().parse(base_url)) | 
					
						
							| 
									
										
										
										
											2013-10-23 23:53:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-03 11:40:29 +02:00
										 |  |  |     if params['language'] == 'all': | 
					
						
							|  |  |  |         language = 'en' | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         language = params['language'].split('_')[0] | 
					
						
							| 
									
										
										
										
											2014-09-04 21:19:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if len(format_strings) > 1: | 
					
						
							|  |  |  |         string_args['language'] = language | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-03 11:40:29 +02:00
										 |  |  |     # write search-language back to params, required in response | 
					
						
							|  |  |  |     params['language'] = language | 
					
						
							| 
									
										
										
										
											2014-01-20 02:31:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:19:11 +02:00
										 |  |  |     params['url'] = search_url.format(**string_args) | 
					
						
							| 
									
										
										
										
											2014-09-03 11:40:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-23 23:53:27 +02:00
										 |  |  |     return params | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-03 11:40:29 +02:00
										 |  |  | # get response from search-request | 
					
						
							| 
									
										
										
										
											2013-10-23 23:53:27 +02:00
										 |  |  | def response(resp): | 
					
						
							| 
									
										
										
										
											2014-09-03 11:40:29 +02:00
										 |  |  |     results = [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-23 23:53:27 +02:00
										 |  |  |     search_results = loads(resp.text) | 
					
						
							| 
									
										
										
										
											2014-09-03 11:40:29 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # return empty array if there are no results | 
					
						
							|  |  |  |     if not search_results.get('query', {}).get('search'): | 
					
						
							|  |  |  |         return [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # parse results | 
					
						
							|  |  |  |     for result in search_results['query']['search']: | 
					
						
							| 
									
										
										
										
											2014-12-16 17:10:20 +01:00
										 |  |  |         url = base_url.format(language=resp.search_params['language']) +\ | 
					
						
							|  |  |  |             'wiki/' + quote(result['title'].replace(' ', '_').encode('utf-8')) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:19:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-03 11:40:29 +02:00
										 |  |  |         # append result | 
					
						
							|  |  |  |         results.append({'url': url, | 
					
						
							|  |  |  |                         'title': result['title'], | 
					
						
							|  |  |  |                         'content': ''}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # return results | 
					
						
							|  |  |  |     return results |