dailymotion.py 1.3 KB

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