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