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.

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