@@ -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+
113from urllib import urlencode
214from urlparse import urljoin
315from lxml import html
416
17+# engine dependent config
518categories = ['images']
19+paging = True
620
21+# search-url
722base_url = 'https://www.deviantart.com/'
823search_url = base_url+'search?offset={offset}&{query}'
924
10−paging = True
11−
1225
26+# do search-request
1327def request(query, params):
1428 offset = (params['pageno'] - 1) * 24
29+
1530 params['url'] = search_url.format(offset=offset,
1631 query=urlencode({'q': query}))
32+
1733 return params
1834
1935
36+# get response from search-request
2037def response(resp):
2138 results = []
39+
40+ # return empty array if a redirection code is returned
2241 if resp.status_code == 302:
23− return results
42+ return []
43+
2444 dom = html.fromstring(resp.text)
45+
46+ # parse results
2547 for result in dom.xpath('//div[contains(@class, "tt-a tt-fh")]'):
2648 link = result.xpath('.//a[contains(@class, "thumb")]')[0]
2749 url = urljoin(base_url, link.attrib.get('href'))
2850 title_links = result.xpath('.//span[@class="details"]//a[contains(@class, "t")]') # noqa
2951 title = ''.join(title_links[0].xpath('.//text()'))
3052 img_src = link.xpath('.//img')[0].attrib['src']
53+
54+ # append result
3155 results.append({'url': url,
3256 'title': title,
3357 'img_src': img_src,
3458 'template': 'images.html'})
59+
60+ # return results
3561 return results