dailymotion.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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=1&{query}' # noqa
  8. # TODO use video result template
  9. content_tpl = '<a href="{0}" title="{0}" ><img src="{1}" /></a><br />'
  10. def request(query, params):
  11. global search_url
  12. params['url'] = search_url.format(
  13. query=urlencode({'search': query, 'localization': locale}))
  14. return params
  15. def response(resp):
  16. results = []
  17. search_res = loads(resp.text)
  18. if not 'list' in search_res:
  19. return results
  20. for res in search_res['list']:
  21. title = res['title']
  22. url = res['url']
  23. if res['thumbnail_360_url']:
  24. content = content_tpl.format(url, res['thumbnail_360_url'])
  25. else:
  26. content = ''
  27. if res['description']:
  28. description = text_content_from_html(res['description'])
  29. content += description[:500]
  30. results.append({'url': url, 'title': title, 'content': content})
  31. return results
  32. def text_content_from_html(html_string):
  33. desc_html = html.fragment_fromstring(html_string, create_parent=True)
  34. return desc_html.text_content()