| 
									
										
										
										
											2013-10-19 22:19:14 +02:00
										 |  |  | #!/usr/bin/env python | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-19 23:12:18 +02:00
										 |  |  | from urllib import urlencode | 
					
						
							|  |  |  | from json import loads | 
					
						
							| 
									
										
										
										
											2013-10-19 22:19:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-19 22:19:31 +02:00
										 |  |  | categories = ['images'] | 
					
						
							| 
									
										
										
										
											2013-10-19 22:19:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-23 23:55:37 +02:00
										 |  |  | url = 'https://ajax.googleapis.com/' | 
					
						
							|  |  |  | search_url = url + 'ajax/services/search/images?v=1.0&start=0&rsz=large&safe=off&filter=off&{query}' | 
					
						
							| 
									
										
										
										
											2013-10-19 22:19:14 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | def request(query, params): | 
					
						
							| 
									
										
										
										
											2013-10-25 11:20:46 +02:00
										 |  |  |     params['url'] = search_url.format(query=urlencode({'q': query})) | 
					
						
							| 
									
										
										
										
											2013-10-19 22:19:14 +02:00
										 |  |  |     return params | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def response(resp): | 
					
						
							|  |  |  |     results = [] | 
					
						
							| 
									
										
										
										
											2013-10-19 23:12:18 +02:00
										 |  |  |     search_res = loads(resp.text) | 
					
						
							| 
									
										
										
										
											2013-10-20 21:40:14 +02:00
										 |  |  |     if not search_res.get('responseData'): | 
					
						
							| 
									
										
										
										
											2013-10-19 23:12:18 +02:00
										 |  |  |         return [] | 
					
						
							| 
									
										
										
										
											2013-10-20 21:40:14 +02:00
										 |  |  |     if not search_res['responseData'].get('results'): | 
					
						
							| 
									
										
										
										
											2013-10-20 19:45:13 +02:00
										 |  |  |         return [] | 
					
						
							| 
									
										
										
										
											2013-10-19 23:12:18 +02:00
										 |  |  |     for result in search_res['responseData']['results']: | 
					
						
							| 
									
										
										
										
											2013-10-23 23:55:37 +02:00
										 |  |  |         href = result['originalContextUrl'] | 
					
						
							| 
									
										
										
										
											2013-10-19 23:12:18 +02:00
										 |  |  |         title = result['title'] | 
					
						
							| 
									
										
										
										
											2013-10-22 23:35:17 +02:00
										 |  |  |         if not result['url']: | 
					
						
							|  |  |  |             continue | 
					
						
							| 
									
										
										
										
											2013-10-23 23:55:37 +02:00
										 |  |  |         results.append({'url': href, 'title': title, 'content': '', 'img_src': result['url'], 'template': 'images.html'}) | 
					
						
							| 
									
										
										
										
											2013-10-19 22:19:14 +02:00
										 |  |  |     return results |