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.

192 lines
6.8 KiB

  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_urllib_request,
  7. compat_urllib_parse,
  8. compat_urllib_parse_unquote,
  9. compat_urllib_parse_urlparse,
  10. )
  11. from ..utils import (
  12. ExtractorError,
  13. float_or_none,
  14. )
  15. class CeskaTelevizeIE(InfoExtractor):
  16. _VALID_URL = r'https?://www\.ceskatelevize\.cz/(porady|ivysilani)/(?:[^/]+/)*(?P<id>[^/#?]+)/*(?:[#?].*)?$'
  17. _TESTS = [{
  18. 'url': 'http://www.ceskatelevize.cz/ivysilani/ivysilani/10441294653-hyde-park-civilizace/214411058091220',
  19. 'info_dict': {
  20. 'id': '61924494876951776',
  21. 'ext': 'mp4',
  22. 'title': 'Hyde Park Civilizace',
  23. 'description': 'md5:fe93f6eda372d150759d11644ebbfb4a',
  24. 'thumbnail': 're:^https?://.*\.jpg',
  25. 'duration': 3350,
  26. },
  27. 'params': {
  28. # m3u8 download
  29. 'skip_download': True,
  30. },
  31. }, {
  32. 'url': 'http://www.ceskatelevize.cz/ivysilani/10532695142-prvni-republika/bonus/14716-zpevacka-z-duparny-bobina',
  33. 'info_dict': {
  34. 'id': '61924494876844374',
  35. 'ext': 'mp4',
  36. 'title': 'První republika: Zpěvačka z Dupárny Bobina',
  37. 'description': 'Sága mapující atmosféru první republiky od r. 1918 do r. 1945.',
  38. 'thumbnail': 're:^https?://.*\.jpg',
  39. 'duration': 88.4,
  40. },
  41. 'params': {
  42. # m3u8 download
  43. 'skip_download': True,
  44. },
  45. }, {
  46. # video with 18+ caution trailer
  47. 'url': 'http://www.ceskatelevize.cz/porady/10520528904-queer/215562210900007-bogotart/',
  48. 'info_dict': {
  49. 'id': '215562210900007-bogotart',
  50. 'title': 'Queer: Bogotart',
  51. 'description': 'Alternativní průvodce současným queer světem',
  52. },
  53. 'playlist': [{
  54. 'info_dict': {
  55. 'id': '61924494876844842',
  56. 'ext': 'mp4',
  57. 'title': 'Queer: Bogotart (Varování 18+)',
  58. 'duration': 10.2,
  59. },
  60. }, {
  61. 'info_dict': {
  62. 'id': '61924494877068022',
  63. 'ext': 'mp4',
  64. 'title': 'Queer: Bogotart (Queer)',
  65. 'thumbnail': 're:^https?://.*\.jpg',
  66. 'duration': 1558.3,
  67. },
  68. }],
  69. 'params': {
  70. # m3u8 download
  71. 'skip_download': True,
  72. },
  73. }]
  74. def _real_extract(self, url):
  75. url = url.replace('/porady/', '/ivysilani/').replace('/video/', '')
  76. mobj = re.match(self._VALID_URL, url)
  77. playlist_id = mobj.group('id')
  78. webpage = self._download_webpage(url, playlist_id)
  79. NOT_AVAILABLE_STRING = 'This content is not available at your territory due to limited copyright.'
  80. if '%s</p>' % NOT_AVAILABLE_STRING in webpage:
  81. raise ExtractorError(NOT_AVAILABLE_STRING, expected=True)
  82. typ = self._html_search_regex(
  83. r'getPlaylistUrl\(\[\{"type":"(.+?)","id":".+?"\}\],', webpage, 'type')
  84. episode_id = self._html_search_regex(
  85. r'getPlaylistUrl\(\[\{"type":".+?","id":"(.+?)"\}\],', webpage, 'episode_id')
  86. data = {
  87. 'playlist[0][type]': typ,
  88. 'playlist[0][id]': episode_id,
  89. 'requestUrl': compat_urllib_parse_urlparse(url).path,
  90. 'requestSource': 'iVysilani',
  91. }
  92. req = compat_urllib_request.Request(
  93. 'http://www.ceskatelevize.cz/ivysilani/ajax/get-client-playlist',
  94. data=compat_urllib_parse.urlencode(data))
  95. req.add_header('Content-type', 'application/x-www-form-urlencoded')
  96. req.add_header('x-addr', '127.0.0.1')
  97. req.add_header('X-Requested-With', 'XMLHttpRequest')
  98. req.add_header('Referer', url)
  99. playlistpage = self._download_json(req, playlist_id)
  100. playlist_url = playlistpage['url']
  101. if playlist_url == 'error_region':
  102. raise ExtractorError(NOT_AVAILABLE_STRING, expected=True)
  103. req = compat_urllib_request.Request(compat_urllib_parse_unquote(playlist_url))
  104. req.add_header('Referer', url)
  105. playlist_title = self._og_search_title(webpage)
  106. playlist_description = self._og_search_description(webpage)
  107. playlist = self._download_json(req, playlist_id)['playlist']
  108. playlist_len = len(playlist)
  109. entries = []
  110. for item in playlist:
  111. formats = []
  112. for format_id, stream_url in item['streamUrls'].items():
  113. formats.extend(self._extract_m3u8_formats(
  114. stream_url, playlist_id, 'mp4', entry_protocol='m3u8_native'))
  115. self._sort_formats(formats)
  116. item_id = item.get('id') or item['assetId']
  117. title = item['title']
  118. duration = float_or_none(item.get('duration'))
  119. thumbnail = item.get('previewImageUrl')
  120. subtitles = {}
  121. if item.get('type') == 'VOD':
  122. subs = item.get('subtitles')
  123. if subs:
  124. subtitles = self.extract_subtitles(episode_id, subs)
  125. entries.append({
  126. 'id': item_id,
  127. 'title': playlist_title if playlist_len == 1 else '%s (%s)' % (playlist_title, title),
  128. 'description': playlist_description if playlist_len == 1 else None,
  129. 'thumbnail': thumbnail,
  130. 'duration': duration,
  131. 'formats': formats,
  132. 'subtitles': subtitles,
  133. })
  134. return self.playlist_result(entries, playlist_id, playlist_title, playlist_description)
  135. def _get_subtitles(self, episode_id, subs):
  136. original_subtitles = self._download_webpage(
  137. subs[0]['url'], episode_id, 'Downloading subtitles')
  138. srt_subs = self._fix_subtitles(original_subtitles)
  139. return {
  140. 'cs': [{
  141. 'ext': 'srt',
  142. 'data': srt_subs,
  143. }]
  144. }
  145. @staticmethod
  146. def _fix_subtitles(subtitles):
  147. """ Convert millisecond-based subtitles to SRT """
  148. def _msectotimecode(msec):
  149. """ Helper utility to convert milliseconds to timecode """
  150. components = []
  151. for divider in [1000, 60, 60, 100]:
  152. components.append(msec % divider)
  153. msec //= divider
  154. return "{3:02}:{2:02}:{1:02},{0:03}".format(*components)
  155. def _fix_subtitle(subtitle):
  156. for line in subtitle.splitlines():
  157. m = re.match(r"^\s*([0-9]+);\s*([0-9]+)\s+([0-9]+)\s*$", line)
  158. if m:
  159. yield m.group(1)
  160. start, stop = (_msectotimecode(int(t)) for t in m.groups()[1:])
  161. yield "{0} --> {1}".format(start, stop)
  162. else:
  163. yield line
  164. return "\r\n".join(_fix_subtitle(subtitles))