dailymotion.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from urllib import urlencode
  2. from lxml import html
  3. from json import loads
  4. categories = ['videos']
  5. locale = 'en_US'
  6. # see http://www.dailymotion.com/doc/api/obj-video.html
  7. search_url = 'https://api.dailymotion.com/videos?fields=title,description,duration,url,thumbnail_360_url&sort=relevance&limit=25&page={pageno}&{query}' # noqa
  8. # TODO use video result template
  9. content_tpl = '<a href="{0}" title="{0}" ><img src="{1}" /></a><br />'
  10. paging = True
  11. def request(query, params):
  12. params['url'] = search_url.format(
  13. query=urlencode({'search': query, 'localization': locale}),
  14. pageno=params['pageno'])
  15. return params
  16. def response(resp):
  17. results = []
  18. search_res = loads(resp.text)
  19. if not 'list' in search_res:
  20. return results
  21. for res in search_res['list']:
  22. title = res['title']
  23. url = res['url']
  24. if res['thumbnail_360_url']:
  25. content = content_tpl.format(url, res['thumbnail_360_url'])
  26. else:
  27. content = ''
  28. if res['description']:
  29. description = text_content_from_html(res['description'])
  30. content += description[:500]
  31. results.append({'url': url, 'title': title, 'content': content})
  32. return results
  33. def text_content_from_html(html_string):
  34. desc_html = html.fragment_fromstring(html_string, create_parent=True)
  35. return desc_html.text_content()