| 
									
										
										
										
											2021-02-03 19:13:39 +01:00
										 |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-07 13:06:05 +01:00
										 |  |  |  Openverse (formerly known as: Creative Commons search engine) [Images] | 
					
						
							| 
									
										
										
										
											2021-02-03 19:13:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from json import loads | 
					
						
							|  |  |  | from urllib.parse import urlencode | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | about = { | 
					
						
							| 
									
										
										
										
											2025-02-02 22:12:24 +01:00
										 |  |  |     "website": 'https://openverse.org/', | 
					
						
							| 
									
										
										
										
											2021-02-03 19:13:39 +01:00
										 |  |  |     "wikidata_id": None, | 
					
						
							| 
									
										
										
										
											2025-02-02 22:12:24 +01:00
										 |  |  |     "official_api_documentation": 'https://api.openverse.org/v1/', | 
					
						
							| 
									
										
										
										
											2021-02-03 19:13:39 +01:00
										 |  |  |     "use_official_api": True, | 
					
						
							|  |  |  |     "require_api_key": False, | 
					
						
							|  |  |  |     "results": 'JSON', | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | categories = ['images'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | paging = True | 
					
						
							|  |  |  | nb_per_page = 20 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-02 22:12:24 +01:00
										 |  |  | base_url = 'https://api.openverse.org/v1/images/' | 
					
						
							| 
									
										
										
										
											2022-01-07 14:08:19 +01:00
										 |  |  | search_string = '?page={page}&page_size={nb_per_page}&format=json&{query}' | 
					
						
							| 
									
										
										
										
											2021-02-03 19:13:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def request(query, params): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |     search_path = search_string.format(query=urlencode({'q': query}), nb_per_page=nb_per_page, page=params['pageno']) | 
					
						
							| 
									
										
										
										
											2021-02-03 19:13:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     params['url'] = base_url + search_path | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return params | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def response(resp): | 
					
						
							|  |  |  |     results = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     json_data = loads(resp.text) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for result in json_data['results']: | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         results.append( | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 'url': result['foreign_landing_url'], | 
					
						
							|  |  |  |                 'title': result['title'], | 
					
						
							|  |  |  |                 'img_src': result['url'], | 
					
						
							|  |  |  |                 'template': 'images.html', | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2021-02-03 19:13:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return results |