flickr.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/usr/bin/env python
  2. from urllib import urlencode
  3. from lxml import html
  4. from urlparse import urljoin
  5. categories = ['images']
  6. url = 'https://secure.flickr.com/'
  7. search_url = url+'search/?{query}&page={page}'
  8. results_xpath = '//div[@id="thumbnails"]//a[@class="rapidnofollow photo-click" and @data-track="photo-click"]' # noqa
  9. paging = True
  10. def request(query, params):
  11. params['url'] = search_url.format(query=urlencode({'q': query}),
  12. page=params['pageno'])
  13. return params
  14. def response(resp):
  15. global base_url
  16. results = []
  17. dom = html.fromstring(resp.text)
  18. for result in dom.xpath(results_xpath):
  19. href = urljoin(url, result.attrib.get('href'))
  20. img = result.xpath('.//img')[0]
  21. title = img.attrib.get('alt', '')
  22. img_src = img.attrib.get('data-defer-src')
  23. if not img_src:
  24. continue
  25. results.append({'url': href,
  26. 'title': title,
  27. 'img_src': img_src,
  28. 'template': 'images.html'})
  29. return results