@@ -1,35 +1,61 @@
3+# @website https://www.deviantart.com/
4+# @provide-api yes (https://www.deviantart.com/developers/) (RSS)
6+# @using-api no (TODO, rewrite to api)
8+# @stable no (HTML can change)
9+# @parse url, title, thumbnail
11+# @todo rewrite to api
1from urllib import urlencode
13from urllib import urlencode
2from urlparse import urljoin
14from urlparse import urljoin
3from lxml import html
15from lxml import html
17+# engine dependent config
5categories = ['images']
18categories = ['images']
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}'
13def request(query, params):
27def request(query, params):
14 offset = (params['pageno'] - 1) * 24
28 offset = (params['pageno'] - 1) * 24
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}))
17 return params
33 return params
36+# get response from search-request
20def response(resp):
37def response(resp):
21 results = []
38 results = []
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 []
24 dom = html.fromstring(resp.text)
44 dom = html.fromstring(resp.text)
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']
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'})
35 return results
61 return results