500px.py 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. # engine dependent config
  16. categories = ['images']
  17. paging = True
  18. # search-url
  19. base_url = 'https://500px.com'
  20. search_url = base_url+'/search?search?page={pageno}&type=photos&{query}'
  21. # do search-request
  22. def request(query, params):
  23. params['url'] = search_url.format(pageno=params['pageno'],
  24. query=urlencode({'q': query}))
  25. return params
  26. # get response from search-request
  27. def response(resp):
  28. results = []
  29. dom = html.fromstring(resp.text)
  30. # parse results
  31. for result in dom.xpath('//div[@class="photo"]'):
  32. link = result.xpath('.//a')[0]
  33. url = urljoin(base_url, link.attrib.get('href'))
  34. title = result.xpath('.//div[@class="title"]//text()')[0]
  35. img_src = link.xpath('.//img')[0].attrib['src']
  36. content = result.xpath('.//div[@class="info"]//text()')[0]
  37. # append result
  38. results.append({'url': url,
  39. 'title': title,
  40. 'img_src': img_src,
  41. 'content': content,
  42. 'template': 'images.html'})
  43. # return results
  44. return results