kika.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..utils import ExtractorError
  5. class KikaIE(InfoExtractor):
  6. _VALID_URL = r'https?://(?:www\.)?kika\.de/(?:[a-z-]+/)*(?:video|sendung)(?P<id>\d+).*'
  7. _TESTS = [
  8. {
  9. 'url': 'http://www.kika.de/baumhaus/videos/video9572.html',
  10. 'md5': '94fc748cf5d64916571d275a07ffe2d5',
  11. 'info_dict': {
  12. 'id': '9572',
  13. 'ext': 'mp4',
  14. 'title': 'Baumhaus vom 29. Oktober 2014',
  15. 'description': None
  16. }
  17. },
  18. {
  19. 'url': 'http://www.kika.de/sendungen/einzelsendungen/weihnachtsprogramm/videos/video8182.html',
  20. 'md5': '5fe9c4dd7d71e3b238f04b8fdd588357',
  21. 'info_dict': {
  22. 'id': '8182',
  23. 'ext': 'mp4',
  24. 'title': 'Beutolomäus und der geheime Weihnachtswunsch',
  25. 'description': 'md5:b69d32d7b2c55cbe86945ab309d39bbd'
  26. }
  27. },
  28. {
  29. 'url': 'http://www.kika.de/videos/allevideos/video9572_zc-32ca94ad_zs-3f535991.html',
  30. 'md5': '94fc748cf5d64916571d275a07ffe2d5',
  31. 'info_dict': {
  32. 'id': '9572',
  33. 'ext': 'mp4',
  34. 'title': 'Baumhaus vom 29. Oktober 2014',
  35. 'description': None
  36. }
  37. },
  38. {
  39. 'url': 'http://www.kika.de/sendungen/einzelsendungen/weihnachtsprogramm/videos/sendung81244_zc-81d703f8_zs-f82d5e31.html',
  40. 'md5': '5fe9c4dd7d71e3b238f04b8fdd588357',
  41. 'info_dict': {
  42. 'id': '8182',
  43. 'ext': 'mp4',
  44. 'title': 'Beutolomäus und der geheime Weihnachtswunsch',
  45. 'description': 'md5:b69d32d7b2c55cbe86945ab309d39bbd'
  46. }
  47. }
  48. ]
  49. def _real_extract(self, url):
  50. # broadcast_id may be the same as the video_id
  51. broadcast_id = self._match_id(url)
  52. webpage = self._download_webpage(url, broadcast_id)
  53. xml_re = r'sectionArticle[ "](?:(?!sectionA[ "])(?:.|\n))*?dataURL:\'(?:/[a-z-]+?)*?/video(\d+)-avCustom\.xml'
  54. video_id = self._search_regex(xml_re, webpage, "xml_url", default=None)
  55. if not video_id:
  56. # Video is not available online
  57. err_msg = 'Video %s is not available online' % broadcast_id
  58. raise ExtractorError(err_msg, expected=True)
  59. xml_url = 'http://www.kika.de/video%s-avCustom.xml' % (video_id)
  60. xml_tree = self._download_xml(xml_url, video_id)
  61. title = xml_tree.find('title').text
  62. webpage_url = xml_tree.find('htmlUrl').text
  63. # Try to get the description, not available for all videos
  64. try:
  65. broadcast_elem = xml_tree.find('broadcast')
  66. description = broadcast_elem.find('broadcastDescription').text
  67. except AttributeError:
  68. # No description available
  69. description = None
  70. # duration string format is mm:ss (even if it is >= 1 hour, e.g. 78:42)
  71. tmp = xml_tree.find('duration').text.split(':')
  72. duration = int(tmp[0]) * 60 + int(tmp[1])
  73. formats_list = []
  74. for elem in xml_tree.find('assets'):
  75. format_dict = {}
  76. format_dict['url'] = elem.find('progressiveDownloadUrl').text
  77. format_dict['ext'] = elem.find('mediaType').text.lower()
  78. format_dict['format'] = elem.find('profileName').text
  79. width = int(elem.find('frameWidth').text)
  80. height = int(elem.find('frameHeight').text)
  81. format_dict['width'] = width
  82. format_dict['height'] = height
  83. format_dict['resolution'] = '%dx%d' % (width, height)
  84. format_dict['abr'] = int(elem.find('bitrateAudio').text)
  85. format_dict['vbr'] = int(elem.find('bitrateVideo').text)
  86. format_dict['tbr'] = format_dict['abr'] + format_dict['vbr']
  87. format_dict['filesize'] = int(elem.find('fileSize').text)
  88. # append resolution and dict for sorting by resolution
  89. formats_list.append((width * height, format_dict))
  90. # Sort by resolution (=quality)
  91. formats_list.sort()
  92. out_list = [x[1] for x in formats_list]
  93. return {
  94. 'id': video_id,
  95. 'title': title,
  96. 'description': description,
  97. 'formats': out_list,
  98. 'duration': duration,
  99. 'webpage_url': webpage_url
  100. }