blekko_images.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. """
  2. Blekko (Images)
  3. @website https://blekko.com
  4. @provide-api yes (inofficial)
  5. @using-api yes
  6. @results JSON
  7. @stable yes
  8. @parse url, title, img_src
  9. """
  10. from json import loads
  11. from urllib import urlencode
  12. # engine dependent config
  13. categories = ['images']
  14. paging = True
  15. safesearch = True
  16. # search-url
  17. base_url = 'https://blekko.com'
  18. search_url = '/api/images?{query}&c={c}'
  19. # safesearch definitions
  20. safesearch_types = {2: '1',
  21. 1: '',
  22. 0: '0'}
  23. # do search-request
  24. def request(query, params):
  25. c = (params['pageno'] - 1) * 48
  26. params['url'] = base_url +\
  27. search_url.format(query=urlencode({'q': query}),
  28. c=c)
  29. if params['pageno'] != 1:
  30. params['url'] += '&page={pageno}'.format(pageno=(params['pageno'] - 1))
  31. # let Blekko know we wan't have profiling
  32. params['cookies']['tag_lesslogging'] = '1'
  33. # parse safesearch argument
  34. params['cookies']['safesearch'] = safesearch_types.get(params['safesearch'], '')
  35. return params
  36. # get response from search-request
  37. def response(resp):
  38. results = []
  39. search_results = loads(resp.text)
  40. # return empty array if there are no results
  41. if not search_results:
  42. return []
  43. for result in search_results:
  44. # append result
  45. results.append({'url': result['page_url'],
  46. 'title': result['title'],
  47. 'content': '',
  48. 'img_src': result['url'],
  49. 'template': 'images.html'})
  50. # return results
  51. return results