flickr.py 1000 B

12345678910111213141516171819202122232425262728293031323334
  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}'
  8. results_xpath = '//div[@id="thumbnails"]//a[@class="rapidnofollow photo-click" and @data-track="photo-click"]' # noqa
  9. def request(query, params):
  10. params['url'] = search_url.format(query=urlencode({'q': query}))
  11. return params
  12. def response(resp):
  13. global base_url
  14. results = []
  15. dom = html.fromstring(resp.text)
  16. for result in dom.xpath(results_xpath):
  17. href = urljoin(url, result.attrib.get('href'))
  18. img = result.xpath('.//img')[0]
  19. title = img.attrib.get('alt', '')
  20. img_src = img.attrib.get('data-defer-src')
  21. if not img_src:
  22. continue
  23. results.append({'url': href,
  24. 'title': title,
  25. 'img_src': img_src,
  26. 'template': 'images.html'})
  27. return results