Browse Source

add comments to deviantart engine

Thomas Pointhuber 10 years ago
parent
commit
16d1526818
1 changed files with 29 additions and 3 deletions
  1. 29
    3
      searx/engines/deviantart.py

+ 29
- 3
searx/engines/deviantart.py View File

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
 from urllib import urlencode
13
 from urllib import urlencode
2
 from urlparse import urljoin
14
 from urlparse import urljoin
3
 from lxml import html
15
 from lxml import html
4
 
16
 
17
+# engine dependent config
5
 categories = ['images']
18
 categories = ['images']
19
+paging = True
6
 
20
 
21
+# search-url
7
 base_url = 'https://www.deviantart.com/'
22
 base_url = 'https://www.deviantart.com/'
8
 search_url = base_url+'search?offset={offset}&{query}'
23
 search_url = base_url+'search?offset={offset}&{query}'
9
 
24
 
10
-paging = True
11
-
12
 
25
 
26
+# do search-request
13
 def request(query, params):
27
 def 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
20
 def response(resp):
37
 def 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