| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2015-05-02 15:45:17 +02:00
										 |  |  | """
 | 
					
						
							|  |  |  |  Deviantart (Images) | 
					
						
							|  |  |  | """
 | 
					
						
							| 
									
										
										
										
											2014-09-02 16:48:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-06 17:42:46 +02:00
										 |  |  | from urllib.parse import urlencode | 
					
						
							| 
									
										
										
										
											2020-11-03 08:44:41 +01:00
										 |  |  | from lxml import html | 
					
						
							| 
									
										
										
										
											2013-10-20 11:12:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2016-07-19 10:06:47 +02:00
										 |  |  | time_range_support = True | 
					
						
							| 
									
										
										
										
											2013-10-20 11:12:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-03 08:44:41 +01:00
										 |  |  | time_range_dict = { | 
					
						
							|  |  |  |     'day': 'popular-24-hours', | 
					
						
							|  |  |  |     'week': 'popular-1-week', | 
					
						
							|  |  |  |     'month': 'popular-1-month', | 
					
						
							|  |  |  |     'year': 'most-recent', | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-20 11:12:10 +02:00
										 |  |  | def request(query, params): | 
					
						
							| 
									
										
										
										
											2016-12-11 16:41:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-03 08:44:41 +01:00
										 |  |  |     # https://www.deviantart.com/search/deviations?page=5&q=foo | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |     query = { | 
					
						
							|  |  |  |         'page': params['pageno'], | 
					
						
							|  |  |  |         'q': query, | 
					
						
							| 
									
										
										
										
											2020-11-03 08:44:41 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-07-26 00:22:05 +02:00
										 |  |  |     if params['time_range'] in time_range_dict: | 
					
						
							| 
									
										
										
										
											2020-11-03 08:44:41 +01:00
										 |  |  |         query['order'] = time_range_dict[params['time_range']] | 
					
						
							| 
									
										
										
										
											2014-09-02 16:48:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-03 08:44:41 +01:00
										 |  |  |     params['url'] = base_url + '/search/deviations?' + urlencode(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 = [] | 
					
						
							| 
									
										
										
										
											2014-09-02 16:48:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-20 11:12:10 +02:00
										 |  |  |     dom = html.fromstring(resp.text) | 
					
						
							| 
									
										
										
										
											2015-01-17 19:24:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-16 14:42:31 +02:00
										 |  |  |     for row in dom.xpath('//div[contains(@data-hook, "content_row")]'): | 
					
						
							|  |  |  |         for result in row.xpath('./div'): | 
					
						
							| 
									
										
										
										
											2020-11-03 08:44:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             a_tag = result.xpath('.//a[@data-hook="deviation_link"]')[0] | 
					
						
							|  |  |  |             noscript_tag = a_tag.xpath('.//noscript') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if noscript_tag: | 
					
						
							|  |  |  |                 img_tag = noscript_tag[0].xpath('.//img') | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 img_tag = a_tag.xpath('.//img') | 
					
						
							|  |  |  |             if not img_tag: | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             img_tag = img_tag[0] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |             results.append( | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     'template': 'images.html', | 
					
						
							|  |  |  |                     'url': a_tag.attrib.get('href'), | 
					
						
							|  |  |  |                     'img_src': img_tag.attrib.get('src'), | 
					
						
							|  |  |  |                     'title': img_tag.attrib.get('alt'), | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2020-11-03 08:44:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-20 11:12:10 +02:00
										 |  |  |     return results |