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.4 KiB

10 years ago
  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_urlparse,
  9. )
  10. from ..utils import (
  11. ExtractorError,
  12. float_or_none,
  13. )
  14. class CeskaTelevizeIE(InfoExtractor):
  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 = self.extract_subtitles(episode_id, subs)
  91. return {
  92. 'id': episode_id,
  93. 'title': title,
  94. 'description': description,
  95. 'thumbnail': thumbnail,
  96. 'duration': duration,
  97. 'formats': formats,
  98. 'subtitles': subtitles,
  99. }
  100. def _get_subtitles(self, episode_id, subs):
  101. original_subtitles = self._download_webpage(
  102. subs[0]['url'], episode_id, 'Downloading subtitles')
  103. srt_subs = self._fix_subtitles(original_subtitles)
  104. return {
  105. 'cs': [{
  106. 'ext': 'srt',
  107. 'data': srt_subs,
  108. }]
  109. }
  110. @staticmethod
  111. def _fix_subtitles(subtitles):
  112. """ Convert millisecond-based subtitles to SRT """
  113. def _msectotimecode(msec):
  114. """ Helper utility to convert milliseconds to timecode """
  115. components = []
  116. for divider in [1000, 60, 60, 100]:
  117. components.append(msec % divider)
  118. msec //= divider
  119. return "{3:02}:{2:02}:{1:02},{0:03}".format(*components)
  120. def _fix_subtitle(subtitle):
  121. for line in subtitle.splitlines():
  122. m = re.match(r"^\s*([0-9]+);\s*([0-9]+)\s+([0-9]+)\s*$", line)
  123. if m:
  124. yield m.group(1)
  125. start, stop = (_msectotimecode(int(t)) for t in m.groups()[1:])
  126. yield "{0} --> {1}".format(start, stop)
  127. else:
  128. yield line
  129. return "\r\n".join(_fix_subtitle(subtitles))