| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							| 
									
										
										
										
											2021-04-26 20:18:20 +02:00
										 |  |  | # lint: pylint | 
					
						
							| 
									
										
										
										
											2023-09-08 12:08:14 +02:00
										 |  |  | """Deviantart (Images)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-02 15:45:17 +02:00
										 |  |  | """
 | 
					
						
							| 
									
										
										
										
											2014-09-02 16:48:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-08 12:08:14 +02:00
										 |  |  | import urllib.parse | 
					
						
							| 
									
										
										
										
											2020-11-03 08:44:41 +01:00
										 |  |  | from lxml import html | 
					
						
							| 
									
										
										
										
											2013-10-20 11:12:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-08 12:08:14 +02:00
										 |  |  | from searx.utils import extract_text, eval_xpath, eval_xpath_list | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  | # about | 
					
						
							|  |  |  | about = { | 
					
						
							|  |  |  |     "website": 'https://www.deviantart.com/', | 
					
						
							|  |  |  |     "wikidata_id": 'Q46523', | 
					
						
							|  |  |  |     "official_api_documentation": 'https://www.deviantart.com/developers/', | 
					
						
							|  |  |  |     "use_official_api": False, | 
					
						
							|  |  |  |     "require_api_key": False, | 
					
						
							|  |  |  |     "results": 'HTML', | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-02 16:48:18 +02:00
										 |  |  | # engine dependent config | 
					
						
							| 
									
										
										
										
											2013-10-20 11:12:10 +02:00
										 |  |  | categories = ['images'] | 
					
						
							| 
									
										
										
										
											2014-09-02 16:48:18 +02:00
										 |  |  | paging = True | 
					
						
							| 
									
										
										
										
											2014-01-30 00:09:47 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-03 08:44:41 +01:00
										 |  |  | # search-url | 
					
						
							|  |  |  | base_url = 'https://www.deviantart.com' | 
					
						
							| 
									
										
										
										
											2014-01-20 02:31:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-08 12:08:14 +02:00
										 |  |  | results_xpath = '//div[@class="_2pZkk"]/div/div/a' | 
					
						
							|  |  |  | url_xpath = './@href' | 
					
						
							|  |  |  | thumbnail_src_xpath = './div/img/@src' | 
					
						
							|  |  |  | img_src_xpath = './div/img/@srcset' | 
					
						
							|  |  |  | title_xpath = './@aria-label' | 
					
						
							|  |  |  | premium_xpath = '../div/div/div/text()' | 
					
						
							|  |  |  | premium_keytext = 'Watch the artist to view this deviation' | 
					
						
							|  |  |  | cursor_xpath = '(//a[@class="_1OGeq"]/@href)[last()]' | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-11 16:41:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-08 12:08:14 +02:00
										 |  |  | def request(query, params): | 
					
						
							| 
									
										
										
										
											2020-11-03 08:44:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-08 12:08:14 +02:00
										 |  |  |     # https://www.deviantart.com/search?q=foo | 
					
						
							| 
									
										
										
										
											2014-09-02 16:48:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-08 12:08:14 +02:00
										 |  |  |     nextpage_url = params['engine_data'].get('nextpage') | 
					
						
							|  |  |  |     # don't use nextpage when user selected to jump back to page 1 | 
					
						
							|  |  |  |     if params['pageno'] > 1 and nextpage_url is not None: | 
					
						
							|  |  |  |         params['url'] = nextpage_url | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         params['url'] = f"{base_url}/search?{urllib.parse.urlencode({'q': query})}" | 
					
						
							| 
									
										
										
										
											2013-10-20 11:12:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-03 08:44:41 +01:00
										 |  |  |     return params | 
					
						
							| 
									
										
										
										
											2013-10-20 11:12:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-20 11:12:10 +02:00
										 |  |  | def response(resp): | 
					
						
							| 
									
										
										
										
											2014-09-02 16:48:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-03 08:44:41 +01:00
										 |  |  |     results = [] | 
					
						
							| 
									
										
										
										
											2013-10-20 11:12:10 +02:00
										 |  |  |     dom = html.fromstring(resp.text) | 
					
						
							| 
									
										
										
										
											2015-01-17 19:24:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-08 12:08:14 +02:00
										 |  |  |     for result in eval_xpath_list(dom, results_xpath): | 
					
						
							|  |  |  |         # skip images that are blurred | 
					
						
							|  |  |  |         _text = extract_text(eval_xpath(result, premium_xpath)) | 
					
						
							|  |  |  |         if _text and premium_keytext in _text: | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         img_src = extract_text(eval_xpath(result, img_src_xpath)) | 
					
						
							|  |  |  |         if img_src: | 
					
						
							|  |  |  |             img_src = img_src.split(' ')[0] | 
					
						
							|  |  |  |             parsed_url = urllib.parse.urlparse(img_src) | 
					
						
							|  |  |  |             img_src = parsed_url._replace(path=parsed_url.path.split('/v1')[0]).geturl() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         results.append( | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 'template': 'images.html', | 
					
						
							|  |  |  |                 'url': extract_text(eval_xpath(result, url_xpath)), | 
					
						
							|  |  |  |                 'img_src': img_src, | 
					
						
							|  |  |  |                 'thumbnail_src': extract_text(eval_xpath(result, thumbnail_src_xpath)), | 
					
						
							|  |  |  |                 'title': extract_text(eval_xpath(result, title_xpath)), | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     nextpage_url = extract_text(eval_xpath(dom, cursor_xpath)) | 
					
						
							|  |  |  |     if nextpage_url: | 
					
						
							|  |  |  |         results.append( | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 'engine_data': nextpage_url.replace("http://", "https://"), | 
					
						
							|  |  |  |                 'key': 'nextpage', | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-11-03 08:44:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-20 11:12:10 +02:00
										 |  |  |     return results |