Ver código fonte

[mod] 500px rewrite

Adam Tauber 9 anos atrás
pai
commit
1a82ed6f54
1 arquivos alterados com 26 adições e 17 exclusões
  1. 26
    17
      searx/engines/www500px.py

+ 26
- 17
searx/engines/www500px.py Ver arquivo

@@ -12,12 +12,10 @@
12 12
  @todo        rewrite to api
13 13
 """
14 14
 
15
-
15
+from json import loads
16 16
 from urllib import urlencode
17 17
 from urlparse import urljoin
18
-from lxml import html
19
-import re
20
-from searx.engines.xpath import extract_text
18
+from xml.sax.saxutils import escape
21 19
 
22 20
 # engine dependent config
23 21
 categories = ['images']
@@ -25,13 +23,27 @@ paging = True
25 23
 
26 24
 # search-url
27 25
 base_url = 'https://500px.com'
28
-search_url = base_url + '/search?search?page={pageno}&type=photos&{query}'
26
+search_url = 'https://api.500px.com/v1/photos/search?type=photos'\
27
+    '&{query}'\
28
+    '&image_size%5B%5D=4'\
29
+    '&image_size%5B%5D=20'\
30
+    '&image_size%5B%5D=21'\
31
+    '&image_size%5B%5D=1080'\
32
+    '&image_size%5B%5D=1600'\
33
+    '&image_size%5B%5D=2048'\
34
+    '&include_states=true'\
35
+    '&formats=jpeg%2Clytro'\
36
+    '&include_tags=true'\
37
+    '&exclude_nude=true'\
38
+    '&page={pageno}'\
39
+    '&rpp=50'\
40
+    '&sdk_key=b68e60cff4c929bedea36ca978830c5caca790c3'
29 41
 
30 42
 
31 43
 # do search-request
32 44
 def request(query, params):
33 45
     params['url'] = search_url.format(pageno=params['pageno'],
34
-                                      query=urlencode({'q': query}))
46
+                                      query=urlencode({'term': query}))
35 47
 
36 48
     return params
37 49
 
@@ -40,19 +52,16 @@ def request(query, params):
40 52
 def response(resp):
41 53
     results = []
42 54
 
43
-    dom = html.fromstring(resp.text)
44
-    regex = re.compile(r'3\.jpg.*$')
55
+    response_json = loads(resp.text)
45 56
 
46 57
     # parse results
47
-    for result in dom.xpath('//div[@class="photo"]'):
48
-        link = result.xpath('.//a')[0]
49
-        url = urljoin(base_url, link.attrib.get('href'))
50
-        title = extract_text(result.xpath('.//div[@class="title"]'))
51
-        thumbnail_src = link.xpath('.//img')[0].attrib.get('src')
52
-        # To have a bigger thumbnail, uncomment the next line
53
-        # thumbnail_src = regex.sub('4.jpg', thumbnail_src)
54
-        content = extract_text(result.xpath('.//div[@class="info"]'))
55
-        img_src = regex.sub('2048.jpg', thumbnail_src)
58
+    for result in response_json['photos']:
59
+        url = urljoin(base_url, result['url'])
60
+        title = escape(result['name'])
61
+        # last index is the biggest resolution
62
+        img_src = result['image_url'][-1]
63
+        thumbnail_src = result['image_url'][0]
64
+        content = escape(result['description'] or '')
56 65
 
57 66
         # append result
58 67
         results.append({'url': url,