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.

287 lines
10 KiB

Switch codebase to use sanitized_Request instead of compat_urllib_request.Request [downloader/dash] Use sanitized_Request [downloader/http] Use sanitized_Request [atresplayer] Use sanitized_Request [bambuser] Use sanitized_Request [bliptv] Use sanitized_Request [brightcove] Use sanitized_Request [cbs] Use sanitized_Request [ceskatelevize] Use sanitized_Request [collegerama] Use sanitized_Request [extractor/common] Use sanitized_Request [crunchyroll] Use sanitized_Request [dailymotion] Use sanitized_Request [dcn] Use sanitized_Request [dramafever] Use sanitized_Request [dumpert] Use sanitized_Request [eitb] Use sanitized_Request [escapist] Use sanitized_Request [everyonesmixtape] Use sanitized_Request [extremetube] Use sanitized_Request [facebook] Use sanitized_Request [fc2] Use sanitized_Request [flickr] Use sanitized_Request [4tube] Use sanitized_Request [gdcvault] Use sanitized_Request [extractor/generic] Use sanitized_Request [hearthisat] Use sanitized_Request [hotnewhiphop] Use sanitized_Request [hypem] Use sanitized_Request [iprima] Use sanitized_Request [ivi] Use sanitized_Request [keezmovies] Use sanitized_Request [letv] Use sanitized_Request [lynda] Use sanitized_Request [metacafe] Use sanitized_Request [minhateca] Use sanitized_Request [miomio] Use sanitized_Request [meovideo] Use sanitized_Request [mofosex] Use sanitized_Request [moniker] Use sanitized_Request [mooshare] Use sanitized_Request [movieclips] Use sanitized_Request [mtv] Use sanitized_Request [myvideo] Use sanitized_Request [neteasemusic] Use sanitized_Request [nfb] Use sanitized_Request [niconico] Use sanitized_Request [noco] Use sanitized_Request [nosvideo] Use sanitized_Request [novamov] Use sanitized_Request [nowness] Use sanitized_Request [nuvid] Use sanitized_Request [played] Use sanitized_Request [pluralsight] Use sanitized_Request [pornhub] Use sanitized_Request [pornotube] Use sanitized_Request [primesharetv] Use sanitized_Request [promptfile] Use sanitized_Request [qqmusic] Use sanitized_Request [rtve] Use sanitized_Request [safari] Use sanitized_Request [sandia] Use sanitized_Request [shared] Use sanitized_Request [sharesix] Use sanitized_Request [sina] Use sanitized_Request [smotri] Use sanitized_Request [sohu] Use sanitized_Request [spankwire] Use sanitized_Request [sportdeutschland] Use sanitized_Request [streamcloud] Use sanitized_Request [streamcz] Use sanitized_Request [tapely] Use sanitized_Request [tube8] Use sanitized_Request [tubitv] Use sanitized_Request [twitch] Use sanitized_Request [twitter] Use sanitized_Request [udemy] Use sanitized_Request [vbox7] Use sanitized_Request [veoh] Use sanitized_Request [vessel] Use sanitized_Request [vevo] Use sanitized_Request [viddler] Use sanitized_Request [videomega] Use sanitized_Request [viewvster] Use sanitized_Request [viki] Use sanitized_Request [vk] Use sanitized_Request [vodlocker] Use sanitized_Request [voicerepublic] Use sanitized_Request [wistia] Use sanitized_Request [xfileshare] Use sanitized_Request [xtube] Use sanitized_Request [xvideos] Use sanitized_Request [yandexmusic] Use sanitized_Request [youku] Use sanitized_Request [youporn] Use sanitized_Request [youtube] Use sanitized_Request [patreon] Use sanitized_Request [extractor/common] Remove unused import [nfb] PEP 8
9 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_parse_unquote,
  7. compat_urllib_parse_urlparse,
  8. )
  9. from ..utils import (
  10. ExtractorError,
  11. float_or_none,
  12. sanitized_Request,
  13. unescapeHTML,
  14. update_url_query,
  15. urlencode_postdata,
  16. USER_AGENTS,
  17. )
  18. class CeskaTelevizeIE(InfoExtractor):
  19. _VALID_URL = r'https?://(?:www\.)?ceskatelevize\.cz/ivysilani/(?:[^/?#&]+/)*(?P<id>[^/#?]+)'
  20. _TESTS = [{
  21. 'url': 'http://www.ceskatelevize.cz/ivysilani/ivysilani/10441294653-hyde-park-civilizace/214411058091220',
  22. 'info_dict': {
  23. 'id': '61924494877246241',
  24. 'ext': 'mp4',
  25. 'title': 'Hyde Park Civilizace: Život v Grónsku',
  26. 'description': 'md5:3fec8f6bb497be5cdb0c9e8781076626',
  27. 'thumbnail': r're:^https?://.*\.jpg',
  28. 'duration': 3350,
  29. },
  30. 'params': {
  31. # m3u8 download
  32. 'skip_download': True,
  33. },
  34. }, {
  35. 'url': 'http://www.ceskatelevize.cz/ivysilani/10441294653-hyde-park-civilizace/215411058090502/bonus/20641-bonus-01-en',
  36. 'info_dict': {
  37. 'id': '61924494877028507',
  38. 'ext': 'mp4',
  39. 'title': 'Hyde Park Civilizace: Bonus 01 - En',
  40. 'description': 'English Subtittles',
  41. 'thumbnail': r're:^https?://.*\.jpg',
  42. 'duration': 81.3,
  43. },
  44. 'params': {
  45. # m3u8 download
  46. 'skip_download': True,
  47. },
  48. }, {
  49. # live stream
  50. 'url': 'http://www.ceskatelevize.cz/ivysilani/zive/ct4/',
  51. 'info_dict': {
  52. 'id': 402,
  53. 'ext': 'mp4',
  54. 'title': r're:^ČT Sport \d{4}-\d{2}-\d{2} \d{2}:\d{2}$',
  55. 'is_live': True,
  56. },
  57. 'params': {
  58. # m3u8 download
  59. 'skip_download': True,
  60. },
  61. 'skip': 'Georestricted to Czech Republic',
  62. }, {
  63. 'url': 'http://www.ceskatelevize.cz/ivysilani/embed/iFramePlayer.php?hash=d6a3e1370d2e4fa76296b90bad4dfc19673b641e&IDEC=217 562 22150/0004&channelID=1&width=100%25',
  64. 'only_matching': True,
  65. }]
  66. def _real_extract(self, url):
  67. playlist_id = self._match_id(url)
  68. webpage = self._download_webpage(url, playlist_id)
  69. NOT_AVAILABLE_STRING = 'This content is not available at your territory due to limited copyright.'
  70. if '%s</p>' % NOT_AVAILABLE_STRING in webpage:
  71. raise ExtractorError(NOT_AVAILABLE_STRING, expected=True)
  72. type_ = None
  73. episode_id = None
  74. playlist = self._parse_json(
  75. self._search_regex(
  76. r'getPlaylistUrl\(\[({.+?})\]', webpage, 'playlist',
  77. default='{}'), playlist_id)
  78. if playlist:
  79. type_ = playlist.get('type')
  80. episode_id = playlist.get('id')
  81. if not type_:
  82. type_ = self._html_search_regex(
  83. r'getPlaylistUrl\(\[\{"type":"(.+?)","id":".+?"\}\],',
  84. webpage, 'type')
  85. if not episode_id:
  86. episode_id = self._html_search_regex(
  87. r'getPlaylistUrl\(\[\{"type":".+?","id":"(.+?)"\}\],',
  88. webpage, 'episode_id')
  89. data = {
  90. 'playlist[0][type]': type_,
  91. 'playlist[0][id]': episode_id,
  92. 'requestUrl': compat_urllib_parse_urlparse(url).path,
  93. 'requestSource': 'iVysilani',
  94. }
  95. entries = []
  96. for user_agent in (None, USER_AGENTS['Safari']):
  97. req = sanitized_Request(
  98. 'https://www.ceskatelevize.cz/ivysilani/ajax/get-client-playlist',
  99. data=urlencode_postdata(data))
  100. req.add_header('Content-type', 'application/x-www-form-urlencoded')
  101. req.add_header('x-addr', '127.0.0.1')
  102. req.add_header('X-Requested-With', 'XMLHttpRequest')
  103. if user_agent:
  104. req.add_header('User-Agent', user_agent)
  105. req.add_header('Referer', url)
  106. playlistpage = self._download_json(req, playlist_id, fatal=False)
  107. if not playlistpage:
  108. continue
  109. playlist_url = playlistpage['url']
  110. if playlist_url == 'error_region':
  111. raise ExtractorError(NOT_AVAILABLE_STRING, expected=True)
  112. req = sanitized_Request(compat_urllib_parse_unquote(playlist_url))
  113. req.add_header('Referer', url)
  114. playlist_title = self._og_search_title(webpage, default=None)
  115. playlist_description = self._og_search_description(webpage, default=None)
  116. playlist = self._download_json(req, playlist_id, fatal=False)
  117. if not playlist:
  118. continue
  119. playlist = playlist.get('playlist')
  120. if not isinstance(playlist, list):
  121. continue
  122. playlist_len = len(playlist)
  123. for num, item in enumerate(playlist):
  124. is_live = item.get('type') == 'LIVE'
  125. formats = []
  126. for format_id, stream_url in item.get('streamUrls', {}).items():
  127. if 'playerType=flash' in stream_url:
  128. stream_formats = self._extract_m3u8_formats(
  129. stream_url, playlist_id, 'mp4', 'm3u8_native',
  130. m3u8_id='hls-%s' % format_id, fatal=False)
  131. else:
  132. stream_formats = self._extract_mpd_formats(
  133. stream_url, playlist_id,
  134. mpd_id='dash-%s' % format_id, fatal=False)
  135. # See https://github.com/ytdl-org/youtube-dl/issues/12119#issuecomment-280037031
  136. if format_id == 'audioDescription':
  137. for f in stream_formats:
  138. f['source_preference'] = -10
  139. formats.extend(stream_formats)
  140. if user_agent and len(entries) == playlist_len:
  141. entries[num]['formats'].extend(formats)
  142. continue
  143. item_id = item.get('id') or item['assetId']
  144. title = item['title']
  145. duration = float_or_none(item.get('duration'))
  146. thumbnail = item.get('previewImageUrl')
  147. subtitles = {}
  148. if item.get('type') == 'VOD':
  149. subs = item.get('subtitles')
  150. if subs:
  151. subtitles = self.extract_subtitles(episode_id, subs)
  152. if playlist_len == 1:
  153. final_title = playlist_title or title
  154. if is_live:
  155. final_title = self._live_title(final_title)
  156. else:
  157. final_title = '%s (%s)' % (playlist_title, title)
  158. entries.append({
  159. 'id': item_id,
  160. 'title': final_title,
  161. 'description': playlist_description if playlist_len == 1 else None,
  162. 'thumbnail': thumbnail,
  163. 'duration': duration,
  164. 'formats': formats,
  165. 'subtitles': subtitles,
  166. 'is_live': is_live,
  167. })
  168. for e in entries:
  169. self._sort_formats(e['formats'])
  170. return self.playlist_result(entries, playlist_id, playlist_title, playlist_description)
  171. def _get_subtitles(self, episode_id, subs):
  172. original_subtitles = self._download_webpage(
  173. subs[0]['url'], episode_id, 'Downloading subtitles')
  174. srt_subs = self._fix_subtitles(original_subtitles)
  175. return {
  176. 'cs': [{
  177. 'ext': 'srt',
  178. 'data': srt_subs,
  179. }]
  180. }
  181. @staticmethod
  182. def _fix_subtitles(subtitles):
  183. """ Convert millisecond-based subtitles to SRT """
  184. def _msectotimecode(msec):
  185. """ Helper utility to convert milliseconds to timecode """
  186. components = []
  187. for divider in [1000, 60, 60, 100]:
  188. components.append(msec % divider)
  189. msec //= divider
  190. return '{3:02}:{2:02}:{1:02},{0:03}'.format(*components)
  191. def _fix_subtitle(subtitle):
  192. for line in subtitle.splitlines():
  193. m = re.match(r'^\s*([0-9]+);\s*([0-9]+)\s+([0-9]+)\s*$', line)
  194. if m:
  195. yield m.group(1)
  196. start, stop = (_msectotimecode(int(t)) for t in m.groups()[1:])
  197. yield '{0} --> {1}'.format(start, stop)
  198. else:
  199. yield line
  200. return '\r\n'.join(_fix_subtitle(subtitles))
  201. class CeskaTelevizePoradyIE(InfoExtractor):
  202. _VALID_URL = r'https?://(?:www\.)?ceskatelevize\.cz/porady/(?:[^/?#&]+/)*(?P<id>[^/#?]+)'
  203. _TESTS = [{
  204. # video with 18+ caution trailer
  205. 'url': 'http://www.ceskatelevize.cz/porady/10520528904-queer/215562210900007-bogotart/',
  206. 'info_dict': {
  207. 'id': '215562210900007-bogotart',
  208. 'title': 'Queer: Bogotart',
  209. 'description': 'Alternativní průvodce současným queer světem',
  210. },
  211. 'playlist': [{
  212. 'info_dict': {
  213. 'id': '61924494876844842',
  214. 'ext': 'mp4',
  215. 'title': 'Queer: Bogotart (Varování 18+)',
  216. 'duration': 10.2,
  217. },
  218. }, {
  219. 'info_dict': {
  220. 'id': '61924494877068022',
  221. 'ext': 'mp4',
  222. 'title': 'Queer: Bogotart (Queer)',
  223. 'thumbnail': r're:^https?://.*\.jpg',
  224. 'duration': 1558.3,
  225. },
  226. }],
  227. 'params': {
  228. # m3u8 download
  229. 'skip_download': True,
  230. },
  231. }, {
  232. # iframe embed
  233. 'url': 'http://www.ceskatelevize.cz/porady/10614999031-neviditelni/21251212048/',
  234. 'only_matching': True,
  235. }]
  236. def _real_extract(self, url):
  237. video_id = self._match_id(url)
  238. webpage = self._download_webpage(url, video_id)
  239. data_url = update_url_query(unescapeHTML(self._search_regex(
  240. (r'<span[^>]*\bdata-url=(["\'])(?P<url>(?:(?!\1).)+)\1',
  241. r'<iframe[^>]+\bsrc=(["\'])(?P<url>(?:https?:)?//(?:www\.)?ceskatelevize\.cz/ivysilani/embed/iFramePlayer\.php.*?)\1'),
  242. webpage, 'iframe player url', group='url')), query={
  243. 'autoStart': 'true',
  244. })
  245. return self.url_result(data_url, ie=CeskaTelevizeIE.ie_key())