500px.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. # 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 = result.xpath('.//div[@class="title"]//text()')[0]
  37. thumbnail_src = link.xpath('.//img')[0].attrib['src']
  38. # To have a bigger thumbnail, uncomment the next line
  39. #thumbnail_src = regex.sub('4.jpg', thumbnail_src)
  40. content = result.xpath('.//div[@class="info"]//text()')[0]
  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