| 
									
										
										
										
											2015-06-01 00:00:32 +02:00
										 |  |  | """
 | 
					
						
							| 
									
										
										
										
											2015-06-02 20:36:58 +02:00
										 |  |  |  Qwant (Web, Images, News, Social) | 
					
						
							| 
									
										
										
										
											2015-06-01 00:00:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |  @website     https://qwant.com/ | 
					
						
							|  |  |  |  @provide-api not officially (https://api.qwant.com/api/search/) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  @using-api   yes | 
					
						
							|  |  |  |  @results     JSON | 
					
						
							|  |  |  |  @stable      yes | 
					
						
							|  |  |  |  @parse       url, title, content | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from urllib import urlencode | 
					
						
							|  |  |  | from json import loads | 
					
						
							| 
									
										
										
										
											2015-06-02 20:36:58 +02:00
										 |  |  | from datetime import datetime | 
					
						
							| 
									
										
										
										
											2015-06-01 00:00:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | # engine dependent config | 
					
						
							| 
									
										
										
										
											2015-06-02 20:36:58 +02:00
										 |  |  | categories = None | 
					
						
							| 
									
										
										
										
											2015-06-01 00:00:32 +02:00
										 |  |  | paging = True | 
					
						
							|  |  |  | language_support = True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 22:11:47 +02:00
										 |  |  | category_to_keyword = {'general': 'web', | 
					
						
							|  |  |  |                        'images': 'images', | 
					
						
							|  |  |  |                        'news': 'news', | 
					
						
							|  |  |  |                        'social media': 'social'} | 
					
						
							| 
									
										
										
										
											2015-06-02 20:36:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-01 00:00:32 +02:00
										 |  |  | # search-url | 
					
						
							| 
									
										
										
										
											2015-06-02 20:36:58 +02:00
										 |  |  | url = 'https://api.qwant.com/api/search/{keyword}?count=10&offset={offset}&f=&{query}' | 
					
						
							| 
									
										
										
										
											2015-06-01 00:00:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # do search-request | 
					
						
							|  |  |  | def request(query, params): | 
					
						
							|  |  |  |     offset = (params['pageno'] - 1) * 10 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 22:11:47 +02:00
										 |  |  |     if categories[0] and categories[0] in category_to_keyword: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         params['url'] = url.format(keyword=category_to_keyword[categories[0]], | 
					
						
							|  |  |  |                                    query=urlencode({'q': query}), | 
					
						
							|  |  |  |                                    offset=offset) | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         params['url'] = url.format(keyword='web', | 
					
						
							|  |  |  |                                    query=urlencode({'q': query}), | 
					
						
							|  |  |  |                                    offset=offset) | 
					
						
							| 
									
										
										
										
											2015-06-01 00:00:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # add language tag if specified | 
					
						
							|  |  |  |     if params['language'] != 'all': | 
					
						
							|  |  |  |         params['url'] += '&locale=' + params['language'].lower() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return params | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # get response from search-request | 
					
						
							|  |  |  | def response(resp): | 
					
						
							|  |  |  |     results = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     search_results = loads(resp.text) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # return empty array if there are no results | 
					
						
							|  |  |  |     if 'data' not in search_results: | 
					
						
							|  |  |  |         return [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     data = search_results.get('data', {}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res = data.get('result', {}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # parse results | 
					
						
							|  |  |  |     for result in res.get('items', {}): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         title = result['title'] | 
					
						
							|  |  |  |         res_url = result['url'] | 
					
						
							|  |  |  |         content = result['desc'] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 22:11:47 +02:00
										 |  |  |         if category_to_keyword.get(categories[0], '') == 'web': | 
					
						
							| 
									
										
										
										
											2015-06-02 20:36:58 +02:00
										 |  |  |             results.append({'title': title, | 
					
						
							|  |  |  |                             'content': content, | 
					
						
							|  |  |  |                             'url': res_url}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 22:11:47 +02:00
										 |  |  |         elif category_to_keyword.get(categories[0], '') == 'images': | 
					
						
							| 
									
										
										
										
											2015-06-02 20:36:58 +02:00
										 |  |  |             thumbnail_src = result['thumbnail'] | 
					
						
							|  |  |  |             img_src = result['media'] | 
					
						
							|  |  |  |             results.append({'template': 'images.html', | 
					
						
							|  |  |  |                             'url': res_url, | 
					
						
							|  |  |  |                             'title': title, | 
					
						
							|  |  |  |                             'content': '', | 
					
						
							|  |  |  |                             'thumbnail_src': thumbnail_src, | 
					
						
							|  |  |  |                             'img_src': img_src}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 22:11:47 +02:00
										 |  |  |         elif (category_to_keyword.get(categories[0], '') == 'news' or | 
					
						
							|  |  |  |               category_to_keyword.get(categories[0], '') == 'social'): | 
					
						
							| 
									
										
										
										
											2015-06-02 20:36:58 +02:00
										 |  |  |             published_date = datetime.fromtimestamp(result['date'], None) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             results.append({'url': res_url, | 
					
						
							|  |  |  |                             'title': title, | 
					
						
							|  |  |  |                             'publishedDate': published_date, | 
					
						
							|  |  |  |                             'content': content}) | 
					
						
							| 
									
										
										
										
											2015-06-01 00:00:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # return results | 
					
						
							|  |  |  |     return results |