www500px.py 1.8KB

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