www500px.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. ## 500px (Images)
  2. #
  3. # @website https://500px.com
  4. # @provide-api yes (https://developers.500px.com/)
  5. #
  6. # @using-api no
  7. # @results HTML
  8. # @stable no (HTML can change)
  9. # @parse url, title, thumbnail, img_src, content
  10. #
  11. # @todo rewrite to api
  12. from urllib import urlencode
  13. from urlparse import urljoin
  14. from lxml import html
  15. import re
  16. from searx.engines.xpath import extract_text
  17. # engine dependent config
  18. categories = ['images']
  19. paging = True
  20. # search-url
  21. base_url = 'https://500px.com'
  22. search_url = base_url + '/search?search?page={pageno}&type=photos&{query}'
  23. # do search-request
  24. def request(query, params):
  25. params['url'] = search_url.format(pageno=params['pageno'],
  26. query=urlencode({'q': query}))
  27. return params
  28. # get response from search-request
  29. def response(resp):
  30. results = []
  31. dom = html.fromstring(resp.text)
  32. regex = re.compile('3\.jpg.*$')
  33. # parse results
  34. for result in dom.xpath('//div[@class="photo"]'):
  35. link = result.xpath('.//a')[0]
  36. url = urljoin(base_url, link.attrib.get('href'))
  37. title = extract_text(result.xpath('.//div[@class="title"]'))
  38. thumbnail_src = link.xpath('.//img')[0].attrib.get('src')
  39. # To have a bigger thumbnail, uncomment the next line
  40. # thumbnail_src = regex.sub('4.jpg', thumbnail_src)
  41. content = extract_text(result.xpath('.//div[@class="info"]'))
  42. img_src = regex.sub('2048.jpg', thumbnail_src)
  43. # append result
  44. results.append({'url': url,
  45. 'title': title,
  46. 'img_src': img_src,
  47. 'content': content,
  48. 'thumbnail_src': thumbnail_src,
  49. 'template': 'images.html'})
  50. # return results
  51. return results