You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
5.5 KiB

10 years ago
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import re
  4. from .subtitles import SubtitlesInfoExtractor
  5. from ..compat import (
  6. compat_urllib_request,
  7. compat_urllib_parse,
  8. compat_urllib_parse_urlparse,
  9. )
  10. from ..utils import (
  11. ExtractorError,
  12. float_or_none,
  13. )
  14. class CeskaTelevizeIE(SubtitlesInfoExtractor):
  15. _VALID_URL = r'https?://www\.ceskatelevize\.cz/(porady|ivysilani)/(.+/)?(?P<id>[^?#]+)'
  16. _TESTS = [
  17. {
  18. 'url': 'http://www.ceskatelevize.cz/ivysilani/ivysilani/10441294653-hyde-park-civilizace/214411058091220',
  19. 'info_dict': {
  20. 'id': '214411058091220',
  21. 'ext': 'mp4',
  22. 'title': 'Hyde Park Civilizace',
  23. 'description': 'Věda a současná civilizace. Interaktivní pořad - prostor pro vaše otázky a komentáře',
  24. 'thumbnail': 're:^https?://.*\.jpg',
  25. 'duration': 3350,
  26. },
  27. 'params': {
  28. # m3u8 download
  29. 'skip_download': True,
  30. },
  31. },
  32. {
  33. 'url': 'http://www.ceskatelevize.cz/ivysilani/10532695142-prvni-republika/bonus/14716-zpevacka-z-duparny-bobina',
  34. 'info_dict': {
  35. 'id': '14716',
  36. 'ext': 'mp4',
  37. 'title': 'První republika: Zpěvačka z Dupárny Bobina',
  38. 'description': 'Sága mapující atmosféru první republiky od r. 1918 do r. 1945.',
  39. 'thumbnail': 're:^https?://.*\.jpg',
  40. 'duration': 88.4,
  41. },
  42. 'params': {
  43. # m3u8 download
  44. 'skip_download': True,
  45. },
  46. },
  47. ]
  48. def _real_extract(self, url):
  49. url = url.replace('/porady/', '/ivysilani/').replace('/video/', '')
  50. mobj = re.match(self._VALID_URL, url)
  51. video_id = mobj.group('id')
  52. webpage = self._download_webpage(url, video_id)
  53. NOT_AVAILABLE_STRING = 'This content is not available at your territory due to limited copyright.'
  54. if '%s</p>' % NOT_AVAILABLE_STRING in webpage:
  55. raise ExtractorError(NOT_AVAILABLE_STRING, expected=True)
  56. typ = self._html_search_regex(r'getPlaylistUrl\(\[\{"type":"(.+?)","id":".+?"\}\],', webpage, 'type')
  57. episode_id = self._html_search_regex(r'getPlaylistUrl\(\[\{"type":".+?","id":"(.+?)"\}\],', webpage, 'episode_id')
  58. data = {
  59. 'playlist[0][type]': typ,
  60. 'playlist[0][id]': episode_id,
  61. 'requestUrl': compat_urllib_parse_urlparse(url).path,
  62. 'requestSource': 'iVysilani',
  63. }
  64. req = compat_urllib_request.Request(
  65. 'http://www.ceskatelevize.cz/ivysilani/ajax/get-client-playlist',
  66. data=compat_urllib_parse.urlencode(data))
  67. req.add_header('Content-type', 'application/x-www-form-urlencoded')
  68. req.add_header('x-addr', '127.0.0.1')
  69. req.add_header('X-Requested-With', 'XMLHttpRequest')
  70. req.add_header('Referer', url)
  71. playlistpage = self._download_json(req, video_id)
  72. playlist_url = playlistpage['url']
  73. if playlist_url == 'error_region':
  74. raise ExtractorError(NOT_AVAILABLE_STRING, expected=True)
  75. req = compat_urllib_request.Request(compat_urllib_parse.unquote(playlist_url))
  76. req.add_header('Referer', url)
  77. playlist = self._download_json(req, video_id)
  78. item = playlist['playlist'][0]
  79. formats = []
  80. for format_id, stream_url in item['streamUrls'].items():
  81. formats.extend(self._extract_m3u8_formats(stream_url, video_id, 'mp4'))
  82. self._sort_formats(formats)
  83. title = self._og_search_title(webpage)
  84. description = self._og_search_description(webpage)
  85. duration = float_or_none(item.get('duration'))
  86. thumbnail = item.get('previewImageUrl')
  87. subtitles = {}
  88. subs = item.get('subtitles')
  89. if subs:
  90. subtitles['cs'] = subs[0]['url']
  91. if self._downloader.params.get('listsubtitles', False):
  92. self._list_available_subtitles(video_id, subtitles)
  93. return
  94. subtitles = self._fix_subtitles(self.extract_subtitles(video_id, subtitles))
  95. return {
  96. 'id': episode_id,
  97. 'title': title,
  98. 'description': description,
  99. 'thumbnail': thumbnail,
  100. 'duration': duration,
  101. 'formats': formats,
  102. 'subtitles': subtitles,
  103. }
  104. @staticmethod
  105. def _fix_subtitles(subtitles):
  106. """ Convert millisecond-based subtitles to SRT """
  107. if subtitles is None:
  108. return subtitles # subtitles not requested
  109. def _msectotimecode(msec):
  110. """ Helper utility to convert milliseconds to timecode """
  111. components = []
  112. for divider in [1000, 60, 60, 100]:
  113. components.append(msec % divider)
  114. msec //= divider
  115. return "{3:02}:{2:02}:{1:02},{0:03}".format(*components)
  116. def _fix_subtitle(subtitle):
  117. for line in subtitle.splitlines():
  118. m = re.match(r"^\s*([0-9]+);\s*([0-9]+)\s+([0-9]+)\s*$", line)
  119. if m:
  120. yield m.group(1)
  121. start, stop = (_msectotimecode(int(t)) for t in m.groups()[1:])
  122. yield "{0} --> {1}".format(start, stop)
  123. else:
  124. yield line
  125. fixed_subtitles = {}
  126. for k, v in subtitles.items():
  127. fixed_subtitles[k] = "\r\n".join(_fix_subtitle(v))
  128. return fixed_subtitles