on master help sign in
add comments to deviantart engine
16d1526Thomas Pointhuber
searx/engines/deviantart.py+29−3
@@ -1,35 +1,61 @@
1+## Deviantart (Images)
2+#
3+# @website https://www.deviantart.com/
4+# @provide-api yes (https://www.deviantart.com/developers/) (RSS)
5+#
6+# @using-api no (TODO, rewrite to api)
7+# @results HTML
8+# @stable no (HTML can change)
9+# @parse url, title, thumbnail
10+#
11+# @todo rewrite to api
12+
1from urllib import urlencode
13from urllib import urlencode
2from urlparse import urljoin
14from urlparse import urljoin
3from lxml import html
15from lxml import html
4
16
17+# engine dependent config
5categories = ['images']
18categories = ['images']
19+paging = True
6
20
21+# search-url
7base_url = 'https://www.deviantart.com/'
22base_url = 'https://www.deviantart.com/'
8search_url = base_url+'search?offset={offset}&{query}'
23search_url = base_url+'search?offset={offset}&{query}'
9
24
10−paging = True
11−
12
25
26+# do search-request
13def request(query, params):
27def request(query, params):
14 offset = (params['pageno'] - 1) * 24
28 offset = (params['pageno'] - 1) * 24
29+
15 params['url'] = search_url.format(offset=offset,
30 params['url'] = search_url.format(offset=offset,
16 query=urlencode({'q': query}))
31 query=urlencode({'q': query}))
32+
17 return params
33 return params
18
34
19
35
36+# get response from search-request
20def response(resp):
37def response(resp):
21 results = []
38 results = []
39+
40+ # return empty array if a redirection code is returned
22 if resp.status_code == 302:
41 if resp.status_code == 302:
23− return results
42+ return []
43+
24 dom = html.fromstring(resp.text)
44 dom = html.fromstring(resp.text)
45+
46+ # parse results
25 for result in dom.xpath('//div[contains(@class, "tt-a tt-fh")]'):
47 for result in dom.xpath('//div[contains(@class, "tt-a tt-fh")]'):
26 link = result.xpath('.//a[contains(@class, "thumb")]')[0]
48 link = result.xpath('.//a[contains(@class, "thumb")]')[0]
27 url = urljoin(base_url, link.attrib.get('href'))
49 url = urljoin(base_url, link.attrib.get('href'))
28 title_links = result.xpath('.//span[@class="details"]//a[contains(@class, "t")]') # noqa
50 title_links = result.xpath('.//span[@class="details"]//a[contains(@class, "t")]') # noqa
29 title = ''.join(title_links[0].xpath('.//text()'))
51 title = ''.join(title_links[0].xpath('.//text()'))
30 img_src = link.xpath('.//img')[0].attrib['src']
52 img_src = link.xpath('.//img')[0].attrib['src']
53+
54+ # append result
31 results.append({'url': url,
55 results.append({'url': url,
32 'title': title,
56 'title': title,
33 'img_src': img_src,
57 'img_src': img_src,
34 'template': 'images.html'})
58 'template': 'images.html'})
59+
60+ # return results
35 return results
61 return results