.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
deviantart.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Deviantart (Images)
3
4"""
5
6import urllib.parse
7from lxml import html
8
9from searx.utils import extract_text, eval_xpath, eval_xpath_list
10
11# about
12about = {
13 "website": 'https://www.deviantart.com/',
14 "wikidata_id": 'Q46523',
15 "official_api_documentation": 'https://www.deviantart.com/developers/',
16 "use_official_api": False,
17 "require_api_key": False,
18 "results": 'HTML',
19}
20
21# engine dependent config
22categories = ['images']
23paging = True
24
25# search-url
26base_url = 'https://www.deviantart.com'
27
28results_xpath = '//div[@class="_2pZkk"]/div/div/a'
29url_xpath = './@href'
30thumbnail_src_xpath = './div/img/@src'
31img_src_xpath = './div/img/@srcset'
32title_xpath = './@aria-label'
33premium_xpath = '../div/div/div/text()'
34premium_keytext = 'Watch the artist to view this deviation'
35cursor_xpath = '(//a[@class="_1OGeq"]/@href)[last()]'
36
37
38def request(query, params):
39
40 # https://www.deviantart.com/search?q=foo
41
42 nextpage_url = params['engine_data'].get('nextpage')
43 # don't use nextpage when user selected to jump back to page 1
44 if params['pageno'] > 1 and nextpage_url is not None:
45 params['url'] = nextpage_url
46 else:
47 params['url'] = f"{base_url}/search?{urllib.parse.urlencode({'q': query})}"
48
49 return params
50
51
52def response(resp):
53
54 results = []
55 dom = html.fromstring(resp.text)
56
57 for result in eval_xpath_list(dom, results_xpath):
58 # skip images that are blurred
59 _text = extract_text(eval_xpath(result, premium_xpath))
60 if _text and premium_keytext in _text:
61 continue
62 img_src = extract_text(eval_xpath(result, img_src_xpath))
63 if img_src:
64 img_src = img_src.split(' ')[0]
65 parsed_url = urllib.parse.urlparse(img_src)
66 img_src = parsed_url._replace(path=parsed_url.path.split('/v1')[0]).geturl()
67
68 results.append(
69 {
70 'template': 'images.html',
71 'url': extract_text(eval_xpath(result, url_xpath)),
72 'img_src': img_src,
73 'thumbnail_src': extract_text(eval_xpath(result, thumbnail_src_xpath)),
74 'title': extract_text(eval_xpath(result, title_xpath)),
75 }
76 )
77
78 nextpage_url = extract_text(eval_xpath(dom, cursor_xpath))
79 if nextpage_url:
80 results.append(
81 {
82 'engine_data': nextpage_url.replace("http://", "https://"),
83 'key': 'nextpage',
84 }
85 )
86
87 return results
request(query, params)
Definition deviantart.py:38